Introduction
A hostname is a unique identifier assigned to a machine on a network. In Linux OS, setting a static hostname ensures consistency, making it easier to manage and identify servers or workstations. This article explains different methods to assign a static hostname on Linux, covering both temporary and permanent changes across various distributions.
Checking the Current Hostname
Before changing the hostname, check the current one using:
hostnamectl
Or simply:
hostname
This will display the existing hostname of your Linux machine.
Method 1: Using hostnamectl (Recommended for Systemd-based Systems)
Most modern Linux distributions (Ubuntu, Debian, CentOS, Fedora) use systemd, which provides the hostnamectl command for managing hostnames.
Changing the Hostname
- Set the static hostname:
sudo hostnamectl set-hostname my-static-hostname
- Verify the change:
hostnamectl
The change is immediate and persists across reboots.
Method 2: Manually Editing /etc/hostname
For distributions that do not use hostnamectl, modify the /etc/hostname file directly.
- Open the file in a text editor:
sudo nano /etc/hostname
- Replace the existing name with your desired hostname.
- Save the file and exit (CTRL + X → Y → ENTER).
- Apply the change:
sudo systemctl restart systemd-hostnamed
Or reboot the system:
sudo reboot
Method 3: Updating /etc/hosts
After changing the hostname, update /etc/hosts to reflect the new name.
- Edit the file:
sudo nano /etc/hosts
- Modify the line containing the old hostname:
127.0.0.1 my-static-hostname
- Save and exit.
This prevents networking issues and ensures proper hostname resolution.


