How to Change Hostname on Ubuntu 20.04

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.

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:
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:

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:	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: localhost
To: ln-sg-vpn-001
Here is my complete file:	localhost      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


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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum