Changing your Linux hostname updates the system’s identifier across the network and local sessions. If you’re wondering how to change Linux hostname, you’ve come to the right place. This guide walks you through every method, from simple commands to permanent edits, so you can rename your machine without breaking anything.
Your hostname is what other devices see when they connect to your Linux box. It shows up in terminal prompts, logs, and network broadcasts. A clear, consistent hostname makes administration easier and avoids confusion on multi-machine setups.
What Is A Linux Hostname And Why Change It?
A hostname is a label assigned to a device on a network. It’s like a nickname for your computer. By default, Linux distributions assign a generic name during installation, like “ubuntu” or “localhost”. Changing it helps you identify specific machines, especially in server environments or home labs.
You might need to change it for several reasons:
- You bought a used server with someone else’s hostname
- You’re setting up a cluster and need consistent naming
- You want to personalize your development machine
- You’re following security best practices for network identification
The process is straightforward, but you need to understand the difference between temporary and permanent changes. A temporary change lasts only until reboot. A permanent change survives restarts and network reconnections.
How To Change Linux Hostname Temporarily
This method is great for testing or quick fixes. It doesn’t modify any configuration files, so the change vanishes after you reboot or restart networking.
Using The Hostname Command
The hostname command is the simplest tool. Open a terminal and run:
sudo hostname new-hostname
Replace “new-hostname” with your desired name. No quotes needed. The change takes effect immediately. You can verify it with:
hostname
Your terminal prompt might not update until you open a new session. This is normal. The underlying system now uses the new name.
Using The Sysctl Command
Another temporary method involves the kernel parameter. Run:
sudo sysctl kernel.hostname=new-hostname
This does the same thing as the hostname command. It’s useful if you’re scripting or prefer sysctl syntax. Again, the change is non-persistent.
Both commands require root privileges. If you forget sudo, you’ll get a permission error. That’s your system protecting itself.
How To Change Linux Hostname Permanently
For a lasting change, you need to edit configuration files. The exact file depends on your Linux distribution and init system. Most modern distros use systemd, but some still rely on traditional methods.
Method 1: Using Hostnamectl (Systemd Systems)
If your system uses systemd (Ubuntu 16.04+, CentOS 7+, Debian 8+, Fedora), the hostnamectl command is the recommended way. It updates all necessary files automatically.
Step 1: Check your current hostname
hostnamectl status
This shows the static, transient, and pretty hostnames. Static is the one stored in /etc/hostname. Transient is the kernel’s current value. Pretty is a descriptive name for display.
Step 2: Set the new hostname
sudo hostnamectl set-hostname new-hostname
This changes the static hostname. It also updates the transient hostname if you’re running as root. The command modifies /etc/hostname and /etc/hostname if needed.
Step 3: Verify the change
hostnamectl status
You should see your new name under “Static hostname”. No reboot is required, but you may need to log out and back in for your shell prompt to reflect the change.
Method 2: Editing /Etc/hostname Directly
On systems without systemd, or if you prefer manual control, edit the hostname file. Use any text editor:
sudo nano /etc/hostname
Replace the existing name with your new one. Save and exit. Then update the /etc/hosts file to avoid resolution issues.
Open /etc/hosts:
sudo nano /etc/hosts
Find the line that references your old hostname. It usually looks like:
127.0.1.1 old-hostname
Change it to:
127.0.1.1 new-hostname
Save and exit. This ensures that the loopback interface resolves your hostname correctly. Without this step, some applications might fail to bind to localhost.
Finally, apply the change:
sudo hostname -F /etc/hostname
Or simply reboot. The hostname command reads the file and sets the kernel value.
Method 3: Using The /Etc/sysconfig/network File (Older Systems)
On older Red Hat-based distributions (CentOS 6, RHEL 6), the hostname is stored in /etc/sysconfig/network. Edit it:
sudo nano /etc/sysconfig/network
Find the line HOSTNAME= and change it:
HOSTNAME=new-hostname
Then update /etc/hosts as described above. Run hostname new-hostname to apply immediately, or reboot.
How To Change Linux Hostname On Specific Distributions
While the general methods work everywhere, some distros have quirks. Here’s how to handle the most common ones.
Ubuntu And Debian
Ubuntu uses systemd by default since version 15.04. Use hostnamectl. If you’re on an older version, edit /etc/hostname and /etc/hosts. Don’t forget to also check /etc/cloud/cloud.cfg if you’re on a cloud instance. Cloud-init can override your hostname on reboot.
To prevent cloud-init from resetting it, edit /etc/cloud/cloud.cfg and set preserve_hostname: true.
CentOS, RHEL, And Fedora
CentOS 7+ and Fedora use systemd. Use hostnamectl. For CentOS 6, edit /etc/sysconfig/network and /etc/hosts. Also check /etc/hostname if it exists.
On some versions, the hostnamectl command might not set the pretty hostname. You can do that separately:
sudo hostnamectl set-hostname "My Server" --pretty
The pretty hostname is for display only and doesn’t affect network operations.
Arch Linux
Arch uses systemd. Run hostnamectl set-hostname new-hostname. Then update /etc/hosts to match. Arch’s default /etc/hosts file is minimal, so you might need to add the line manually.
Example /etc/hosts entry:
127.0.0.1 localhost
127.0.1.1 new-hostname
OpenSUSE
OpenSUSE uses systemd. Use hostnamectl. Alternatively, you can use YaST, the graphical configuration tool. Open YaST, go to System > Network Settings, and change the hostname under the Hostname/DNS tab.
Common Issues And Troubleshooting
Changing a hostname usually goes smoothly, but you might hit a few snags. Here’s how to fix them.
Hostname Resets After Reboot
This means your change wasn’t permanent. Check if you edited the correct file. On systemd systems, hostnamectl should handle it. If you edited manually, ensure /etc/hostname contains only the new hostname, with no extra spaces or lines.
Also verify that cloud-init or other management tools aren’t overriding it. Check /etc/cloud/cloud.cfg and any startup scripts.
Hostname Command Shows Old Name
If you used hostnamectl but the hostname command still shows the old name, you might need to log out and back in. The shell prompt caches the old value. Opening a new terminal usually fixes it.
If not, run sudo hostname new-hostname to sync the transient hostname.
Network Services Fail After Change
Some services bind to the hostname. After changing it, restart networking and affected services:
sudo systemctl restart networking
sudo systemctl restart sshd
If you use Docker or other container tools, they might need a restart too. The hostname is often used in container names and network aliases.
Hostname Contains Invalid Characters
Linux hostnames have rules. They can contain letters (a-z, A-Z), digits (0-9), and hyphens (-). They cannot start or end with a hyphen. No spaces, underscores, or special characters. Stick to simple names like “web-server-01” or “dev-box”.
If you use invalid characters, the system might reject them or truncate them silently.
Verifying The Hostname Change
After making the change, confirm it’s working correctly. Use these commands:
hostname– shows the current transient hostnamehostnamectl status– shows all hostname typescat /etc/hostname– shows the static hostname fileping -c 1 $(hostname)– tests if the hostname resolves
If ping fails, your /etc/hosts file might be misconfigured. Double-check the loopback entry.
You can also check the system log for hostname-related messages:
journalctl | grep hostname
This shows if any service complained about the change.
Best Practices For Naming Your Linux Host
Choosing a good hostname saves headaches later. Follow these guidelines:
- Use descriptive names: “db-primary-01” is better than “server1”
- Avoid spaces and underscores: they cause parsing issues in scripts
- Keep it short: long hostnames are hard to type and display
- Use a consistent scheme: for example, [role]-[location]-[number]
- Don’t use “localhost” as your hostname: it breaks network identification
For home networks, simple names like “media-pc” or “laptop” work fine. For production, consider including the environment (dev, staging, prod) in the name.
How To Change Linux Hostname Without Rebooting
All the methods described above take effect immediately. You don’t need to reboot. However, some services might not pick up the change until restarted. If you want to avoid any downtime, plan the change during a maintenance window.
To apply without reboot:
- Use
hostnamectlor edit the files - Run
sudo systemctl restart systemd-hostnamed(if using systemd) - Restart any services that depend on the hostname
- Log out and back in to update your shell
That’s it. Your system now uses the new hostname without interruption.
Automating Hostname Changes With Scripts
If you manage multiple machines, scripting the change saves time. Here’s a simple bash script:
#!/bin/bash
NEW_HOSTNAME="$1"
if [ -z "$NEW_HOSTNAME" ]; then
echo "Usage: $0 new-hostname"
exit 1
fi
sudo hostnamectl set-hostname "$NEW_HOSTNAME"
sudo sed -i "s/127.0.1.1.*/127.0.1.1 $NEW_HOSTNAME/" /etc/hosts
echo "Hostname changed to $NEW_HOSTNAME"
Save it as change-hostname.sh, make it executable with chmod +x, and run it with the desired name.
For older systems without hostnamectl, modify the script to edit /etc/hostname directly.
Frequently Asked Questions
Can I Change The Hostname Without Sudo?
No, changing the hostname requires root privileges. The hostname command and configuration files are protected. You need sudo or a root shell.
Will Changing The Hostname Affect My SSH Connections?
Existing SSH sessions continue to work. New connections will see the new hostname. If you use SSH key authentication, the host key check might warn you about a changed hostname. This is normal; you can update your known_hosts file.
Does The Hostname Change Affect Docker Containers?
Docker containers use their own hostname, separate from the host. Changing the host’s hostname doesn’t affect running containers. However, container names that include the hostname might need updating.
What Is The Difference Between Static And Transient Hostname?
The static hostname is stored in /etc/hostname and persists across reboots. The transient hostname is the kernel’s current value, which can be changed temporarily. On systemd systems, setting the static hostname also updates the transient one.
How Do I Change The Hostname On A Raspberry Pi?
Raspberry Pi OS is based on Debian. Use hostnamectl or edit /etc/hostname and /etc/hosts. The process is identical to other Debian-based systems. Some Raspberry Pi images use cloud-init, so check that file if the change doesn’t stick.
Final Thoughts On Changing Your Linux Hostname
Knowing how to change Linux hostname is a basic but essential skill. Whether you use the quick temporary method or the permanent configuration edit, the process is straightforward. Always update /etc/hosts to avoid resolution problems. Test the change with hostname and ping. And remember to restart any services that rely on the hostname.
With this guide, you can confidently rename any Linux machine, from a tiny Raspberry Pi to a massive production server. The steps are the same, just adapted for your distribution. Keep your hostnames meaningful and consistent, and your network will thank you.
If you run into issues, check the logs and verify your files. Most problems come from forgetting to edit /etc/hosts or from cloud-init overrides. Once you’ve done it a few times, it becomes second nature.
Now go ahead and give your Linux box a name that makes sense. Your future self will apprecate the clarity.