This article describes steps to change the hostname of a server running Ubuntu & Centos
Changing hostname of a Ubuntu server
To change the hostname of an Ubuntu server, you can follow these steps:
Log into your server as the root user or a user with sudo privileges.
Open a terminal or SSH session.
Use the hostnamectl
command to change the hostname. Specify the new hostname you want to set as an argument. For example, to change the hostname to "newhostname":
sudo hostnamectl set-hostname newhostname
Note: Replace "newhostname" with the desired hostname you want to set.
Update the /etc/hosts
file to reflect the new hostname. Open the file in a text editor:
sudo nano /etc/hosts
Inside the file, you will see a line that starts with the IP address of your server, followed by the original hostname. Replace the old hostname with the new one. For example:
127.0.0.1 newhostname
Save the changes and exit the text editor.
Restart the system or restart the networking service to apply the changes:
sudo systemctl restart networking
or
sudo reboot
Choose either option depending on your preference and system requirements.
After completing these steps, the hostname of your Ubuntu server will be changed to the new one you specified. You can verify the change by opening a new terminal or SSH session and checking the command prompt or running the hostnamectl
command again:
hostnamectl
The output will display the current hostname of the system.
Changing hostname of a Centos server
The steps to change hostname of a Centos server is pretty much similar to a Ubuntu server. Below are the steps to change the hostname of a Centos server:
Log into your server as the root user or a user with sudo privileges.
Open a terminal or SSH session.
Use the hostnamectl
command to change the hostname. Specify the new hostname you want to set as an argument. For example, to change the hostname to "newhostname":
sudo hostnamectl set-hostname newhostname
Note: Replace "newhostname" with the desired hostname you want to set.
Update the /etc/hosts
file to reflect the new hostname. Open the file in a text editor:
sudo nano /etc/hosts
Inside the file, you will see a line that starts with the IP address of your server, followed by the original hostname. Replace the old hostname with the new one. For example:
127.0.0.1 newhostname
Save the changes and exit the text editor.
Open the /etc/sysconfig/network
file in a text editor:
sudo nano /etc/sysconfig/network
Locate the line that starts with HOSTNAME=
and change the hostname to the new one. For example:
HOSTNAME=newhostname
Restart the system or restart the networking service to apply the changes:
sudo systemctl restart networking
or
sudo reboot
Choose either option depending on your preference and system requirements.
After completing these steps, the hostname of your Ubuntu server will be changed to the new one you specified. You can verify the change by opening a new terminal or SSH session and checking the command prompt or running the hostnamectl
command again:
hostnamectl
The output will display the current hostname of the system.