How to Change Hostname on Ubuntu 20.04

How do I change the hostname on Ubuntu 20.04 LTS Linux system permanently?

This step-by-step page shows commands for changing the hostname on an Ubuntu 20.04 LTS system. Please note that the hostname set at the time of installation either by sysadmin or cloud service provider such as Linode, AWS, and others. To change the Ubuntu computer name, you must log in as the root user. The best way to do this is to use the sudo command.

ADVERTISEMENTS

The procedure to change the computer name on Ubuntu Linux 20.04 LTS

  1. Run the following command:
    sudo hostnamectl set-hostname new-computer-name-here
  2. Another option is to type the following command to edit /etc/hostname using nano text editor:
    sudo nano /etc/hostname
  3. Remove the old name and setup new name.
  4. Edit the /etc/hosts file:
    sudo nano /etc/hosts
  5. Replace any occurrence of the existing computer name with your new one.
  6. Reboot the system to changes take effect:
    sudo reboot

Show the current hostname

Ubuntu Linux 20.04 LTS server or desktop can simply use the hostnamectl command to change the hostname. To see current setting just type the following command:
hostnamectl
Ubuntu 20.4 LTS hostnamectl command

Changing the hostname on Ubuntu using the hostnamectl

In this example, I am going to set the system hostname to ln-sg-vpn-001 using the hostnamectl:
sudo hostnamectl set-hostname ln-sg-vpn-001
Please note that the hostnamectl command works silently and does not provide any output on the screen. We can verify the success or failure of using either the hostname or hostnamectl command itself. In other words, verify that the Ubuntu computer name changed:
hostname
hostnamectl

Change Hostname Ubuntu 20.04 LTS command

Update the /etc/hosts file

You must edit the /etc/hosts file. It is a simple text file that associates IP addresses with hostnames, one line per IP address in the following format. Use the cat command to display current settings:
cat /etc/hosts
Sample outputs:

127.0.0.1	localhost

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Make sure you change the old hostname (such as localhost) to the new one (ln-sg-vpn-001):
sudo vi /etc/hosts
Find:
202.54.2.5 localhost
To:
202.54.2.5 ln-sg-vpn-001
Here is my complete file:

127.0.0.1	localhost
202.54.2.5      ln-sg-vpn-001
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Save and close the file in vim.

Reboot the Linux system (optional)

This is optional step and not required. Type any one of the following reboot command/shutdown command/systemctl command:
sudo systemctl reboot
## OR ##
sudo reboot
## OR ##
sudo shutdown -r now

Conclusion

The rebooting of the system is an optional step and not required. This page explained how to change your Ubuntu server hostname without restarting the system. See the hostnamectl man page here for more info.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.