How To Change Hostname In Linux : System Name Change Commands

Every Linux system needs a unique hostname to identify itself on a network. If you are wondering how to change hostname in Linux, you have come to the right place. This guide walks you through every method, from simple commands to permanent configuration changes.

Your hostname is like your computer’s name on the local network. It helps other devices find yours, and it shows up in your terminal prompt. Changing it is a common task for sysadmins and home users alike.

We will cover temporary changes, permanent changes, and even how to set a pretty hostname. By the end, you will be able to rename any Linux machine with confidence.

How To Change Hostname In Linux

Before we jump into the steps, let’s understand what a hostname actually is. In Linux, the hostname is stored in the kernel and can be viewed with the hostname command. There are three types: static, pretty, and transient. The static hostname is the one stored in /etc/hostname, while the transient one is set by the system at boot.

Most of the time, you want to change the static hostname so it survives reboots. We will show you both the old-school method and the modern hostnamectl way.

Check Your Current Hostname

First, see what your system is currently calling itself. Open a terminal and type:

hostname

This prints the current hostname. You can also use:

hostnamectl

The hostnamectl command gives you more details, including the static, pretty, and transient hostnames. It also shows your OS version and kernel info.

Temporary Change Using The Hostname Command

If you only need a temporary change until the next reboot, use the hostname command. This is great for testing or quick fixes.

  1. Open your terminal.
  2. Run: sudo hostname new-hostname
  3. Replace new-hostname with your desired name.
  4. Verify with: hostname

This change takes effect immediately but will be lost after a reboot. It does not modify any configuration files.

Permanent Change Using Hostnamectl

For a permanent change, the hostnamectl command is the modern and recommended way. It works on systems using systemd, which includes most modern distributions like Ubuntu, Fedora, and Debian.

  1. Open a terminal.
  2. Run: sudo hostnamectl set-hostname new-hostname
  3. Replace new-hostname with your chosen name.
  4. Verify with: hostnamectl

This command updates both the static hostname in /etc/hostname and the transient hostname immediately. It also updates the pretty hostname if you specify it.

Set A Pretty Hostname

A pretty hostname is a user-friendly name that can include spaces and special characters. It is not used for network identification but shows up in some system tools.

sudo hostnamectl set-hostname "My Awesome Server" --pretty

You can check it with:

hostnamectl --pretty

Edit The Hostname File Manually

If you prefer the old-school method, you can edit the /etc/hostname file directly. This is still valid on many systems.

  1. Open the file with a text editor: sudo nano /etc/hostname
  2. Replace the existing name with your new hostname.
  3. Save and exit.
  4. Run: sudo hostname -F /etc/hostname to apply it immediately.

This method is simple but requires you to also update the /etc/hosts file to avoid issues.

Update The Hosts File

After changing your hostname, you must update the /etc/hosts file. This file maps hostnames to IP addresses and is used by many network services.

  1. Open /etc/hosts: sudo nano /etc/hosts
  2. Find the line that starts with 127.0.1.1 or 127.0.0.1.
  3. Replace the old hostname with your new one.
  4. Save and exit.

For example, a typical line looks like:

127.0.1.1   old-hostname

Change it to:

127.0.1.1   new-hostname

Failure to update this file can cause slow sudo commands and other network issues.

Change Hostname On Ubuntu And Debian

Ubuntu and Debian systems follow the same steps as above. The hostnamectl command works perfectly. However, some older versions may require editing /etc/hostname and /etc/hosts manually.

On Ubuntu, you can also use the hostname command for a temporary change. For a permanent change, always use hostnamectl or the manual file edit.

Change Hostname On CentOS And RHEL

CentOS, RHEL, and Fedora also use systemd, so hostnamectl is the best tool. The steps are identical to the ones above.

  1. Run: sudo hostnamectl set-hostname new-hostname
  2. Update /etc/hosts as shown earlier.
  3. Reboot or restart networking: sudo systemctl restart network

On older CentOS 6 systems, you would edit /etc/sysconfig/network and run hostname command. But for modern versions, stick with hostnamectl.

Change Hostname On Arch Linux

Arch Linux users can also use hostnamectl. The process is the same as other systemd-based distros.

sudo hostnamectl set-hostname new-hostname

Then update /etc/hosts manually. Arch does not have a default hosts file, so you may need to create one if it does not exist.

Verify The Change

After making changes, always verify that everything worked. Use these commands:

  • hostname – shows the current hostname.
  • hostnamectl – shows static, pretty, and transient names.
  • cat /etc/hostname – shows the static hostname file.
  • cat /etc/hosts – check for correct mapping.

If you see the old name anywhere, double-check your edits.

Common Mistakes And Troubleshooting

Here are some issues you might face and how to fix them.

Hostname Not Changing After Reboot

This usually means you only used the hostname command without editing the configuration file. Use hostnamectl or edit /etc/hostname to make it permanent.

Slow Sudo Commands

If sudo becomes slow after changing the hostname, you likely forgot to update /etc/hosts. The system is trying to resolve the old hostname. Fix the hosts file and the problem goes away.

Hostname Contains Invalid Characters

Hostnames can only contain letters, numbers, and hyphens. No spaces, underscores, or special characters. If you use invalid characters, the system may reject them or cause errors.

Permission Denied

Changing the hostname requires root privileges. Always use sudo before the commands. If you get permission errors, ensure you have sudo access.

Using The Sysctl Method

There is also a way to change the hostname using sysctl, though it is less common. This method modifies kernel parameters directly.

sudo sysctl kernel.hostname=new-hostname

This changes the hostname immediately but is temporary. It is useful for scripting or when you cannot use hostnamectl.

Change Hostname With A Script

If you manage many servers, you can automate the hostname change with a simple bash script. Here is an example:

#!/bin/bash
NEW_HOSTNAME="server-01"
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 this as change-hostname.sh, make it executable with chmod +x, and run it with sudo.

Understanding The Hostname Types

Linux supports three types of hostnames. Knowing them helps you choose the right method.

  • Static hostname: Stored in /etc/hostname, used at boot.
  • Transient hostname: Set by the kernel, can be changed dynamically.
  • Pretty hostname: A descriptive name, not used for network identification.

When you use hostnamectl set-hostname, it updates all three unless you specify otherwise.

Network Hostname Vs Local Hostname

Your local hostname is what you set on the machine. But on a network, other devices may see a different name if DNS or DHCP assigns one. For full control, configure your router or DNS server to match your new hostname.

If you use DHCP, your router may override the hostname. You can usually disable this in your router settings or configure your Linux machine to send its hostname via DHCP.

Changing The Hostname In A Container Or VM

In Docker containers or virtual machines, changing the hostname is similar but may have limitations. For containers, you can set the hostname at runtime with docker run --hostname. Inside the container, use hostnamectl if systemd is available.

For VMs, the hostname change is the same as a physical machine. Just ensure the VM has network access and update /etc/hosts as needed.

Best Practices For Naming Hostnames

Choose a hostname that is descriptive and follows a convention. Here are some tips:

  • Use lowercase letters and hyphens.
  • Avoid underscores and spaces.
  • Include a role or location, like web-01 or london-db.
  • Keep it short but meaningful.
  • Do not use the same hostname on multiple machines on the same network.

Testing The Hostname Change

After you change the hostname, test it by pinging your machine from another device. Use the new hostname to see if it resolves correctly.

ping new-hostname

If it fails, check your DNS or hosts file. You can also use nslookup or dig to debug.

Reverting A Hostname Change

If you made a mistake, reverting is easy. Just run the same commands with the old hostname. For example:

sudo hostnamectl set-hostname old-hostname

Then update /etc/hosts back to the original. Reboot if needed.

Frequently Asked Questions

What is the command to change hostname in Linux?

The main command is sudo hostnamectl set-hostname new-hostname. For a temporary change, use sudo hostname new-hostname.

Do I need to reboot after changing the hostname?

No, the change takes effect immediately. However, some services like email or logging may require a restart to pick up the new name. A reboot is not necessary but can be done for consistency.

Why is my hostname not changing permanently?

You likely used only the hostname command without editing the configuration file. Use hostnamectl or edit /etc/hostname and /etc/hosts to make it permanent.

Can I change the hostname without sudo?

No, changing the hostname requires root privileges. You must use sudo or log in as root.

What happens if I set an invalid hostname?

The system may reject it or cause network issues. Stick to letters, numbers, and hyphens. Avoid spaces and special characters.

Conclusion

Changing your hostname in Linux is a straightforward task once you know the right commands. Whether you use hostnamectl for a permanent change or the hostname command for a quick fix, the process is simple. Always remember to update /etc/hosts to avoid problems.

Now you know how to change hostname in Linux on any distribution. Practice on a test machine first if you are unsure. With these steps, you can rename any Linux system quickly and correctly.