Skip to main content

Command Palette

Search for a command to run...

How to Change Hostname in Linux

Updated
6 min read

Changing the hostname on your Linux system is a common task that you might need to do for various reasons. Whether you want to personalize your computer, organize devices on a network, or fix a naming conflict, knowing how to change the hostname is essential. In this article, I’ll guide you through simple and clear steps to update your Linux hostname safely and correctly.

You’ll see methods that work on most Linux distributions, including Ubuntu, CentOS, Fedora, and Debian. I’ll also explain how to check your current hostname and verify the changes. By the end, you’ll feel confident managing your system’s identity with ease.

What Is a Hostname in Linux?

Your Linux hostname is the name that identifies your computer on a network. It’s like your system’s personal ID card. When you connect to other devices or servers, the hostname helps others recognize your machine.

  • Hostnames can be simple names like myserver or fully qualified domain names (FQDN) like myserver.example.com.
  • They are used in network communications, system logs, and terminal prompts.
  • Changing the hostname can help you organize multiple devices or improve security by avoiding default names.

Understanding what a hostname is will help you appreciate why and when you might want to change it.

How to Check Your Current Hostname

Before changing your hostname, it’s good to know what it currently is. You can check it easily with a few commands.

Open your terminal and type:

hostname

This command prints your current hostname.

Alternatively, you can use:

hostnamectl status

This command shows detailed information, including the static hostname, transient hostname, and pretty hostname.

  • Static hostname: The permanent name set on your system.
  • Transient hostname: A temporary name assigned by DHCP or other services.
  • Pretty hostname: A user-friendly name with spaces or special characters.

Knowing these details helps you understand how your system identifies itself.

Changing Hostname Temporarily

If you want to change your hostname just for the current session (until reboot), you can use the hostname command.

For example:

sudo hostname new-hostname

Replace new-hostname with your desired name.

  • This change is immediate but temporary.
  • After a reboot, the hostname will revert to the original.
  • Useful for testing or short-term changes.

Remember, this method doesn’t update system files, so it’s not permanent.

Changing Hostname Permanently Using hostnamectl

The recommended way to change the hostname permanently on modern Linux systems is by using the hostnamectl command. This tool works on most distributions that use systemd.

Here’s how to do it:

  1. Open your terminal.
  2. Run the command:

    sudo hostnamectl set-hostname new-hostname
    
  3. Replace new-hostname with the name you want.

  4. To verify, type:

    hostnamectl status
    

This method updates the static hostname and ensures the change persists after reboot.

Additional Tips

  • You don’t need to reboot after this change; it applies immediately.
  • Some services might require a restart to recognize the new hostname.
  • Use simple names without spaces or special characters for compatibility.

Editing Hostname Files Manually

On some Linux systems, especially older ones or those without systemd, you might need to edit configuration files directly.

1. Edit /etc/hostname

This file contains the system’s hostname.

  • Open it with a text editor, for example:

    sudo nano /etc/hostname
    
  • Replace the existing name with your new hostname.

  • Save and exit.

2. Edit /etc/hosts

This file maps hostnames to IP addresses. You should update it to avoid network issues.

  • Open /etc/hosts:

    sudo nano /etc/hosts
    
  • Find the line with your old hostname, usually near 127.0.1.1 or 127.0.0.1.

  • Replace the old hostname with the new one.

Example:

127.0.0.1   localhost
127.0.1.1   new-hostname
  • Save and exit.

3. Reboot Your System

After editing these files, reboot your computer:

sudo reboot

This ensures all services recognize the new hostname.

Changing Hostname on Specific Linux Distributions

Different Linux distributions may have slight variations in how hostnames are managed. Here’s a quick guide for popular ones.

Ubuntu and Debian

  • Use hostnamectl for permanent changes.
  • Edit /etc/hostname and /etc/hosts if needed.
  • Reboot or restart the hostname service.

CentOS, Fedora, and RHEL

  • Use hostnamectl as the primary method.
  • For older versions, edit /etc/sysconfig/network and set:

    HOSTNAME=new-hostname
    
  • Update /etc/hosts accordingly.

  • Reboot or restart network services.

Arch Linux

  • Use hostnamectl or edit /etc/hostname.
  • Update /etc/hosts.
  • Reboot or restart the systemd-hostnamed service.

Troubleshooting Hostname Changes

Sometimes, changing the hostname might not work as expected. Here are common issues and fixes:

  • Hostname not updating in terminal prompt: Restart your terminal or log out and back in.
  • Network services not recognizing new hostname: Restart network services or reboot.
  • Permission denied errors: Use sudo to run commands with root privileges.
  • Hostname reverting after reboot: Ensure you edited /etc/hostname or used hostnamectl correctly.

If problems persist, check system logs for errors or consult your distribution’s documentation.

Why Changing Hostname Matters

Changing your Linux hostname is more than just a cosmetic tweak. It can:

  • Help identify devices on a network clearly.
  • Improve security by avoiding default or generic names.
  • Assist in system administration and automation.
  • Prevent conflicts when multiple devices have the same name.

By managing your hostname properly, you keep your system organized and easier to manage.

Summary Table: Hostname Change Methods

MethodDescriptionPersistenceSuitable For
hostname commandTemporary hostname changeNoQuick tests
hostnamectl commandPermanent change via systemdYesModern Linux systems
Editing /etc/hostnameManual file edit for hostnameYesOlder or non-systemd
Editing /etc/hostsUpdate hostname-IP mappingsYesAll systems

Conclusion

Changing the hostname in Linux is a straightforward process once you know the right steps. Whether you want a temporary change or a permanent one, tools like hostnamectl make it easy and reliable. For older systems, manual file edits still work well.

Remember to update both the hostname and hosts files to avoid network issues. After making changes, a reboot or service restart ensures everything runs smoothly. With this knowledge, you can confidently manage your Linux system’s identity and keep your network organized.


FAQs

How do I check my current hostname in Linux?

You can check your hostname by running the command hostname or hostnamectl status in the terminal. These commands show your system’s current name and related details.

Can I change the hostname without rebooting?

Yes, using hostnamectl set-hostname new-hostname changes the hostname immediately without rebooting. However, some services may need a restart to recognize the change.

What is the difference between static and transient hostname?

The static hostname is the permanent name set on your system. The transient hostname is temporary and assigned by network services like DHCP. The static hostname persists across reboots.

Why should I update the /etc/hosts file after changing hostname?

Updating /etc/hosts ensures your system correctly maps the new hostname to the local IP address. This prevents network and service issues related to hostname resolution.

What if my hostname change doesn’t persist after reboot?

If the hostname reverts, make sure you used hostnamectl or edited /etc/hostname correctly. Also, update /etc/hosts and reboot the system to apply changes permanently.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts