What changing your hostname does and why you might need to
Your hostname is the name your Linux computer uses to identify itself on a network. It appears in your terminal prompt, in network listings, and when other machines try to connect to yours. Changing it is straightforward — you edit a configuration file, restart the service that manages hostnames, and the change takes effect immediately.
You might change your hostname because you inherited a server with a generic name like "localhost" or "server1", you're setting up a new machine and want something descriptive, or you're moving a virtual machine and need to avoid naming conflicts on your network. The process differs slightly depending on whether you're using systemd (the modern standard on most distributions) or an older init system, but both are simple enough to do in five minutes.
Key Takeaways
- The hostname lives in /etc/hostname on most modern Linux systems, and you change it by editing that file with a text editor like nano or vi.
- After editing the file, you must run hostnamectl set-hostname (on systemd systems) or restart the hostname service to make the change take effect.
- You should also update /etc/hosts to map your new hostname to 127.0.0.1, or some applications will be slow to start.
- The change takes effect immediately after you restart the service; you do not need to reboot the entire system.
- You can verify the change worked by typing hostname in the terminal — it should print your new name.
Changing the hostname on systemd systems (the modern way)
Most Linux distributions released in the last five years use systemd, which includes a tool called hostnamectl that handles hostname changes for you. This is the easiest route and requires only one command.
Open a terminal and type:
sudo hostnamectl set-hostname your-new-hostname
Replace "your-new-hostname" with whatever you want to call the machine. Use lowercase letters, numbers, and hyphens only — no spaces or underscores. For example: sudo hostnamectl set-hostname web-server-01
The command runs immediately and the change takes effect right away. You do not need to restart anything. To verify it worked, type hostname and press Enter — it should print your new name back to you.
Changing the hostname on older systems (manual file editing)
If you're on an older distribution or one that does not use systemd, you'll edit the hostname file directly. Open a terminal and type:
sudo nano /etc/hostname
This opens the file in the nano text editor. You'll see the current hostname on a single line. Delete it, type your new hostname, and press Ctrl+X, then Y, then Enter to save and exit.
Next, restart the hostname service so the system picks up the change. Type:
sudo systemctl restart systemd-hostnamed
If that command fails (you'll see an error message), your system uses a different init system. Try this instead:
sudo /etc/init.d/hostname restart
Verify the change by typing hostname — it should print your new name.
Updating /etc/hosts to avoid slow application startup
Your system also keeps a file called /etc/hosts that maps hostnames to IP addresses. If you don't update this file to match your new hostname, some applications will pause for several seconds at startup while they wait for a network lookup to time out.
Open the file with:
sudo nano /etc/hosts
Look for a line that starts with 127.0.0.1 and contains your old hostname. It might look like this:
127.0.0.1 localhost old-hostname
Change it to include your new hostname instead:
127.0.0.1 localhost new-hostname
Save and exit (Ctrl+X, Y, Enter). This tells your system that your new hostname points to your local machine, which prevents the startup delay.
What to do if you cannot use sudo
If you don't have sudo access, you'll need the root password. Log in as root with su (type the root password when prompted), then run the hostname commands without the sudo prefix:
hostnamectl set-hostname your-new-hostname
Or edit the files directly as root. When you're done, type exit to return to your regular user account.
Checking your work and troubleshooting
After you change the hostname, verify it took effect by typing hostname in the terminal. It should print your new name immediately. If it still shows the old name, the change did not take effect — go back and check that you saved the file correctly and ran the restart command.
If you're on a network and other machines are trying to reach you by hostname, they may still use the old name for a while because they have cached it. This is normal and resolves on its own within a few minutes to a few hours, depending on the network. You can speed this up by restarting the machines that need to reach you, but it's usually not necessary.
If applications are still slow to start after you changed the hostname, double-check that you updated /etc/hosts correctly. The most common mistake is forgetting to add the new hostname to the 127.0.0.1 line, which leaves the system trying to look it up on the network instead of finding it locally.
Frequently Asked Questions
Do I need to reboot my system after changing the hostname?
No. The hostname change takes effect immediately after you run the command or restart the service. A full system reboot is not necessary and will not speed up the process.
Can I use spaces or special characters in my hostname?
No. Hostnames must contain only lowercase letters, numbers, and hyphens. Spaces, underscores, and other special characters will cause errors on most networks. If you want to separate words, use hyphens instead: web-server-01 instead of web server 01.
What if I change the hostname and then realize I made a typo?
Just run the command again with the correct name. There's no limit to how many times you can change it. Use sudo hostnamectl set-hostname correct-name and verify with hostname.
Will changing the hostname break my network connection or services?
No. The hostname is just a label — it does not affect your IP address or network connectivity. Services will keep running. The only thing that might be slow is applications that try to look up the hostname on startup, which is why updating /etc/hosts is recommended.
How do I check what my current hostname is?
Type hostname in the terminal and press Enter. It will print your current hostname on the next line. You can also type hostnamectl to see more detailed information about your system's hostname settings.