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
- Run the following command:
sudo hostnamectl set-hostname new-computer-name-here - Another option is to type the following command to edit /etc/hostname using nano text editor:
sudo nano /etc/hostname - Remove the old name and setup new name.
- Edit the /etc/hosts file:
sudo nano /etc/hosts - Replace any occurrence of the existing computer name with your new one.
- 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
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
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.
🐧 0 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |