Finding your Ubuntu hostname takes one command; changing it permanently takes two. On Ubuntu 22.04, 24.04, and later, hostnamectl is the reliable tool for both, and itās more predictable than editing config files by hand.
What Is a Hostname in Ubuntu?
Ubuntu tracks three hostname types. Youāll mainly care about the first one:
- Static hostname: the canonical label stored in
/etc/hostname. It appears in your terminal prompt, in SSH connections, and is how most network services identify the machine. - Pretty hostname: a human-friendly version that can include spaces and mixed case (for example, āMaryās Home Serverā). Optional and mostly cosmetic.
- Transient hostname: a temporary name typically assigned by DHCP from your network router. Most desktop users never set this directly.
The Device name field in GNOME Settings maps to the static hostname. Theyāre the same value surfaced in two places.
Find Your Hostname
Via the Terminal
- Open a terminal with
Ctrl + Alt + T. - Run:
hostnamectl
The output shows all three hostname types plus OS and kernel details. To see just the short static hostname:
hostname
For the fully-qualified domain name, if your system is configured with one:
hostname -f
Via GNOME Settings
- Open Settings.
- Go to System > About. The exact navigation varies by GNOME version, but look for an About or About This Device option.
- The Device name field shows your current static hostname.
Change Your Hostname
Method 1: Use hostnamectl (recommended)
hostnamectl writes the new name, updates /etc/hostname, and takes effect immediately. Itās the right choice for Ubuntu desktops, Raspberry Pi units running Ubuntu, and standard server installs alike.
- Open a terminal with
Ctrl + Alt + T. - Run:
sudo hostnamectl set-hostname new-hostname
Replace new-hostname with your chosen name. Use only lowercase letters, digits, and hyphens; no spaces, no underscores.
- Open
/etc/hosts:
sudo nano /etc/hosts
- Find the line starting with
127.0.1.1and update the hostname on that line:
127.0.1.1 new-hostname
- Save with
Ctrl + O, then exit withCtrl + X. - Reboot so all running services pick up the change:
sudo reboot
After the reboot, run hostnamectl to confirm. Your terminal prompt should reflect the new name.
Donāt skip the /etc/hosts update. If the 127.0.1.1 line still references the old hostname, sudo commands will pause with āunable to resolve hostā warnings and SSH connections may behave erratically.
Method 2: Change via GNOME Settings
Open Settings from the dock or the app grid, then follow these steps:
- Go to System > About (path may vary slightly by GNOME version).
- Click the Device name field.
- Type the new hostname and press Enter.
GNOME calls hostnamectl under the hood, so the change persists through reboots. Still update /etc/hosts manually after this; GNOME wonāt do it for you. Use steps 3ā5 from Method 1.
Method 3: Edit /etc/hostname directly (alternative)
This approach bypasses hostnamectl and works on any Ubuntu install, including headless and minimal setups.
- Open
/etc/hostname:
sudo nano /etc/hostname
- The file contains a single line: your current hostname. Replace it:
new-hostname
- Save with
Ctrl + O, then exit withCtrl + X. - Update
/etc/hoststo match (same as steps 3ā5 in Method 1). - Reboot:
sudo reboot
Method 4: Temporary change (current session only)
Testing a name before committing? This changes the hostname for the running session without touching /etc/hostname:
sudo hostname temp-hostname
A reboot returns the machine to whatever static hostname is in /etc/hostname. Donāt use this for permanent changes.
Troubleshooting
sudo or SSH is slow after the change
This almost always means /etc/hosts still references the old hostname. Open it:
sudo nano /etc/hosts
Confirm the 127.0.1.1 line matches your new hostname, save, and test:
ping new-hostname
Hostname keeps reverting after a reboot
On cloud VPS instances, cloud-init may reset the hostname at boot from the providerās metadata. To stop it:
- Open the cloud-init config:
sudo nano /etc/cloud/cloud.cfg
- Add or update this line:
preserve_hostname: true
- Save and exit, then set your hostname again with
hostnamectland reboot.
Old hostname still shows in the prompt
Shell sessions open before the change hold the old name in memory. Log out and back in, or reboot to clear it system-wide.
Setting a Pretty Hostname (Optional)
If you want a display-friendly label that can include spaces, such as āLiving Room Serverā or āDev Machine 2ā, you can set a pretty hostname separately from the static one:
sudo hostnamectl set-hostname "Living Room Server" --pretty
With --pretty, only the pretty hostname changes; the static hostname (for example, living-room-server) stays exactly as it was. The pretty hostname is the cosmetic label some GUIs surface. Most users donāt need this distinction, but itās handy when the machine-readable name has to stay lowercase-hyphenated while the display name doesnāt.
Which Method Should You Use?
Method 1 (hostnamectl) is the solid pick for the vast majority of setups. Itās direct, persistent, and consistent across Ubuntu 22.04, 24.04, and later. The step that trips most people up is the /etc/hosts update: skip it and youāll see sudo warnings and sluggish SSH that look completely unrelated to the hostname change. If youāre on a cloud VPS and the hostname keeps reverting despite doing everything correctly, check /etc/cloud/cloud.cfg for the preserve_hostname setting before anything else.