To change the name of a system running the Ubuntu Linux operating system from a command-line interface (CLI), i.e., a terminal window, you can take the following steps:
-
In the terminal window enter the command
sudo hostnamectl set-hostname newnamewhere newname is the new name you wish to assign to the system. E.g.:jim@Firefly:~$ sudo hostnamectl set-hostname Smaug [sudo: authenticate] Password: jim@Firefly:~$
That will change the host name stored in
/etc/hostname. -
Then edit the
/etc/hostsfile, replacing the old host name there with the new one. E.g., if I had the following lines in the hosts file, I would modify the second line containing the old host name.127.0.0.1 localhost 127.0.1.1 Firefly
The 127.0.0.1 in the above lines is the localhost IP address, a loopback address that can be used when troubleshooting network issues. Ubuntu also adds a 127.0.1.1 address in
/etc/hostswith the name you have assigned to the system. The name should match the one in/etc/hostname, so change the name for the 127.0.1.1 address to the new name you wish to use.
References:
-
What is difference between localhost address 127.0.0.1 and 127.0.1.1
Updated: April 2, 2021
Ask Ubuntu
