Do you need to change the hostname on Ubuntu 20.04 LTS Linux system permanently?
This guide will help you.
Changing the hostname of a new or old server is very essential to make the server function properly.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to solve Ubuntu related tasks.
In this context, we shall look into the steps to change hostname on Ubuntu Linux system.
To modify the Ubuntu computer or host name, you must log in as the root user. The best way to do this is to use the sudo command.
Follow the steps below to implement this change;
i. Start by logging into your Server with an ssh tool as the root user and the execute the following command;
sudo hostnamectl set-hostname your-new-computer-name-here
ii. Then, edit the hostname file at "/etc/hostname" using the nano command as shown below;
sudo nano /etc/hostname
iii. Next, change the old name with your preferred hostname and save the file and exit.
iv. Also modify the hosts file "/etc/hosts" by running the command;
sudo nano /etc/hosts.
In this file you can assign the Server IP address to the hostname on the same line. After this save the file and exit.
v. Finally, reboot the system to effect the changes made. Do this with the following command;
sudo reboot
For Ubuntu Linux 20.04 LTS server or desktop, you can simply use the hostnamectl command to change the hostname.
To see current setting just run the following command:
hostnamectl
Lets us say we want to change the hostname to user.hostname.com, you can do this by executing the command;
sudo hostnamectl set-hostname user.hostname.com
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.
What this means is that, you can do this by running any of the following command;
hostname
hostnamectl
It is important to modify the /etc/hosts file to implement hostname changes.
This file is simply a text file which is associated to the server IP addresses with hostnames, on a single line format.
To display the current settings, simply execute the cat command below;
cat /etc/hosts
You will see and output such as this;
127.0.0.1 localhost
your_ip_address localhost
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Now, change the old hostname (such as localhost) to the new one (user.hostname.com) by running:
sudo vi /etc/hosts
Then, Find:
your_ip_address localhost
Change To:
your_ip_address user.hostname.com
Now the complete contents of the hosts file will be;
127.0.0.1 localhost
your_ip_address user.hostname.com
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
After this, save the file and exit the text editor.
It is recommended to reboot the system but this is optional.
To reboot the system, simply execute the following reboot command/shutdown command/systemctl command:
sudo systemctl reboot
## OR ##
sudo reboot
## OR ##
sudo shutdown -r now
This article will guide you on how to change your Ubuntu server hostname without restarting the system.