How Can I Change Hostname on My Linux Server Ubuntu OS ?
When managing Ubuntu servers, setting a clear and accurate hostname helps identify your machine on a network and simplifies administration. Whether you're preparing a staging environment or deploying a production node, changing the hostname is a quick and essential task.
This guide walks you through how to view and change the hostname on an Ubuntu Server (16.04 and above).
Step 1: Check the Current Hostname
Run the following command to see your current hostname:
hostname
For more detailed information:
hostnamectl
Example output:
Static hostname: ubuntu-server
Pretty hostname: Ubuntu Server
Step 2: Change the Hostname (Ubuntu 16.04+ with systemd)
Ubuntu uses hostnamectl
, a systemd tool that simplifies hostname changes.
Run the command:
sudo hostnamectl set-hostname your-new-hostname
Replace your-new-hostname
with the name you want (e.g. web-node-01
).
Example:
sudo hostnamectl set-hostname web-node-01
Step 3: Update /etc/hosts File
To avoid issues with local resolution (e.g. sudo delays or errors), update your /etc/hosts
file:
- Open the file with a text editor:
sudo nano /etc/hosts
- Look for a line like:
127.0.1.1 old-hostname
- Change
old-hostname
to your new one:
127.0.1.1 web-node-01
Save and exit (Ctrl + O
, Enter
, Ctrl + X
).
Step 4: Verify the Change
You can verify that your hostname has changed with:
hostname
hostnamectl
Expected output:
Static hostname: web-node-01
You may also see the new hostname in your shell prompt (after re-logging or opening a new terminal).
(Optional) Reboot
Though not required, rebooting ensures that all services reflect the new hostname:
sudo reboot
- Hostnames should contain only letters, numbers, and hyphens, and must not begin or end with a hyphen.
- Avoid using spaces or special characters.
- In cloud environments (AWS, Azure, etc.), hostnames may revert after reboot unless configured in cloud-init or provider settings.
Example Use Cases
Hostname | Purpose |
---|---|
| Primary database |
| First web server |
| Development server |
Conclusion
Changing the hostname on Ubuntu is fast, simple, and requires no advanced setup. Using hostnamectl
, you can update your server's identity in seconds and ensure consistency across your infrastructure.
For automated deployments, you can even include hostname configuration in your cloud-init scripts or provisioning tools like Ansible.