Setting a unique and descriptive identity for your server is one of the first steps in effective system administration. You might find yourself in a scenario where you have just launched a generic cloud instance and need to distinguish it from others in your cluster, or perhaps you are resolving a “chicken and egg” problem during a domain join where the system must identify itself as a Fully Qualified Domain Name (FQDN) to be recognized by network services. Without a correctly configured hostname, automated scripts and monitoring tools may fail to identify your machine, leading to confusion in logs and network reports.
On Ubuntu, the hostname is more than just a label; it is a critical system identifier used by various network protocols and internal services. Whether you are performing a fresh installation or managing an active production server, knowing how to modify this identifier safely is essential for maintaining Linux server security. This guide explains the modern way to update your machine’s identity using the hostnamectl utility and the manual steps required to ensure your changes are persistent across reboots.
Table of Contents
Key Takeaways: Mastering Ubuntu Hostnames
- hostnamectl → The primary command-line tool used to view and modify the system hostname and related settings.
- Static Hostname → The traditional hostname stored in the /etc/hostname file, which is used at boot.
- FQDN (Fully Qualified Domain Name) → A complete domain name for a specific computer, or host, on the internet (e.g.,
n1.example.internal). - Sudo Privileges → Administrative rights are required to change system identifiers; ensure you are understanding the Linux sudo command before proceeding.
- System Persistence → Changes made via modern tools are designed to survive reboots, but manually updating configuration files ensures absolute consistency.
Method 1: Using hostnamectl (Recommended)
The hostnamectl command is the standard way to manage hostnames on modern Ubuntu systems. It handles the update at runtime and ensures the underlying configuration files are modified correctly.
1. Check Current Status Before making changes, verify your current identity and system information: hostnamectl status
2. Set a New Hostname To change your hostname to a new value (e.g., web-server-01), use the following syntax: sudo hostnamectl set-hostname web-server-01
3. Verify the Change Confirm the update was successful by running the command again or checking the short version: hostname -s
Expected Output: web-server-01
Method 2: Manually Editing Configuration Files
In some advanced scenarios or older releases, you may need to manually edit files to ensure the Ubuntu network configuration recognizes the new name immediately.
1. Edit /etc/hostname This file contains only the short name of the machine. Use a text editor like vi to replace the old name with the new one.
2. Update /etc/hosts To prevent internal resolution errors, you must also map your new name to the local loopback address: 127.0.0.1 localhost 127.0.1.1 web-server-01
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
Step-by-Step Process: Setting an FQDN
If you are integrating your system into a corporate network or setting up a Kerberos realm, follow these steps to set a Fully Qualified Domain Name:
- Open your terminal and verify you have administrative access.
- Define the full name: Run
sudo hostnamectl set-hostname n1.example.internal. - Update the hosts file: Ensure the FQDN is listed before the short name in
/etc/hoststo assist with reverse lookups. - Restart the naming service: Modern tools do this automatically, but you can run
sudo systemctl restart systemd-hostnamedto be sure. - Test the resolution: Run
hostname -fto ensure the system returns the full domain name. - Reboot if necessary: While runtime changes are immediate, a reboot verifies that the settings are truly persistent.
Summary Tables
| Command | Purpose | Expected Result |
|---|---|---|
hostnamectl | Shows current hostname and OS details | Detailed system summary |
hostname -f | Returns the Fully Qualified Domain Name | server.example.com |
hostname -s | Returns the short hostname | server |
hostname -I | Shows the IP addresses of the host | 192.168.1.10 |
| File Path | Role in Naming | Persistence |
|---|---|---|
| /etc/hostname | Stores the static name used at boot | Permanent |
| /etc/hosts | Maps names to IP addresses locally | Permanent |
| /etc/resolv.conf | Points to nameservers for DNS | Session/Managed |
FAQs
Do I need to reboot after changing the hostname? Not necessarily. The hostnamectl tool updates the kernel’s hostname immediately. However, active shell users must re-login, and some long-running services may need a restart to recognize the new name.
What is the difference between a “Static” and “Pretty” hostname? A static hostname is the standard, no-spaces name used by the system (e.g., ubuntu-server). A pretty hostname is a high-level descriptive name for humans, which can include spaces and special characters (e.g., Willie's Production Node).
How do I find my IP address after changing the name? You can use the command hostname -I or follow a specialized guide to check your IP address in Ubuntu.


