How to Change Hostname on Ubuntu 20.04

last updated in Categories , ,

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.

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Notable Replies

  1. @CrazyObjects

    Just curious, how is it different from previous way? Has 20.4 added / modified hostname management in any manner? Thanks.

    Not much changed. However, due to systemd based distro it is recommend not to use the hostname command and just stick with the hostnamectl.

Continue the discussion www.nixcraft.com

2 more replies

Participants