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.
The procedure to change the computer name on Ubuntu Linux 20.04 LTS
- Run the following command:
sudo hostnamectl set-hostname new-computer-name-here
- Another option is to type the following command to edit /etc/hostname using nano text editor:
sudo nano /etc/hostname
- Remove the old name and setup new name.
- Edit the /etc/hosts file:
sudo nano /etc/hosts
- Replace any occurrence of the existing computer name with your new one.
- Reboot the system to changes take effect:
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:
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:
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:
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
Here is my complete file:
127.0.0.1 localhost 220.127.116.11 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
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 ##
## OR ##
sudo shutdown -r now
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.