How to Change Hostname in Ubuntu

The Network Identity Vector

When you deploy a new Ubuntu server via a cloud provider, the system often auto-generates a highly chaotic hostname (e.g., `ip-172-31-44-98`). This alphanumeric string dictates how the server identifies itself on the local intranet and in the master system logs. Managing a cluster of ten servers with these identical, auto-generated names is mathematically inefficient and prone to severe routing errors. You must intercept the core system configuration files and inject a clean, highly specific identifier (e.g., `db-cluster-alpha`).

How to Change Hostname in Ubuntu

Modern Ubuntu environments (running systemd) utilize a dedicated utility called `hostnamectl` to manage this identity, completely bypassing the legacy method of manually editing the `/etc/hostname` file with a text editor.

1. Open your terminal application or connect to the server via SSH.
2. The Prerequisite Interrogation (Verify Current Identity):
* Before making changes, verify the current state of the machine.
* Type exactly:
hostnamectl
* Press Enter.
* The terminal will output a diagnostic block. Look at the very first line: Static hostname. This is the target you are going to overwrite.
3. The Cryptographic Injection (`set-hostname`):
* Because the hostname is a core system parameter, modifying it requires root administrative privileges.
* Assume you want to rename the server to `web-node-01`.
* Type exactly:
sudo hostnamectl set-hostname web-node-01
* Press Enter and supply your cryptographic password.
* Crucial Syntax Rule: Hostnames must be strictly lowercase, cannot contain spaces, and cannot contain special characters (except hyphens).
4. The `hosts` File Verification (Mandatory Step):
* While `hostnamectl` updates the primary identifier, it does not update the local DNS loopback file. If you skip this step, certain applications (like Apache or sudo) will throw DNS resolution errors.
* Type: sudo nano /etc/hosts
* Look at the second line (it usually starts with `127.0.1.1`). It will still have the old, chaotic hostname.
* Use your arrow keys, delete the old name, and type `web-node-01`.
* Press Ctrl+O (to save), Enter (to confirm the file name), and Ctrl+X (to exit nano).
5. The Finalization Protocol:
* The changes are live immediately. However, your current SSH terminal prompt (e.g., `user@ip-172…`) will not update until you sever the connection and log back in. Type `exit`, then SSH back into the server to see the new identity applied.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.