Ubuntu 20.04 LTS change hostname permanently

/etc/hostname

ADVERTISEMENTS



How to display the current Ubuntu hostname

I am a brand-new Ubuntu Linux 20.04 LTS VM user. By default, my server name to ‘localhost’ during cloud installation, but now I would like to change the computer name to ‘ubuntu-2004-nixcraft’. Can you tell me how do I remove the localhost and set it to ubuntu-2004-nixcraft on Ubuntu 20.04 Linux? How do I change the Ubuntu computer name from ‘localhost’ to ‘ubuntu-2004-nixcraft’? Can you tell me more about Ubuntu Linux change hostname command?One can use the hostnamectl command to view or set the Ubuntu 20.04 LTS server’s host name. The host name or computer name is usually at system startup infile. Open the Terminal application and type the following commands to set or change the hostname or computer name on Ubuntu.

Run the following command:

hostnamectl

Of course, you can use the echo command/printf command to print hostname too:

echo " $HOSTNAME " printf "%s

" $HOSTNAME hostname echo "$HOSTNAME" printf "%s

" $HOSTNAME hostname

/etc/hostname

cat /etc/hostname

Ubuntu 20.04 LTS change hostname permanently

Finally, try the cat command on

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

Type the hostnamectl command :

sudo hostnamectl set-hostname newNameHere

Delete the old name and setup new name. Next Edit the /etc/hosts file:

sudo nano /etc/hosts

Replace any occurrence of the existing computer name with your new hostname. Reboot the system to changes take effect:

sudo reboot

How to change hostname on Ubuntu 20.04 without a system restart

Let us view current computer name setting, just type the following command:

hostnamectl

Next change hostname from localhost to ubuntu-2004-nixcraft, enter:

sudo hostnamectl set-hostname ubuntu-2004-nixcraft

Verify new changes:

hostnamectl



ip a

ip a s eth0

12 : eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 00: 16 :3e: 24 :b3:08 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.105.28.43/ 24 brd 10.105.28.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80:: 216 :3eff:fe24:b308/ 64 scope link valid_lft forever preferred_lft forever 12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 00:16:3e:24:b3:08 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.105.28.43/24 brd 10.105.28.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::216:3eff:fe24:b308/64 scope link valid_lft forever preferred_lft forever

Find your public or private IP address by running the ip command

Update /etc/hosts file with that IP address such as 10.105.28.43 using a text editor such as vim command/nano command:

$ sudo vi /etc/hosts

OR

$ sudo nano /etc/hosts

Update/append:

10.105.28.43 ubuntu-2004-nixcraft

Make sure you replace all occurrence of the old computer name with your new one too. Save and close the file in vim.

Conclusion

You learned how to change hostname permanently when using Ubuntu 20.04 LTS server or desktop computer as per your project needs.

