Changing the hostname of a Linux machine is often one of the first tasks a system administrator or user performs after a fresh installation. Whether you are setting up a development environment, configuring a server for the cloud, or simply trying to identify a device on your network, giving your machine a distinct name is essential for organization and remote management.

Understanding Hostnames in the Linux Ecosystem

The hostname is a human-readable label assigned to a device on a network. In Linux, this name is stored in several locations and managed by different components, which can sometimes lead to confusion. To successfully change the computer name, it is crucial to understand the distinction between the static, pretty, and transient hostnames.
The Three Types of Hostnames

Modern Linux distributions, particularly those using systemd, utilize a layered approach to naming. The static hostname is the traditional name stored in the /etc/hostname file and persists across reboots. The pretty hostname is a free-form UTF-8 string used for display purposes, such as in graphical interfaces. Finally, the transient hostname is a temporary name assigned by the kernel or network protocols like DHCP; changes here are lost upon restart.
Changing the Hostname via Command Line

For immediate changes that take effect without rebooting, the hostnamectl command is the standard tool. This utility interacts directly with systemd, ensuring that all layers of the hostname are updated correctly depending on the flags used.
Applying the New Name Immediately
To change the computer name on the fly, you can use the following command, replacing new_name with your desired identifier:

sudo hostnamectl set-hostname new_name
This command updates the static hostname file. If you wish to update the pretty name for cosmetic purposes, you can add the --pretty flag. To manage all three hostname types simultaneously, the set-hostname command with the --transient flag can be used, though this is less common for standard configuration.
Manual Configuration for Legacy Systems

While hostnamectl is the modern standard, some older distributions or specific configurations might require manual edits to configuration files. This method involves directly modifying the files that the system reads during the boot process or login sequence.
Editing Core System Files


















To change the computer name the traditional way, you need to edit two primary files using a text editor like Nano or Vim. First, update the /etc/hostname file, which contains the static name. Second, adjust the /etc/hosts file, where the machine often maps its own IP address to the old name. Failing to update the hosts file can lead to confusion where the system looks up the wrong address for localhost.
Verifying the Changes
After applying the new name, whether through hostnamectl or manual editing, it is good practice to verify that the system recognizes the update. You can check the current status of the hostname by running the hostnamectl command without arguments, which provides a summary of the static, pretty, and transient states.
Testing Network Resolution
To ensure the change is fully effective on the network, use the ping command directed at the new hostname. This confirms that the name resolves correctly to the machine's IP address and that other devices on the network can identify the server using the new label.
Permanent Considerations Across Reboots
One of the primary goals of changing the Linux computer name is persistence. Unlike dynamic changes made in the terminal session, modifying the hostname files ensures that the system retains the identity after a power cycle. The /etc/hostname file is read early during the boot process by systemd or the SysVinit scripts, making it the source of truth for the machine's identity long-term.