When you spin up a new virtual private server (VPS) in the cloud, or install a fresh copy of Ubuntu on a physical machine, the operating system usually assigns it a generic, default “hostname” (like ubuntu or a random string of numbers). If you are managing a network of five different servers, connecting via SSH and seeing root@ubuntu on all five machines is a recipe for disaster; you will inevitably run a destructive command on the wrong server.
The hostname is the fundamental identity of the machine on the network. Changing it to something descriptive (like database-primary or webserver-01) is a mandatory best practice for systems administration.
Method 1: The hostnamectl Command (The Modern Standard)
If you are using a modern Linux distribution that utilizes systemd (which includes almost all current versions of Ubuntu, Debian, CentOS, and RedHat), you should use the official hostnamectl utility. This tool cleanly changes the hostname without requiring a full system reboot.
- Open your terminal and connect to the server via SSH (or open the local terminal if you are on a desktop).
- First, check your current hostname by simply typing:
hostnamectl
The terminal will print a block of system information. Look for the line labeled Static hostname.
- To change the name, use the
set-hostnameflag followed by the new name you want. Because this changes the system identity, you must run it with administrator (root) privileges usingsudo:
sudo hostnamectl set-hostname database-primary
Press Enter. The command will run silently and return you to the prompt. If you type hostnamectl again, you will see the new name is active. (Note: Your terminal prompt might still show the old name until you completely close the terminal window and open a new one to refresh the session).
Method 2: Manually Editing the Configuration Files (The Old School Method)
If you are running an archaic version of Linux that does not support systemd, or if a bug is preventing the hostnamectl command from working, you must manually rewrite the raw configuration files using a text editor.
- Open the primary hostname configuration file using the
nanotext editor:
sudo nano /etc/hostname
- This file should contain exactly one word: your old hostname. Delete it entirely, type your new hostname (e.g.,
database-primary), and save the file (Press Ctrl+O, Enter, then Ctrl+X).
Crucial Step: Updating the Hosts File
If you stop here, your server will eventually break. Many internal Linux processes rely on mapping the local IP address (127.0.0.1) to the hostname. If you change the main hostname but forget to update the internal map, software like Apache or MySQL will crash because they cannot find themselves on the network.
- Open the hosts map file:
sudo nano /etc/hosts
- Look for the line that starts with
127.0.1.1(or sometimes127.0.0.1) followed by your old hostname. - Delete the old hostname and type the exact same new hostname you used in step 2.
- Save the file and exit.
Because you bypassed the modern tools and edited the raw files, you must completely restart the computer for the changes to fully propagate across the network layer. Type sudo reboot to finish the process.