Mar 28, 2024 4 min read

How to Change Hostname on Ubuntu 22.04

Change Hostname on Ubuntu 22.04 with our step-by-step tutorial. A hostname is a unique label that identifies a device connected to a network.

Change Hostname on Ubuntu 22.04
Change Hostname on Ubuntu 22.04
Table of Contents

Introduction

Before we begin talking about how to change hostname on Ubuntu 22.04, let's briefly understand – What is hostname?

A hostname is a unique label that identifies a device connected to a network. It can be a computer, server, or any networked device. A hostname is used to navigate through the Internet and within local networks. It helps users to access websites, send emails, or establish connections with other devices.

Understanding hostnames is essential for efficient network communication, as they enable computers to communicate using human-readable names instead of numerical IP addresses.

In this tutorial, you will change hostname on Ubuntu 22.04. We will also address a few FAQs on how to change hostname on Ubuntu 22.04.

Displaying the Current Hostname

Use the hostnamectl command without an argument to view the current hostname:

hostnamectl
   Static hostname: vegastack-ubuntu2204
         Icon name: computer-vm
           Chassis: vm
        Machine ID: fde729d3db264dd199540018bd89c69f
           Boot ID: 5f94980339bc4819848297658ae752a3
    Virtualization: oracle
  Operating System: Ubuntu 22.04 LTS
            Kernel: Linux 5.15.0-27-generic
      Architecture: x86-64
   Hardware Vendor: innotek GmbH
    Hardware Model: VirtualBox

The hostname that is currently in use in this example is ubuntu2004.localdomain.

Changing the System Hostname with hostnamectl

Three distinct hostname classes are recognized by the hostnamectl command:

  • static - The traditional hostname. It is user-configurable and kept in the /etc/hostname file.
  • pretty - A free-form, descriptive UTF8 hostname that is presented to the user. Take Vegastack's laptop, for instance.
  • transient - A dynamic hostname that is maintained by the kernel. The temporary hostname can be modified at runtime by DHCP or mDNS servers. By default, it is the same to the static hostname.

For both static and transient names, it is generally advised to use a fully-qualified domain name (FQDN), such as host.example.com.

Modifying the hostname of the system is an easy task. The hostnamectl command has the following syntax:

sudo hostnamectl set-hostname host.example.com

For instance, you would use the following command to change the system static hostname to host.vegastack.com:

sudo hostnamectl set-hostname host.vegastack.com

You can also choose to set the attractive hostname:

sudo hostnamectl set-hostname "Vegastack's laptop" --pretty

The hostnamectl command generates no output. Returns 0 in the event of success and a non-zero failure code otherwise.

The static hostname is stored in /etc/hostname, and the pretty hostname is stored in /etc/machine-info file.

Use the hostnamectl command to confirm that the hostname has been modified:

sudo hostnamectl

On the terminal, your new hostname will appear as follows:

Output

Static hostname: host.vegastack.com
   Pretty hostname: Vegastack's desktop
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a04e3543f3da460294926b7c41e87a0d
           Boot ID: aa31b274703440dfb622ef2bd84c52cb
    Virtualization: oracle
  Operating System: Ubuntu 22.04 LTS
            Kernel: Linux 5.4.0-26-generic
      Architecture: x86-64

Changing the hostname in the GUI

If you are using Ubuntu Desktop, you can use GNOME Settings to modify your computer hostname.

As indicated below, select "Settings" from the system menu by opening it in the top-right corner.

Navigate to the "About" tab in the "Settings" window, then select "Device name." Click the "Rename" button after entering your new system hostname in the pop-up dialog box.

Both the static and attractive names will change as a result.

The /etc/hosts File

You must map the hostname to the local IP address if your system hostname lacks a public DNS name.

IP addresses and host names are mapped using this /etc/hosts file. This mapping is used by network applications running on the instance.

Change the old hostname to the new one by opening the file:

                                   /etc/hosts

127.0.0.1   localhost
127.0.0.1   host.vegastack.com

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Ubuntu Cloud Server

When you restart the system after running Ubuntu on a cloud instance and installing the cloud-init package, the modified hostname will be recovered. This package manages the initialization of the cloud instances and is typically installed by default in the images supplied by the cloud providers.

You can edit the /etc/cloud/cloud.cfg file to permanently apply the change.

sudo vim /etc/cloud/cloud.cfg

Look up preserve_hostname and set its value to true instead of false:

                              /etc/cloud/cloud.cfg
                              
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

Save and close your editor.

You don't need to make any more adjustments if the file is missing from your system. This indicates that the cloud-init package isn't installed.

FAQs to Change Hostname on Ubuntu 22.04

How can I check the current hostname on Ubuntu 22.04?

Open the terminal and type hostname command. It will display the current hostname of your Ubuntu system.

How can I change the hostname on Ubuntu 22.04?

Edit the /etc/hostname file using a text editor. Replace the existing hostname with the new one, save the file, and reboot your system.

Is it necessary to reboot the system after changing the hostname?

Yes, rebooting is required for the changes to take effect and update the hostname on Ubuntu.

Can I change the hostname without rebooting the system?

Yes, you can change the hostname temporarily using the hostname command, but it will revert to the original hostname upon reboot.

Are there any restrictions or guidelines for choosing a hostname?

The hostname should be unique within the network, typically consist of lowercase letters, digits, or dashes, and not exceed 63 characters.

Can I use special characters or spaces in the hostname?

It is recommended to avoid using special characters or spaces in the hostname to prevent any issues with system compatibility.

How can I verify if the hostname has been successfully changed?

After rebooting, use the hostname command again in the terminal. It should display the updated hostname, confirming the change on Ubuntu 22.04.

Conclusion

We hope this tutorial helped you understand how to change hostname on Ubuntu 22.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.