Your Linux system’s hostname is its identity on the network and is simple to update. Knowing how to change the hostname in Linux is a basic skill for anyone managing a server or a personal machine. This guide walks you through every method, from quick commands to permanent changes, with clear steps you can follow right now.
A hostname is the label assigned to your computer on a network. It helps other devices identify your system, whether it’s a local home setup or a cloud server. Changing it can fix naming conflicts, improve organization, or reflect a new role for your machine.
You might need to update your hostname after moving a server to a different project. Or maybe you just want a more descriptive name for your Raspberry Pi. Whatever the reason, the process is straightforward once you understand the tools.
In this article, we cover temporary and permanent methods. We also explain how to verify the change and avoid common pitfalls. By the end, you’ll confidently manage hostnames on any Linux distribution.
How To Change The Hostname In Linux
There are several ways to accomplish this task. The method you choose depends on whether you need a temporary fix or a permanent rename. Let’s start with the simplest approach.
Using The Hostname Command
The hostname command is the fastest way to set a new name temporarily. Open a terminal and type:
sudo hostname new-hostname
Replace new-hostname with your desired name. This change takes effect immediately but only lasts until the next reboot. It’s perfect for testing or quick adjustments.
To see the current hostname, run:
hostname
This command displays the active hostname. If you want to check the full FQDN (Fully Qualified Domain Name), use:
hostname -f
Remember, the temporary change doesn’t survive a system restart. For a permanent solution, you need to edit configuration files.
Permanent Change Via /Etc/hostname
Most modern Linux distributions store the hostname in /etc/hostname. Editing this file makes the change persistent across reboots.
First, open the file with a text editor like nano or vim:
sudo nano /etc/hostname
You’ll see the current hostname on a single line. Delete it and type your new name. Save the file and exit.
For example, if the file contains old-server, replace it with new-server. After saving, you must either reboot or run sudo hostname new-server to apply it immediately.
This method works on Ubuntu, Debian, Fedora, and most other distributions. However, some systems like CentOS 7 or RHEL use additional files.
Editing /Etc/sysconfig/network (RHEL/CentOS 7 And Older)
On older Red Hat-based systems, the hostname is also defined in /etc/sysconfig/network. Open this file:
sudo nano /etc/sysconfig/network
Look for a line starting with HOSTNAME=. If it exists, change it to your new name. If not, add this line:
HOSTNAME=new-hostname
Save the file and reboot for the change to take full effect. On newer versions like CentOS 8 or Fedora, this file is deprecated, but it’s still good to check.
Using Hostnamectl (Systemd Systems)
If your Linux distribution uses systemd, the hostnamectl command is the modern way to manage hostnames. It handles both temporary and permanent changes.
To set a new hostname, run:
sudo hostnamectl set-hostname new-hostname
This updates /etc/hostname automatically and applies the change immediately. You don’t need to edit files manually.
To set a pretty hostname (a more descriptive name), use:
sudo hostnamectl set-hostname "My Web Server" --pretty
To verify the current settings, run:
hostnamectl
This command shows the static hostname, pretty hostname, and other system info. It’s a handy tool for troubleshooting.
Updating The /Etc/hosts File
After changing the hostname, you must update the /etc/hosts file. This file maps hostnames to IP addresses and is used for local resolution.
Open the file:
sudo nano /etc/hosts
Look for a line that contains the old hostname. Typically, it looks like:
127.0.1.1 old-hostname
Change old-hostname to your new name. Also check for any other references, like in the 127.0.0.1 line.
For example, if your new hostname is web-server, update the line to:
127.0.1.1 web-server
Failing to update this file can cause slow system responses or errors with commands like sudo. It’s a common oversight.
Verifying The Change
After making the change, confirm it worked. Use these commands:
hostname– shows the current hostnamehostnamectl– displays detailed hostname infocat /etc/hostname– shows the persistent hostname
Also check that the hostname resolves correctly:
ping -c 1 $(hostname)
If the ping fails, double-check your /etc/hosts file. A misconfiguration here is the most common issue.
Changing The Hostname On Cloud Servers
Cloud instances like AWS EC2 or DigitalOcean droplets often have special considerations. The cloud provider may reset the hostname on reboot.
On AWS, you might need to disable DHCP hostname setting. Edit /etc/sysconfig/network-scripts/ifcfg-eth0 (or your interface) and add:
DHCP_HOSTNAME=new-hostname
Then restart the network service. On Ubuntu, you may need to edit /etc/cloud/cloud.cfg and set preserve_hostname: true.
Always test after a reboot to ensure the change sticks. Cloud environments can be tricky.
Common Mistakes And Troubleshooting
Here are frequent errors and how to fix them:
- Hostname reverts after reboot – You didn’t edit
/etc/hostnameor/etc/hostscorrectly. Check both files. - sudo command slow – The hostname in
/etc/hostsdoesn’t match the system hostname. Update it. - Hostname contains invalid characters – Use only letters, numbers, and hyphens. No underscores or spaces.
- Hostname too long – Keep it under 64 characters. FQDNs can be longer but the hostname part should be short.
If you see an error like “hostname: Name or service not known,” it’s likely a hosts file issue. Fix that first.
Using A Script For Bulk Changes
If you manage multiple servers, automate the process with a script. Here’s a simple bash script:
#!/bin/bash
NEW_HOSTNAME="server-$(hostname -s)"
sudo hostnamectl set-hostname $NEW_HOSTNAME
sudo sed -i "s/127.0.1.1.*/127.0.1.1 $NEW_HOSTNAME/" /etc/hosts
This script sets a new hostname based on the current one. You can modify it for your needs. Test it on a single system first.
Understanding Hostname Types
Linux supports three types of hostnames:
- Static – The traditional hostname stored in
/etc/hostname - Pretty – A free-form description, like “My Personal Computer”
- Transient – A temporary name set by the network, like from DHCP
When you use hostnamectl set-hostname, it changes the static hostname by default. The pretty hostname is optional and only used for display.
Network Manager And Hostname
On some systems, NetworkManager can override the hostname. To prevent this, edit /etc/NetworkManager/NetworkManager.conf and add:
[main]
hostname-mode=none
Then restart NetworkManager:
sudo systemctl restart NetworkManager
This ensures your manual hostname stays in place. It’s especially useful on laptops that switch networks.
Hostname And DNS
Changing your hostname locally doesn’t update DNS records. If your server is public, you need to update your DNS provider separately. The hostname is just for local identification.
For internal networks, you might need to update your router’s DHCP settings or local DNS server. Otherwise, other devices may still use the old name.
Step-By-Step Summary
Here’s a quick recap of the process:
- Choose a valid hostname (letters, numbers, hyphens only).
- Use
sudo hostnamectl set-hostname new-name(systemd) or edit/etc/hostname. - Update
/etc/hostswith the new hostname. - Verify with
hostnameandhostnamectl. - Reboot or restart services if needed.
This works on Ubuntu, Debian, Fedora, CentOS, and most others. For older systems, check /etc/sysconfig/network.
Testing Your Changes
After applying the new hostname, test it thoroughly. Open a new terminal session and check the prompt. It should show the new name.
Run a command that uses the hostname, like:
ssh localhost
If it works without errors, your configuration is correct. Also test network services that depend on the hostname.
Hostname Best Practices
Follow these guidelines for consistent naming:
- Use descriptive names like
web01ordb-server. - Avoid special characters except hyphens.
- Keep it short but meaningful.
- Document your naming scheme for team use.
Good hostnames make system management easier. They help you identify roles at a glance.
When To Change The Hostname
Common scenarios include:
- Repurposing a server for a new task
- Standardizing naming across your infrastructure
- Fixing a duplicate hostname conflict
- Moving a system to a different network segment
Always plan the change during a maintenance window if the system is in production. Some services may need restarting.
Hostname And Containers
In Docker containers, changing the hostname is different. Use the --hostname flag when running a container:
docker run --hostname my-container ubuntu
Inside the container, you can also use hostnamectl if it’s available. But container hostnames are usually transient.
For LXC or LXD containers, edit the container’s configuration file or use the lxc command.
Automating With Ansible
If you manage many servers, use Ansible to change hostnames. Here’s a simple playbook:
---
- hosts: all
tasks:
- name: Set hostname
hostname:
name: "{{ new_hostname }}"
- name: Update /etc/hosts
lineinfile:
path: /etc/hosts
regexp: '^127\.0\.1\.1'
line: "127.0.1.1 {{ new_hostname }}"
This playbook changes the hostname on all target servers. It’s efficient for large deployments.
Hostname And Systemd Services
Some systemd services depend on the hostname. After changing it, restart these services:
sudo systemctl restart systemd-logind
sudo systemctl restart systemd-hostnamed
This ensures the new hostname is picked up by all system components. It’s usually not required but can prevent odd behavior.
Checking Logs For Issues
If you encounter problems, check system logs:
journalctl -xe | grep hostname
This shows any errors related to hostname changes. Common issues include permission errors or missing files.
Hostname And Samba
If your Linux system shares files with Windows via Samba, update the NetBIOS name. Edit /etc/samba/smb.conf and set:
netbios name = new-hostname
Then restart Samba:
sudo systemctl restart smbd
This ensures Windows machines see the new name in network browsing.
Hostname And Email Servers
For mail servers, the hostname should match the FQDN. Set it correctly to avoid email delivery issues. Use a valid domain name like mail.example.com.
Test with:
hostname -f
It should return the full domain name. If not, check your DNS and hosts file.
Hostname And Security
A descriptive hostname can leak information. Avoid including sensitive details like “finance” or “production” if security is a concern. Use generic names instead.
Also, ensure that hostname changes are logged for audit purposes. This helps track configuration changes.
Hostname On Embedded Systems
On devices like Raspberry Pi running Raspberry Pi OS, the process is the same. Use hostnamectl or edit /etc/hostname. The Pi’s hostname is used for network discovery.
After changing it, reboot or restart the Avahi daemon:
sudo systemctl restart avahi-daemon
This updates mDNS broadcasts so other devices see the new name.
Hostname And Virtual Machines
For VMs, the hypervisor may set the hostname. In VMware or VirtualBox, you can set it in the VM settings. But the guest OS still needs its own configuration.
Always set the hostname inside the VM for consistency. Cloud-init often handles this automatically on cloud images.
Final Verification Checklist
Before considering the change complete, run through this list:
- [ ]
hostnameshows the new name - [ ]
hostnamectlshows static hostname updated - [ ]
cat /etc/hostnamecontains the new name - [ ]
/etc/hostshas the correct mapping - [ ] No errors in
journalctl - [ ] Reboot test passes
This ensures a smooth transition without surprises.
Conclusion
Changing the hostname in Linux is a simple but important task. Whether you use the quick hostname command or the persistent hostnamectl tool, the steps are clear. Always update /etc/hosts to avoid issues. Test your changes and document them for future reference.
With this guide, you can confidently rename any Linux system. The process works across distributions and environments. Now you know how to change the hostname in