How to change the hostname on different Linux distributions
Have you ever felt the need to give your Linux machine a new identity? Whether you’re organizing your home network, managing a cluster of servers, or just want a more creative name for your development environment, changing the hostname is an essential skill in the Linux world.
The process can vary slightly between different distribution families, but there’s no need to worry! In this guide, we’ll walk you through, step-by-step, how to change the hostname on three of the most popular distributions: Debian/Ubuntu, Fedora/CentOS/RHEL, and Arch Linux.
First Things First: What is the Hostname?
Simply put, the hostname is the label that identifies your computer on a network. Think of it as your machine’s proper name. There are three main types:
- Static: This is the main, persistent name of the machine, stored in
/etc/hostname. - Transient: A dynamic name that can be assigned by the network (e.g., via DHCP).
- Pretty: A more descriptive, user-friendly name (e.g., “John’s Plex Server”).
We will focus on changing the static hostname, which is the one that persists after a reboot.
The Modern and Universal Method: Using hostnamectl
For most modern systems that use systemd (which is almost all of them nowadays), the hostnamectl tool is the cleanest and most recommended way to do it.
Step 1: Check Your Current Hostname
Open a terminal and run the following command to see the current information:
hostnamectl
You will see detailed output like this:
Static hostname: old-machine
Icon name: computer-desktop
Chassis: desktop
Machine ID: f1d2c3b4a5...
Boot ID: a1b2c3d4e5...
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 5.15.0-78-generic
Architecture: x86-64
Step 2: Set the New Hostname
Now for the magic. Use the following command, replacing "new-server" with the name you want. You will need administrator privileges, which is why we use sudo.
sudo hostnamectl set-hostname "new-server"
And that’s it! The change is applied immediately to both the static and transient hostnames. No reboot is necessary.
The Manual Method (The Old-School Way)
If you prefer the manual method or are on a very old system without systemd, you can edit the configuration files directly.
For Debian / Ubuntu / and derivatives:
- Edit the
/etc/hostnamefile. This file contains only the static hostname.sudo nano /etc/hostname - Edit the
/etc/hostsfile for local mapping. Change the old reference to the new one on the127.0.1.1line.sudo nano /etc/hosts - Reboot your machine for the changes to take full effect.
For Fedora / CentOS / RHEL / Arch Linux:
The process is very similar. The main file to modify is also /etc/hostname. Simply edit it with your new name:
sudo nano /etc/hostname
Although using hostnamectl is also highly recommended on these systems if available, editing this file and rebooting the machine will also work.
Conclusion
As you can see, changing the hostname in Linux is a straightforward task, especially with the help of hostnamectl. This tool unifies the process across multiple distributions, saving us time and potential errors. For the vast majority of cases, it will be your best ally.
I hope this guide has been very useful to you. Do you have any other tricks or questions about Linux system administration? Leave them in the comments!
