Skip to content

Linux basicsFor beginnersDuration around 5 min.

How to change the hostname on Ubuntu permanently

The hostname appears in the command prompt, in logs and in the server's emails. This tutorial changes it permanently โ€” without a reboot โ€” and shows which file needs to match.

Tested on with systemd on Ubuntu 24.04 LTS and Ubuntu 26.04 LTS

At a glance

  • sudo hostnamectl set-hostname web01 changes the name immediately and writes it to /etc/hostname.
  • In /etc/hosts, this requires the line 127.0.1.1 with the full and short name.
  • cloud-init leaves a manually set name as is; this is explicitly defined with preserve_hostname: true.

Prerequisites

  • A server running Ubuntu 24.04 or 26.04 and a user with sudo privileges, such as a VPS or a Cloud Server.
  • A name consisting of lowercase letters, numbers and hyphens, maximum 63 characters per part โ€” such as web01. You only use the full name web01.example.com with your own domain.

1. Show the current hostname

Show name
hostnamectl
hostname
First line of the hostnamectl output
 Static hostname: ubuntu-alt

2. Set the new hostname

hostnamectl changes the name immediately and writes it to /etc/hostname. A reboot is not necessary; the command prompt shows the new name from the next login.

Set hostname
sudo hostnamectl set-hostname web01
Review
hostnamectl --static
cat /etc/hostname

3. Adjust the entry in /etc/hosts

So that the server can resolve its own name, it belongs in /etc/hosts. Replace the old name in the line with 127.0.1.1 or create it โ€” first the full name, then the short name.

Open file
sudo nano /etc/hosts
/etc/hosts
127.0.0.1 localhost
127.0.1.1 web01.example.com web01

Afterwards, hostname returns the short name and hostname -f the full name:

Short and full name
hostname
hostname -f
Output
web01
web01.example.com

4. Permanent on cloud images

Many cloud images boot with cloud-init. It applies the name from the provider's details on every boot โ€” it leaves a name that you have changed yourself in /etc/hostname as is by default. If you want to explicitly define this, create a custom file:

Create file
sudo nano /etc/cloud/cloud.cfg.d/99-hostname.cfg
/etc/cloud/cloud.cfg.d/99-hostname.cfg
preserve_hostname: true

If the folder /etc/cloud does not exist, cloud-init is not running, and this step is skipped.

5. Display name with spaces

In addition to the technical name, hostnamectl supports a freely selectable display name. It only appears in the output of hostnamectl and does not change anything in the network or logs.

Set display name
sudo hostnamectl set-hostname "Webserver Frankfurt" --pretty

Name and reverse DNS

If the server sends emails, the full hostname should also be the reverse DNS entry of its IP address. Both together help to ensure that emails are not marked as spam.

Typical errors

  • sudo: unable to resolve host: On Ubuntu 24.04, the new name is missing in /etc/hosts. Add it as in step 3; the command itself runs despite the message.
The message
sudo: unable to resolve host web01: Name or service not known
  • Old name in the command prompt: The current session still shows the old name. Sign out and sign in again.
  • Name is old again after reboot: Check whether cloud-init is running and create the file from step 4.

We tested all commands in this guide on 2 December 2026 โ€” with systemd on Ubuntu 24.04 LTS and Ubuntu 26.04 LTS. If a step changes at the manufacturer, we update it here along with the date.

Official documentation

Frequently asked questions about the hostname

Do I have to reboot the server after changing it?

No. hostnamectl sets the name immediately; only the current session shows the old name until you sign in again.

Short or full hostname?

The short name is usually sufficient as the hostname. You also add the full name with domain to /etc/hosts, then hostname -f returns it.

Which characters can the hostname contain?

Letters, numbers and hyphens, maximum 63 characters per part. Spaces and umlauts only belong in the display name.

What is the entry with 127.0.1.1 for?

It ensures that the server can resolve its own name without DNS. Debian and Ubuntu use 127.0.1.1 instead of 127.0.0.1 for this.

Does cloud-init reset my hostname?

cloud-init leaves a name that you have changed yourself in /etc/hostname as is by default. You explicitly define this with preserve_hostname: true.

Further reading

More tutorials

  • The scp command with examples: copy files over SSH

    Linux basicsFor beginnersabout 5 min

    scp copies files over an SSH connection โ€” encrypted, with the same credentials as your login, and without an additional service on the server. This guide shows the commands for common use cases, each with an example to copy.

  • How to check your Ubuntu version, codename and kernel

    Linux basicsFor beginnersabout 2 min

    A single command in the terminal shows which Ubuntu version is running on a server. Here are the common ways with their output โ€” for the version with point release, the codename, the kernel and the architecture.

  • How to remove a directory in Linux with rm and rmdir

    Linux basicsFor beginnersabout 5 min

    In Linux, you delete an empty folder with rmdir and a folder including its contents with rm -r. This tutorial shows both commands with their messages, the variants with and without prompting, and what to check before running rm -rf โ€” because there is no recycle bin in the terminal.

A server with its own name

VPS and Cloud Servers start with Ubuntu 24.04 LTS โ€” you set the hostname afterwards as shown here.