Ubuntu Linux Change Hostname
On Ubuntu, the hostname is the name used to identify your computer or server on the system and, depending on your network configuration, on the local network. You can view the current hostname with hostname or hostnamectl and change it permanently from the Terminal.
For current Ubuntu systems that use systemd, the simplest method is hostnamectl set-hostname. This tutorial also retains the manual method of editing /etc/hostname and /etc/hosts, which is useful for understanding where the hostname configuration is stored and referenced.
Check the Current Hostname in Ubuntu
Before changing the hostname, check the current value. The hostname command prints the system hostname:
hostname
You can also use hostnamectl to display hostname information maintained by systemd:
hostnamectl
Look for the Static hostname value in the output. In the example used throughout this tutorial, the existing hostname is ubuntu-desktop and the new hostname will be ubuntu-pc.
Change Ubuntu Hostname Permanently with hostnamectl
On Ubuntu versions that use systemd, including modern Ubuntu desktop and server releases, use hostnamectl set-hostname to set the static hostname.
sudo hostnamectl set-hostname new-hostname
For example, to change the hostname from ubuntu-desktop to ubuntu-pc, run:
sudo hostnamectl set-hostname ubuntu-pc
The command normally produces no output when it succeeds. Verify the new hostname with:
hostnamectl --static
ubuntu-pc
You can also run hostname to verify the active hostname. A terminal prompt that still displays the previous hostname may need a new terminal session or a new login before the prompt itself is refreshed.
Update /etc/hosts After Changing the Ubuntu Hostname
Changing the hostname with hostnamectl updates the system’s static hostname, but it does not necessarily update hostname entries that you have in /etc/hosts. If that file contains the old hostname, update the matching entry so local name resolution remains consistent.
Open the file with:
sudo nano /etc/hosts
On many Ubuntu installations, you may see an entry similar to:
127.0.1.1 ubuntu-desktop
If so, replace only the old hostname with the new one:
127.0.1.1 ubuntu-pc
Do not remove unrelated addresses or host mappings from /etc/hosts.
Steps to Change Hostname Manually in Ubuntu
The following steps show the manual method of changing the Ubuntu hostname by editing /etc/hostname and the corresponding hostname entry in /etc/hosts. The screenshots use Ubuntu 20.04 LTS and change the hostname from ubuntu-desktop to ubuntu-pc.
Step 1: Open Terminal by using Ctrl+Alt+T shortcut key or from Applications.
)
The Terminal title and prompt in this example read sony@ubuntu-desktop. Here, sony is the username and ubuntu-desktop is the hostname. You can also check the hostname using the hostname command.
)
Step 2: Change the static hostname stored in /etc/hostname. In this example, we will change it from ubuntu-desktop to ubuntu-pc. Open /etc/hostname with nano using administrator privileges.
$ sudo nano /etc/hostname
)
Enter your password if prompted. When entering a password for sudo, the Terminal normally does not display characters or asterisks. Type the password and press Enter.
)
The /etc/hostname file opens in nano. It normally contains the static hostname. Replace ubuntu-desktop with ubuntu-pc.
)
Step 3: Change the hostname to your desired name.
)
Press Ctrl+X to exit nano. When nano asks whether to save the modified buffer, choose to save the change.
)
Type y or Y and press Enter.
)
Press Enter again to confirm the filename. The new static hostname is now stored in /etc/hostname.
Step 4: Update the old hostname in /etc/hosts if an entry for it is present.
)
Open the file using nano editor with root access (sudo). Run the following command in Terminal.
$ sudo nano /etc/hosts
)
Locate the line that maps the old hostname. In the example shown in the screenshot, replace ubuntu-desktop with ubuntu-pc. The hostname referenced in /etc/hosts should match the new hostname when that entry is intended to represent the local machine.
)
Change the hostname and press Ctrl+X.
)
Press y or Y, and then press Enter.
)
Press Enter again to confirm the filename. The matching hostname entry in /etc/hosts is now updated.
Step 5: When using this manual file-editing method, rebooting is a straightforward way to ensure services and sessions start with the new hostname. Run the following command in Terminal, or reboot using the desktop interface.
)
After the system starts again, verify the hostname using hostname or hostnamectl.
Verify the New Ubuntu Hostname
After changing the hostname, use more than one check if you want to confirm both the active hostname and the configured static hostname.
hostname
hostnamectl --static
cat /etc/hostname
For the example in this tutorial, all three should report ubuntu-pc.
If you updated /etc/hosts, you can also check the relevant local mapping with:
grep ubuntu-pc /etc/hosts
Ubuntu Hostname Naming Rules to Follow
For a simple static hostname, use a short DNS-compatible name consisting of letters, digits, and hyphens. Avoid spaces and shell-special characters. A practical example is ubuntu-pc, while a name such as Ubuntu PC should not be used as the static hostname.
If the computer participates in DNS, Active Directory, configuration management, cloud-init, or another network-management system, check that system’s naming requirements before changing the hostname. Such services can depend on the configured name or may manage it themselves.
Change Ubuntu Hostname Without Rebooting
When you use hostnamectl set-hostname, the active and persistent hostname can normally be changed without rebooting the entire Ubuntu system:
sudo hostnamectl set-hostname ubuntu-pc
Run hostname immediately afterward to check the active value. Existing applications, remote sessions, or shell prompts may continue displaying a cached or previously established hostname until they are restarted or a new login session is created.
Ubuntu Hostname Change Command Summary
| Task | Command or file |
|---|---|
| Show the current hostname | hostname |
| Show systemd hostname information | hostnamectl |
| Change the hostname permanently | sudo hostnamectl set-hostname new-hostname |
| Show the configured static hostname | hostnamectl --static |
| Store the static hostname manually | /etc/hostname |
| Update a local hostname mapping when required | /etc/hosts |
Ubuntu Hostname Change Tutorial Summary
In this Linux Tutorial, we learned how to check and change the hostname of an Ubuntu Linux PC. For current systemd-based Ubuntu installations, sudo hostnamectl set-hostname new-hostname is the direct method for changing the static hostname permanently. We also covered the manual /etc/hostname and /etc/hosts method and commands for verifying the resulting hostname.
Ubuntu Hostname Editorial QA Checklist
- Confirm that
hostnamectl set-hostnameis presented as the primary method for current systemd-based Ubuntu releases. - Confirm that the tutorial does not claim
hostnamectlautomatically rewrites hostname entries in/etc/hosts. - Confirm that the old and new example hostnames remain consistently identified as
ubuntu-desktopandubuntu-pc. - Confirm that the manual method distinguishes
/etc/hostnamefrom the local name mappings stored in/etc/hosts. - Confirm that rebooting is not incorrectly described as mandatory after a successful
hostnamectl set-hostnamecommand. - Confirm that verification includes both the active hostname and the persistent static hostname.
- Confirm that hostname examples avoid spaces and use DNS-compatible letters, digits, and hyphens.
- Confirm that systems managed by cloud-init, DNS, directory services, or configuration-management tools are not assumed to behave like an unmanaged desktop installation.
TutorialKart.com