How To Change Hostname in Ubuntu

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.

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:

  1. Open your terminal and verify you have administrative access.
  2. Define the full name: Run sudo hostnamectl set-hostname n1.example.internal.
  3. Update the hosts file: Ensure the FQDN is listed before the short name in /etc/hosts to assist with reverse lookups.
  4. Restart the naming service: Modern tools do this automatically, but you can run sudo systemctl restart systemd-hostnamed to be sure.
  5. Test the resolution: Run hostname -f to ensure the system returns the full domain name.
  6. Reboot if necessary: While runtime changes are immediate, a reboot verifies that the settings are truly persistent.

Summary Tables

CommandPurposeExpected Result
hostnamectlShows current hostname and OS detailsDetailed system summary
hostname -fReturns the Fully Qualified Domain Nameserver.example.com
hostname -sReturns the short hostnameserver
hostname -IShows the IP addresses of the host192.168.1.10
File PathRole in NamingPersistence
/etc/hostnameStores the static name used at bootPermanent
/etc/hostsMaps names to IP addresses locallyPermanent
/etc/resolv.confPoints to nameservers for DNSSession/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.


Related Posts

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 655

Leave a Reply

Your email address will not be published. Required fields are marked *