Changing the machine name, also known as the hostname, on an Ubuntu system is a common task for system administrators and users who want to personalize their environment or identify devices more clearly on a network.
The hostname is a unique identifier assigned to your computer and is used by various network services and tools.
This comprehensive guide explains multiple ways to change the machine name in Ubuntu, from using command-line tools to manually editing configuration files. Each method is detailed with examples and explanations, ensuring you can choose the approach that best suits your requirements.
What is a Hostname?
The hostname is a label assigned to a device on a network and is used to identify the device in various network communications. It can be a simple word or a fully qualified domain name (FQDN).
Ubuntu, like most Linux distributions, uses the hostname in system prompts and network identification.
Note: Changing the hostname does not affect your system’s IP address or any running services. However, some services might require a restart to recognize the new hostname.
Why Change the Hostname?
- Personalization: To give your machine a custom name that reflects its purpose or owner.
- Network Identification: Easier recognition when managing multiple machines on a network.
- Compliance: Meet organizational or infrastructure naming standards.
- Security: Avoid exposing default hostnames that might reveal information about your system.
Prerequisites
Before proceeding, ensure you have:
- Access to an Ubuntu system (desktop or server).
- Sudo or root privileges to edit system files and run commands.
- A terminal or SSH access to execute commands.
Method 1: Using the hostnamectl Command
The hostnamectl command is part of systemd and is the recommended way to change the hostname in modern Ubuntu systems (16.04 and later). It allows you to easily set different types of hostnames.
Step-by-Step Instructions
- Open a terminal window.
- Check your current hostname by running:
hostnamectl status - Set the new hostname (replace
new-hostnamewith your desired name):sudo hostnamectl set-hostname new-hostname - Verify the change:
hostnamectl status - Optionally, reboot or log out and back in to see the hostname updated in your terminal prompt.
Tip: The hostnamectl command changes the static, transient, and pretty hostnames simultaneously, making it a one-stop solution.
Understanding Different Hostname Types
| Hostname Type | Description | Example |
|---|---|---|
| Static hostname | The traditional hostname stored in /etc/hostname. Persistent across reboots. |
myserver |
| Transient hostname | The hostname assigned by the kernel, often via DHCP or mDNS. Temporary. | myserver-1234 |
| Pretty hostname | A free-form UTF8 hostname for displaying purposes. Can contain spaces and special characters. | My Server |
You can set each hostname type individually using:
sudo hostnamectl set-hostname new-hostname --staticsudo hostnamectl set-hostname "Pretty Hostname" --prettysudo hostnamectl set-hostname new-hostname --transient(usually not recommended manually)
Method 2: Manually Editing System Files
For those who prefer or require manual control, Ubuntu stores hostname information in specific files. Editing these files directly can change the hostname.
Files Involved
| File | Purpose | Location |
|---|---|---|
/etc/hostname |
Stores the static hostname of the machine. | /etc/hostname |
/etc/hosts |
Maps hostnames to IP addresses for local resolution. | /etc/hosts |
Steps to Change Hostname Manually
- Open the
/etc/hostnamefile with a text editor, for example:sudo nano /etc/hostname - Replace the existing hostname with your new hostname.
- Save and exit the editor (in nano, press
Ctrl+O, thenEnter, thenCtrl+X). - Edit the
/etc/hostsfile:sudo nano /etc/hosts - Locate the line starting with
127.0.1.1. It will look similar to:127.0.1.1 old-hostnameReplace
old-hostnamewith your new hostname. - Save and exit the editor.
- Apply the changes immediately without rebooting:
sudo hostname new-hostname - Verify the hostname:
hostname
Warning: If you skip updating /etc/hosts, some network services might fail or behave unexpectedly.
Method 3: Using the hostname Command (Temporary Change)
The hostname command allows you to change the hostname temporarily until the next reboot. This method is useful for testing or temporary network configurations.
How to Use
sudo hostname new-hostname
This changes the hostname immediately but does not persist across reboots. To make the change permanent, update /etc/hostname and /etc/hosts as explained in Method 2.
Verifying the Hostname Change
After changing the hostname, it is important to verify the change to ensure it has been applied correctly.
| Command | Description | Example Output |
|---|---|---|
hostname |
Prints the current hostname. | new-hostname |
hostnamectl status |
Displays detailed hostname information. |
|
cat /etc/hostname |
Shows the static hostname stored in the file. | new-hostname |
cat /etc/hosts |
Check hostname mapping entries. |
127.0.0.1 localhost
|
Common Issues and Troubleshooting
Changing the hostname is usually straightforward, but sometimes you may encounter issues. Below are common problems and how to resolve them.
Hostname Not Updating in Terminal Prompt
Many terminal prompts display the hostname. If the prompt does not update after changing the hostname:
- Try logging out and logging back in.
- Restart the terminal session.
- Reboot the system if necessary.
Network Services Not Recognizing New Hostname
Some services cache the hostname at startup. To fix this:
- Restart the affected services.
- Reboot the system if restarting services is insufficient.
Permission Denied or Command Not Found
If you encounter permission errors, ensure you are using sudo for commands that modify system files or settings. For example:
sudo hostnamectl set-hostname new-hostname
If hostnamectl is not available, your system might be using an older init system. The manual method should work in any case.
Additional Tips
- Use Simple Hostnames: Stick to alphanumeric characters and hyphens to avoid issues with network compatibility.
- Avoid Spaces: Spaces in hostnames can cause problems with scripts and services.
- Consistent Naming: Follow your organization’s naming conventions if applicable.
- Backup Files: Always backup important configuration files before editing:
sudo cp /etc/hostname /etc/hostname.backup
Summary of Commands
| Purpose | Command |
|---|---|
| Check current hostname | hostnamectl status or hostname |
| Set new hostname (permanent) | sudo hostnamectl set-hostname new-hostname |
| Set new hostname (temporary) | sudo hostname new-hostname |
| Edit hostname file | sudo nano /etc/hostname |
| Edit hosts file | sudo nano /etc/hosts |
Conclusion
Changing the machine name in Ubuntu is a straightforward but essential task for system identification and network management. Using hostnamectl is the recommended and easiest method for modern Ubuntu systems, providing flexibility and simplicity.
Alternatively, manual editing of /etc/hostname and /etc/hosts files remains effective and useful in environments where systemd tools are unavailable or unwanted.
Always verify hostname changes and ensure that network services recognize the new name to avoid disruptions. With this guide, you can confidently change and manage your Ubuntu system’s hostname as needed.