How to Find and Change Your Hostname in Ubuntu (22.04, 24.04, and Later)

Ā·
5 min read

Help Desk Geek is reader-supported. We may earn a commission when you buy through links on our site. Learn more.

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

  1. Open a terminal with Ctrl + Alt + T.
  2. Run:
hostnamectl
Terminal window showing the full output of the hostnamectl command, with Static hostname, Pretty hostname, Transient hostname, OS, kernel, and architecture fields all visible

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

  1. Open Settings.
  2. Go to System > About. The exact navigation varies by GNOME version, but look for an About or About This Device option.
  3. The Device name field shows your current static hostname.
GNOME Settings System > About panel with the Device name field visible, showing the 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.

  1. Open a terminal with Ctrl + Alt + T.
  2. 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.

  1. Open /etc/hosts:
sudo nano /etc/hosts
  1. Find the line starting with 127.0.1.1 and update the hostname on that line:
127.0.1.1    new-hostname
  1. Save with Ctrl + O, then exit with Ctrl + X.
  2. Reboot so all running services pick up the change:
sudo reboot
Terminal showing the sudo hostnamectl set-hostname command, followed by hostnamectl output confirming the updated Static hostname field

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:

  1. Go to System > About (path may vary slightly by GNOME version).
  2. Click the Device name field.
  3. Type the new hostname and press Enter.
GNOME Settings System > About panel with the Device name field selected and a new hostname being typed into the editable input

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.

  1. Open /etc/hostname:
sudo nano /etc/hostname
  1. The file contains a single line: your current hostname. Replace it:
new-hostname
nano editor open with /etc/hostname, showing a single hostname on the first line being replaced with a new value
  1. Save with Ctrl + O, then exit with Ctrl + X.
  2. Update /etc/hosts to match (same as steps 3–5 in Method 1).
  3. 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:

  1. Open the cloud-init config:
sudo nano /etc/cloud/cloud.cfg
  1. Add or update this line:
preserve_hostname: true
  1. Save and exit, then set your hostname again with hostnamectl and 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.