Changing the hostname of an Ubuntu system is a common administrative task that can help personalize a computer or server and better organize networked machines. The hostname is essentially the name assigned to a device in a network—it appears in the terminal prompt, is used in SSH connections, and helps identify the machine to other services and users on the network.
TL;DR
To change the hostname in Ubuntu, one can use the hostnamectl command, edit the /etc/hostname and /etc/hosts files, and reboot the system if necessary. This operation requires root permissions. It’s important to make sure the new hostname is unique and follows naming conventions. The process is quick and can be done entirely from the command line.
1. Understanding Hostnames in Ubuntu
Before diving into the steps, it’s important to understand what a hostname is. A hostname is a label that identifies a machine on a network. This label is used by networking tools and external systems to recognize and communicate with your device.
Ubuntu systems use a static and transient hostname:
- Static hostname: This is the traditional hostname stored in configuration files.
- Transient hostname: This is a dynamic hostname maintained by the kernel, which can be changed at runtime.
2. Prerequisites
Before changing the hostname on an Ubuntu system, ensure the following:
- You have sudo or root access to the system.
- You choose a valid hostname (letters, digits, and hyphens are allowed; no spaces or special characters).
3. Checking the Current Hostname
To see the current hostname, open a terminal and run:
hostnamectl
Alternatively, to see just the existing name, run:
hostname
These commands will return the machine’s current hostname and related information.
4. Changing the Hostname Using hostnamectl
Starting with Ubuntu 16.04 and onwards, the easiest and preferred way to change the hostname is using the hostnamectl command.
To change your hostname, use the syntax:
sudo hostnamectl set-hostname NEW_HOSTNAME
Replace NEW_HOSTNAME with the desired hostname. Example:
sudo hostnamectl set-hostname dev-server
This command updates the system’s static and transient hostnames.
5. Editing /etc/hostname Manually
If you use older versions of Ubuntu or prefer manual configuration, you can also change the hostname by editing the /etc/hostname file directly.
Execute the following command to open the file in a text editor:
sudo nano /etc/hostname
Delete the old hostname and add the new one. Save and close the file by pressing CTRL+X, then Y, and Enter.
6. Updating the /etc/hosts File
After modifying the /etc/hostname file, it is important to update the /etc/hosts file so the system recognizes its own new hostname.
Open the hosts file using a text editor:
sudo nano /etc/hosts
Locate the lines resembling the following:
127.0.1.1 old-hostname
Change old-hostname to your new hostname:
127.0.1.1 new-hostname
Save and exit the file. These changes ensure that the local hostname resolution works correctly.
7. Reboot the System (Optional)
Although the changes made using hostnamectl take effect immediately, a system reboot verifies that the new hostname is consistently used across all services.
To reboot the system, run:
sudo reboot
8. Verifying the Hostname Change
After the reboot, open a terminal and run:
hostnamectl
You should now see the new hostname reflected in the command output. You can also run:
hostname
or look at your shell prompt to see if the change has taken effect.
9. Changing the Hostname Temporarily (Optional)
In some cases, users may only wish to change the hostname temporarily—for example, during testing.
You can temporarily set a hostname using this command:
sudo hostname temporary-name
This change lasts until the next reboot. Keep in mind that services may not honor this change in the same way as a permanent hostname change.
10. Tips and Best Practices
- Hostnames should be unique within a network environment to prevent conflicts.
- Use lowercase letters and hyphens; avoid special characters and spaces.
- Always double-check that /etc/hosts reflects the correct mapping.
- If scripts or services rely on hostname (e.g., SSH keys, web servers), update their configurations accordingly after renaming.
FAQs
Q1: Do I need to reboot after changing the hostname?
A: Not always. If using hostnamectl, the new name takes effect immediately. However, rebooting ensures all services recognize the new name.
Q2: Is it safe to change the hostname on a production server?
A: Yes, but proceed with caution. Make sure no critical services depend explicitly on the current hostname. Always test in a staging environment first.
Q3: What if I only want a temporary change?
A: Use the sudo hostname temporary-name command. This change will revert once the system is rebooted.
Q4: Can I use special characters in a hostname?
A: No. Hostnames should consist only of alphanumeric characters and hyphens. Avoid using spaces or special symbols.
Q5: How do I know if the hostname was successfully updated?
A: You can confirm the change using hostnamectl or simply by checking your shell prompt and the contents of /etc/hostname and /etc/hosts.
Q6: Does the hostname affect my IP address?
A: No, the hostname does not directly change your IP address. However, systems relying on DNS or local host resolution may map names to IPs, so hostname changes may impact internal networking behavior.
Conclusion
Changing the hostname in Ubuntu is a straightforward process, whether done through command-line tools like hostnamectl or by editing system files manually. Proper hostname configuration aids in better system identification and network management.
Following these steps helps ensure that your Ubuntu machine’s identity aligns with its role and makes it easier to manage within a larger network or deployment environment.