When you install Ubuntu, the system prompts you to choose a name for your computer. This name, known as the hostname, is what identifies your machine on a local network and what appears in your terminal prompt (e.g., user@my-desktop).
If you made a typo during installation, or if you are repurposing an old server for a new project, you do not need to reinstall the operating system to change its name. You can rename your Ubuntu PC instantly using the hostnamectl command.
Checking Your Current Hostname
Before making any changes, you should confirm exactly what the system is currently named.
- Open your terminal application (press Ctrl + Alt + T).
- Type the following command and press Enter:
hostnamectl - The terminal will output a block of system information. Look for the first line, labelled Static hostname. This is your computer’s current name.
How to Change the Hostname
To change the name, you will use the same hostnamectl command, but you must run it with administrator privileges using sudo.
The syntax for changing the name is:
sudo hostnamectl set-hostname new-name-here
Important Rules for Naming: Hostnames must conform to strict networking rules. They can only contain lowercase letters (a-z), numbers (0-9), and hyphens (-). You cannot use spaces, underscores, or special characters. A valid example would be office-server-01 or peters-laptop.
For example, to rename your computer to “database-node”, you would run:
sudo hostnamectl set-hostname database-node
After pressing Enter, you will be prompted for your password. Type it carefully (no characters will appear on the screen) and press Enter. The command will execute silently without returning any confirmation message.
Updating the Hosts File
While hostnamectl successfully changed the system’s official name, you must complete one more manual step to prevent internal networking errors. Ubuntu maps your computer’s hostname to the local loopback IP address (127.0.1.1) in a configuration file called /etc/hosts.
If you do not update this file to match your new name, some applications may struggle to connect to internal services, and commands using sudo may take significantly longer to run.
- Open the file in a text editor (like nano) with administrator privileges:
sudo nano /etc/hosts - Look at the first few lines of the file. You will see a line that looks like this:
127.0.1.1 old-computer-name - Use the arrow keys to move the cursor, delete the
old-computer-name, and type your new hostname in its place. Do not change the IP address. - Save the file by pressing Ctrl + O (the letter O), then press Enter to confirm the filename.
- Exit the text editor by pressing Ctrl + X.
Verifying the Change
The changes take effect immediately across the system. However, your current open terminal window will likely still display the old name in the prompt.
Simply close the terminal window and open a new one. Your prompt should now read user@new-name, and if you run hostnamectl again, you will see your new static hostname properly applied.