When you connect to a Linux server via SSH, the terminal prompt usually displays your username followed by an “@” symbol and the name of the server (e.g., admin@web-server-01:~$). This server name is called the “hostname.” If you manage a large fleet of virtual machines, leaving the default hostname (like ubuntu or a string of random characters provided by your cloud host) makes it incredibly difficult to know which server you are currently logged into, increasing the risk of running destructive commands on the wrong machine. You can easily view and change this system identifier using the Linux hostname command.
Viewing the Current Hostname
Before making any changes, you should verify the current identity of the machine.
- Open your terminal or SSH session.
- Type the command:
hostname - Press Enter.
The terminal will print a single string of text (e.g., database-primary), representing the short name of the machine. If you want to see the fully qualified domain name (FQDN), which includes the network domain (e.g., database-primary.internal.company.com), you can run hostname -f.
Changing the Hostname Temporarily
You can change the hostname of a running server instantly using the same command. However, this method only changes the hostname in the kernel’s volatile memory. If the server reboots, the change will be lost, and the machine will revert to its old name.
Because changing a system’s identity affects network routing, you must run this command with root privileges (sudo).
sudo hostname new-server-name
If you type hostname again immediately after, the output will reflect the new name. Note that your actual bash prompt (the text on the left side of your blinking cursor) may not update to show the new name until you log out and log back into your SSH session.
Changing the Hostname Permanently (systemd)
Modern Linux distributions (like Ubuntu, Debian, CentOS, and RHEL) use systemd to manage system configurations. To permanently change the hostname so that it survives a full system reboot, you should not use the basic hostname command. Instead, you must use the hostnamectl tool provided by systemd.
- Open your terminal.
- Type the following command, replacing the final word with your desired name:
sudo hostnamectl set-hostname web-server-eu-west - Press Enter.
This command automatically updates the kernel memory and rewrites the static configuration file located at /etc/hostname on your hard drive, ensuring the change is entirely permanent.
Important Follow-Up: Update /etc/hosts
After permanently changing a hostname, it is a critical best practice to update your server’s local DNS resolution file. If you skip this step, certain network services (like Apache or sudo itself) might throw warnings or experience severe lag as they try to look up the old, non-existent name.
- Open the hosts file in a text editor with root privileges:
sudo nano /etc/hosts - Look at the top two lines. You will see an entry mapping
127.0.0.1or127.0.1.1to your old hostname. - Delete the old name and type your new hostname in its place.
- Save the file and exit (In Nano: press
Ctrl + O,Enter, thenCtrl + X).