When you initially install Ubuntu, the setup wizard asks you to provide a name for your machine. This identifier—technically known as the “hostname”—is how your computer presents itself to the rest of the local network. If you left it as the generic default (like john-desktop) or you have recently repurposed an old laptop into a dedicated media server, having clear, identifiable computer names is critical for smooth networking. Learning how to change the computer name in Ubuntu Linux ensures you never accidentally connect via SSH or transfer files to the wrong machine.
In this core system administration guide, we will explain the limitations of naming your machine, how to change the hostname seamlessly using the GNOME graphical interface, and how to execute the change definitively using the hostnamectl terminal command.
What is a Hostname?
A hostname is a single, unique label assigned to a device connected to a computer network. When you view the list of connected devices on your home router’s configuration page, or when you open the “Network” tab in your file manager to share a folder, the hostname is what you see.
Furthermore, in Ubuntu, the hostname is constantly displayed in the terminal prompt (e.g., username@hostname:~$). Changing it to something descriptive (like ubuntu-plex-server or dev-laptop) vastly improves your workflow when managing multiple machines simultaneously.
Hostname Naming Rules and Restrictions
You cannot use just any combination of characters for a Linux hostname. To ensure compatibility with standard networking protocols, you must adhere to these strict rules:
- Length: The name must be between 2 and 64 characters long.
- Allowed Characters: You may only use lowercase letters (a-z), numbers (0-9), and hyphens (-).
- Prohibited Characters: You absolutely cannot use spaces, underscores, or special characters (like !, @, #, or ?).
- Formatting: The name cannot begin or end with a hyphen.
Method 1: Using the Graphical Interface (Settings)
If you are running the standard Ubuntu Desktop edition (GNOME), you can change the “Device Name” directly through the system settings. This is the fastest method for casual desktop users.
Step 1: Open System Settings
Click on the system menu in the top-right corner of your screen (where the battery and Wi-Fi icons are located) and click the Settings gear icon.
Step 2: Navigate to the About Page
In the left-hand sidebar of the Settings window, scroll down to the very bottom and click on About.
Step 3: Edit the Device Name
At the very top of the About page, you will see a field labelled Device Name, displaying your current hostname. Click anywhere on this text.
- A small “Rename Device” pop-up window will appear.
- Delete the old name and type in your new, compliant hostname.
- Click the green Rename button.
The change is applied immediately to the graphical interface, and any network services broadcasting via Avahi (like file sharing) will begin using the new name. However, open terminal windows will likely still show the old name until you restart them.
Method 2: Using the Terminal (hostnamectl command)
For Ubuntu Server installations without a GUI, or for power users who want to ensure the change is propagated cleanly at the systemd level, the hostnamectl command is the definitive, professional method.
Step 1: Check Your Current Hostname
Open a new terminal window (Ctrl + Alt + T) and run the following command to view your system’s current detailed identity information:
hostnamectl
Look for the line that says “Static hostname” to confirm your current setup.
Step 2: Set the New Hostname
To change the name, you must use the set-hostname argument. Because this alters a core system configuration, you must execute the command with administrator (sudo) privileges. Type the following command (replacing new-name-here with your desired name):
sudo hostnamectl set-hostname new-name-here
Press Enter and type your administrator password when prompted. The command will execute silently.
Step 3: Update the Hosts File (Crucial Step)
While hostnamectl changes the system name, it does not automatically update the local mapping file. Ubuntu uses the /etc/hosts file to map local names to local IP addresses. If you do not update this file, some local software (especially web servers like Apache or database tools) may fail to start because they cannot resolve the new name.
Open the file in the Nano text editor:
sudo nano /etc/hosts
Look at the very top of the file for a line that begins with 127.0.1.1. It will look something like this:
127.0.1.1 old-desktop-name
Carefully delete the old name and replace it with your newly chosen hostname. Leave the IP address untouched.
Save the file by pressing Ctrl + O, Enter, and then exit by pressing Ctrl + X.
Step 4: Verify the Change
Close your current terminal window and open a new one. The prompt on the left side of your cursor should immediately reflect the new hostname. For example, user@old-desktop:~$ will now read user@new-name-here:~$.
Frequently Asked Questions
Do I need to reboot my computer after changing the hostname?
Technically, no. The hostnamectl command applies the change to the running kernel instantly. However, if you are running complex network services or a local web server, a quick reboot (sudo reboot) is highly recommended to ensure all background services restart and pick up the new configuration cleanly.
Can two computers on the same network have the same hostname?
You should absolutely avoid this. If two machines broadcast the same hostname on a local network, it causes severe DNS conflicts and routing errors, making file sharing and remote access incredibly unreliable.
Properly identifying your hardware is the cornerstone of good network management. By learning how to change the computer name in Ubuntu Linux via the hostnamectl command, you guarantee that your machines are always uniquely identifiable, perfectly organised, and ready for secure remote administration.