How to Change the Hostname on an Ubuntu 22.04 Server

Changing the hostname of an Ubuntu server is a common administrative task. A meaningful hostname makes it easier to identify servers in SSH sessions, monitoring tools, log files, and management dashboards. This guide explains how to safely change the hostname on Ubuntu 22.04 running on a virtual machine, such as a Hetzner Cloud server.

Check the Current Hostname

Before making any changes, verify the current hostname:

hostnamectl

Example output:

Static hostname: snapshot-420739796-ubuntu-2gb-hel1-1
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 5.15.0-187-generic

Set a New Hostname

Use the hostnamectl command to assign a new hostname.

Example:

sudo hostnamectl set-hostname webserver01
sudo hostnamectl set-hostname db-sendgrid-autoverkopen.shop
root@snapshot-420739619-ubuntu-4gb-hel1-1:~# exec bash
root@db-sendgrid-autoverkopen:~#
 

You can replace webserver01 with any descriptive name that follows your naming convention.

Verify the change:

hostname
hostnamectl

Update the Hosts File

Edit the hosts file:

sudo nano /etc/hosts

Locate the line similar to:

127.0.1.1 snapshot-420739796-ubuntu-2gb-hel1-1 snapshot-420739796-ubuntu-2gb-hel1-1

Replace it with:

127.0.1.1 webserver01

The localhost entry should remain unchanged:

127.0.0.1 localhost

Save the file and exit the editor.

Apply the Changes

In many cases, opening a new SSH session is enough to display the new hostname.

If the shell prompt still shows the old hostname, simply reboot the server:

sudo reboot

After reconnecting, the prompt should display the new hostname:

root@webserver01:~#

Verify the Configuration

Confirm everything has been updated correctly:

hostname
hostnamectl
cat /etc/hosts

All commands should now report the new hostname.

Cloud-Init Considerations

Some cloud providers use cloud-init to configure virtual machines during boot. Depending on the configuration, cloud-init may restore the original hostname after a reboot.

To check whether hostname preservation is enabled:

grep preserve_hostname /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.d/* 2>/dev/null

If hostname changes are not preserved after reboot, configure cloud-init to preserve the custom hostname according to your cloud provider's documentation.

Why Change the Hostname?

Using descriptive hostnames provides several advantages:

  • Easier server identification in SSH sessions.

  • Clearer log files and monitoring dashboards.

  • Better organization when managing multiple servers.

  • Reduced risk of connecting to the wrong machine.

  • More professional infrastructure management.

     

Changing the hostname on Ubuntu 22.04 requires only a few steps:

  1. Set the new hostname with hostnamectl.

  2. Update the /etc/hosts file.

  3. Reboot or reconnect if necessary.

  4. Verify the configuration.

Once completed, the new hostname will be reflected throughout the operating system and in future SSH sessions, making server administration much more convenient.

Comments