This document provides a clear, structured, and professional guide explaining how to change the hostname on Ubuntu. It is written for developers, system administrators, or anyone maintaining Linux systems.
A hostname is the unique identifier assigned to a machine on a network. It appears in the terminal prompt, plays a role in local and remote communication, and is used by several system services. Keeping the hostname consistent and meaningful is good practice for system organization and maintenance.
This guide describes two methods for changing the hostname on Ubuntu:
- the recommended method using
hostnamectl - the manual method using configuration files
Ubuntu systems that use systemd provide the hostnamectl tool, which is the safest and most consistent way to modify the hostname.
Use the following command to inspect the active hostname and related system information:
hostnamectlReplace NEW_HOSTNAME with the desired hostname:
sudo hostnamectl set-hostname NEW_HOSTNAMEThis updates the static, pretty, and transient hostnames automatically.
To ensure proper local name resolution, manually update the hosts file:
sudo nano /etc/hostsLook for a line similar to:
127.0.1.1 current-hostname
Replace it with:
127.0.1.1 NEW_HOSTNAME
Save the file and exit:
- CTRL + O → ENTER
- CTRL + X
A reboot ensures all services load the new hostname:
sudo rebootThis method directly edits configuration files. It works, but requires more caution.
sudo nano /etc/hostnameReplace the existing hostname with the new one.
sudo nano /etc/hostsUpdate the 127.0.1.1 entry to reflect the new hostname.
sudo reboot- Hostnames should contain only lowercase letters, digits, and hyphens.
- Avoid spaces or special characters.
- Always update
/etc/hoststo prevent name resolution issues. - Most system services will automatically pick up the new hostname after reboot.
After the system restarts, verify the hostname:
hostnamectlhostnameBoth commands should return the updated hostname.
- Choose meaningful hostnames when working with multiple servers or virtual machines (e.g.,
data-node01,workstation-ubuntu,analysis-server). - If the machine participates in a domain or uses services like SSH, consider verifying the configuration after the change.
Changing the hostname on Ubuntu is straightforward when using hostnamectl. Updating both hostname and /etc/hosts ensures smooth system behavior and proper resolution. This guide provides a reliable procedure suitable for production environments, educational material, and documentation within a GitHub repository.