Change Hostname on Ubuntu Linux - A Step-by-Step Guide

Changing Your Ubuntu Hostname
During the installation of Ubuntu, you are prompted to select a name for your computer, which is technically referred to as a "hostname." While this hostname can be modified after installation, Ubuntu does not offer a built-in graphical user interface for this purpose.
Modifying the hostname requires utilizing the command line. This method is effective not only in Ubuntu but also in distributions like Linux Mint and other systems based on Debian.
How to Change the Hostname
It’s important to note that other Linux distributions, such as Fedora and those derived from Red Hat, employ different procedures for setting the hostname.
The following steps outline the process for Debian-based systems:
- First, open a terminal window.
- Use the
hostnamectlcommand to view your current hostname. - To set a new hostname, use the command
sudo hostnamectl set-hostname new_hostname, replacingnew_hostnamewith your desired name.
After executing the command, a system reboot is generally recommended to ensure the changes are fully applied across all services and applications.
Alternatively, you can edit the /etc/hostname file and the /etc/hosts file. However, using hostnamectl is the preferred and simpler method.
Remember to choose a hostname that is descriptive and adheres to naming conventions, avoiding spaces and special characters. A well-chosen hostname aids in network identification and management.
Understanding Hostnames
On a network, a hostname serves as a unique identifier for each connected computer, essentially functioning as the machine's name. This name is crucial for identifying the computer within the network infrastructure.
The hostname is prominently displayed in the terminal, providing a quick reference for the system's identity. It's a fundamental aspect of network communication.
While you possess the flexibility to customize your hostname, it’s vital to ensure uniqueness across the network. Duplicate hostnames can lead to connectivity issues and network conflicts.
Selecting a hostname requires adherence to specific rules. Only alphanumeric characters (a-z, 0-9), hyphens (-), and periods (.) are permitted. The name must start and end with either a letter or a number.
Furthermore, hostnames are not case-sensitive; "Server" and "server" are considered identical. Length constraints also apply, with valid hostnames ranging from 2 to 63 characters in length.

Modifying the /etc/hostname File
To begin, launch a terminal window. Within the Ubuntu Unity desktop environment, access the dash by clicking the Ubuntu button. Subsequently, search for Terminal and initiate it with a press of the Enter key.

The hostname, which identifies your system on the network, is stored in the /etc/hostname file. This file is utilized by Ubuntu and other Debian-derived distributions. To edit this file, execute the following command within the terminal, opening it with the graphical text editor, gedit:
sudo gedit /etc/hostname
Alternatively, command-line text editors such as nano or vi can be employed. Use sudo nano /etc/hostname or sudo vi /etc/hostname to open the file. Refer to our guides for assistance with nano or vi if needed.

Related: An Introductory Guide to Text File Editing Using Vi
The /etc/hostname file is remarkably straightforward. It contains a single entry: your computer’s current hostname. To alter this, remove the existing hostname from the file. Then, input your preferred new hostname and save the changes to the text file.

During the boot process, Ubuntu and other Debian-based systems read the contents of the /etc/hostname file. This determines the hostname assigned to your computer. The modification will not be reflected immediately; a system reboot is required, or the hostname command can be used for an instant update.

Modifying the /etc/hosts File
The system's hostname is also recorded within the /etc/hosts file, functioning as a redirection to your local machine – localhost. Therefore, updating the hostname in this file is also necessary.
To edit the /etc/hosts file, utilize a command similar to this:
sudo gedit /etc/hosts
Feel free to substitute 'gedit' with any preferred text editor, such as nano, vi, or another GUI-based editor.
Within the /etc/hosts file, identify the line containing your previous hostname. This line will typically appear as follows:
127.0.1.1 your-old-hostname
Replace the existing hostname with your newly assigned hostname on this line. Subsequently, ensure the /etc/hosts file is saved.
Modifying Your Hostname Instantly
The alteration mentioned previously will be implemented upon system restart. However, the hostname command, executed within a terminal, allows for an immediate hostname change.
It's important to note that this command applies the change only until the next reboot. Therefore, modifying the /etc/hostname file is necessary for a persistent alteration.
Using the Hostname Command
To designate a new hostname, utilize the hostname command in a terminal window. The system will provide feedback if an invalid hostname is entered, allowing for correction.
Execute the command as follows:
sudo hostname your-new-hostname
The hostname of your computer will be updated without delay. However, this change may not be reflected in the current terminal session.
The updated hostname will become visible upon reopening the terminal or initiating a new session.
Finalizing the Process and Alternative Methods
With these steps completed, the hostname modification should be successful. For Linux distributions that are not Debian-based, a different procedure is required.
Consult the documentation specific to your Linux distribution for detailed instructions. Some distributions offer a graphical interface for hostname changes, eliminating the need for terminal commands if preferred.