How can the host name be changed permanently under Ubuntu 20.04 LTS Linux?
This page provides step-by-step instructions for changing the host name in Ubuntu 20.04 LTS. Please note that the host name is defined at the time of installation by a system administrator or an online service provider such as Linode, AWS, and others. To change the name of the Ubuntu machine, you must log in as root. The best way to do this is through command sudo.
Procedure for changing computer names on Ubuntu Linux 20.04 LTS
- Carry out the following command:
sudo hostnamectl set-hostname new-computer-name-here.
- Another option is to enter the following command to edit /etc/hostname with the nano word processor:
sudo nano /etc/hostname
- Remove the old name and install the new one.
- Edit the /etc/hosts file:
sudo nano /etc/hosts
- Replace each event of an existing computer name with a new one.
- Restart the system to make the changes take effect:
Restarting the gardens
Display current host name
The Ubuntu Linux 20.04 LTS server or desktop can simply use the hostnamectl command to change the hostname. To see the current setting, type the following command:
Changing the host name in Ubuntu using hostnamectl
In this example I set the hostname of the system to ln-sg-vpn-001 with the command hostnamectl:
sudo hostnamectl set-hostname ln-sg-vpn-001
Note that the hostnamectl command is silent and will not give any results on screen. We can test the success or failure of the hostname or the Hostnamectl team ourselves. In other words, make sure the name of the Ubuntu computer is changed: hostnamectl Change Ubuntu hostname 20.04 LTS command
Update of the /etc/hosts file
You need to change the /etc/hosts file. This is a simple text file that associates IP addresses with host names, one line per IP address in the following format. Use the cat command to display the current settings:
Test Outputs :
# For IPv6 compatible hosts
::1 localhost ip6-localhost ip6-loopback
ff02 ::1 ip6-allnodes
ff02 ::2 ip6-allouters the following lines are desirable
Make sure you change the old host name (e.g. localhost) to the new one (ln-sg-vpn-001):
sudo vi /etc/hosts
Here is my complete folder :
# For IPv6 hosts
::1 localhost ip6 localhost ip6 loopback
ff02 ::1 ip6 allnodes
ff02 ::2 ip6 allrouters
Save the file and close it in vim.
Reboot the Linux system (optional)
This is an optional step and not mandatory. Enter one of the following commands:
sudo systemctl restart
## OR ##
## OR ##
sudo shutdown -r now
A reboot of the system is an extra step and is not necessary. This page explains how to change the hostname of the Ubuntu server without rebooting the system. More information can be found here on the Hostnamectle Maine page.
Published: Vivek gears
The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and Linux/Unix shell script trainer. Receive the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly by email.