In Ubuntu-based servers, the hostname plays a critical role in identification and management across the network. Changing the hostname is necessary to maintain organizational order, especially between multiple servers. This article explains how to change the hostname on an Ubuntu server step by step.
To temporarily change the hostname, which will be effective until the server is restarted, use the following command:
sudo hostname new-hostname
This change will only be valid for the current session, and the hostname will revert to the old one once the server is rebooted.
To make a permanent hostname change, follow these steps:
Update the Hostname File
First, the old hostname in the /etc/hostname file should be removed, and the new hostname should be entered. This file determines the hostname used during system startup.
sudo nano /etc/hostname
Update the Hosts File
Next, the hosts file should be updated with the new hostname. This file contains information about how the server is identified within the local network. Making the necessary changes ensures proper communication with other devices on the network.
sudo nano /etc/hosts
For example, the line should be updated as follows:
127.0.1.1 new-hostname
Change the Hostname Using Hostnamectl
Finally, to permanently change the hostname, the hostnamectl command can be used. This command will update the hostname both temporarily and permanently across the system.
sudo hostnamectl set-hostname new-hostname
To ensure the hostname changes are fully applied, the server needs to be rebooted. This ensures that all system components work correctly with the new hostname.
sudo reboot
After the reboot, the updated hostname can be checked with the following command:
hostnamectl
Changing the hostname on an Ubuntu server can be done easily by following the correct steps. Both temporary and permanent changes can be applied using the appropriate commands and file updates. This process is essential for maintaining management and organization across a network.