A server’s hostname is its unique identifier on a network. Whether you’re setting up a new server, migrating services, or simply prefer a more descriptive name, changing the hostname on your Ubuntu 24.04 system is a straightforward process. A well-chosen hostname can significantly improve server management, making it easier to identify specific machines in a multi-server environment. This Tutorial will guide you through the steps to effectively update your Ubuntu 24.04 server’s hostname.
First, it’s a good practice to check your current hostname. You can do this using the hostname command or hostnamectl.
hostname
or
hostnamectl
The hostnamectl command provides more detailed information about your system’s hostname settings, including the static, transient, and pretty hostnames.
Next, to permanently change the hostname, you need to edit the /etc/hostname file. This file contains the static hostname of your system. Open it with your preferred text editor, such as nano or vi.
sudo nano /etc/hostname
Inside the file, you will see your current hostname. Delete the existing name and enter your new desired hostname. For example, if you want to change it to mywebserver:
mywebserver
Save the file and exit the editor (Ctrl+X, Y, Enter for nano).
After modifying /etc/hostname, it’s crucial to update the /etc/hosts file. This file maps IP addresses to hostnames and is essential for local name resolution. If you don’t update it, some services might still refer to the old hostname.
sudo nano /etc/hosts
Look for lines that include your old hostname, particularly the one associated with 127.0.1.1 or your server’s public IP address. Change all occurrences of the old hostname to your new one.
For instance, if your file looked like this:
127.0.0.1 localhost
127.0.1.1 old-hostname
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Change it to:
127.0.0.1 localhost
127.0.1.1 mywebserver
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Save the file and exit.
To apply the changes immediately without rebooting, you can use the hostnamectl command. This command will set the static hostname and update the system configuration.
sudo hostnamectl set-hostname mywebserver
Replace mywebserver with your chosen hostname. This command updates the kernel hostname and also sets the static hostname in /etc/hostname. While we manually edited /etc/hostname, this command ensures all system components are aware of the change.
Finally, verify that your hostname has been successfully updated by running the hostname or hostnamectl command again.
hostname
or
hostnamectl
You should now see your new hostname reflected in the output. If you encounter any issues, a system reboot will typically resolve them, as it reloads all network configurations. However, hostnamectl is designed to apply changes without requiring a reboot.
Changing your server’s hostname is a fundamental administrative task that helps maintain clarity and organization within your network infrastructure. By following these steps, you can confidently update your Ubuntu 24.04 server’s identity to suit your operational needs.
