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

Choose a different version or distribution

Introduction

Before we discuss how to quickly change your hostname on Ubuntu 22.04, let's first understand-What is Hostname?

The hostname represents the name of the system on a network. By changing the hostname, you can personalize it to better reflect your preferences or the purpose of the system.

The hostname of a Linux system is important because it is used to identify the device on a network. The hostname is also shown in other prominent places, such as in the terminal prompt. This gives you a constant reminder of which system you are working with.

Hostnames give us a way to know which device we are interacting with, either on the network or physically, without remembering a bunch of IP addresses that are subject to change. You should pick a descriptive hostname like “ubuntu-desktop” or “backup-server” rather than something ambiguous like “server2”.

Advantages

  1. Personalization: Changing the hostname allows you to give your system a unique and recognizable name that reflects your preferences or organization's branding.
  2. Identification: A meaningful hostname helps in easily identifying and distinguishing your system from others on a network.
  3. Organizational Structure: Setting a hostname based on the system's function or role can help maintain an organized infrastructure.
  4. Logging and Audit Trails: A well-defined hostname can be advantageous for managing logs and audit trails, making it easier to track events and troubleshoot issues.
  5. Administration: A descriptive hostname can simplify system administration by quickly identifying the purpose and location of the system.

Displaying the Current Hostname

Use the hostnamectl command without any arguments to view the current hostname:

hostnamectl

Changing the System Hostname

The system hostname can be easily modified. The syntax is as follows:

sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your Pretty HostName" --pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient

For instance, you would use the following command to modify the system's static hostname to v1.vegastack.com:

sudo hostnamectl set-hostname v1.vegastack.com

You can also set the pretty hostname:

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

No output is generated by hostnamectl. When successful, 0 is returned; otherwise, a non-zero failure code is returned.

The pretty hostname is kept in the /etc/machine-info file, while the static hostname is kept in /etc/hostname.

The same hostname should not be used on two different machines connected to the same network.

The hostname is mapped to 127.0.0.1 in /etc/hosts on most of the systems. Open the file and update the old hostname to the new one.

127.0.0.1   localhost
127.0.0.1   v1.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

If the cloud-init package is installed and Ubuntu is operating on a cloud instance, you also need to make changes to the /etc/cloud/cloud.cfg file. This package, which manages the initialization of the cloud instances, is generally installed by default in the images provided by the cloud providers.

If the file is already on your computer, open it:

sudo vim /etc/cloud/cloud.cfg

Find preserve_hostname, and change the value from false to true:

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

Save the file, then exit your editor.

Verify the change

Enter the hostnamectl command to confirm the hostname has been fully changed:

hostnamectl

Your new hostname will appear on the terminal as follows:

Output

   Static hostname: v1.vegastack.com
   Pretty hostname: PeerXP'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

FAQs on how to change Hostname on Ubuntu 22.04

What are the restrictions for setting a hostname on Ubuntu 22.04?

Hostnames on Ubuntu 22.04 can consist of letters (a-z, A-Z), numbers (0-9), and hyphens (-). They should start and end with an alphanumeric character.

Can I change the hostname without restarting my Ubuntu 22.04 system?

Yes, you can change the hostname without a system restart. After updating the hostname, you need to run a command to make the changes effective immediately.

How can I change the hostname on Ubuntu 22.04 temporarily? 

Use the hostname command followed by the desired hostname to change it temporarily. However, this change will revert after a system reboot.

How can I permanently change the hostname on Ubuntu 22.04? 

To change the hostname permanently, you need to update the /etc/hostname file with the new desired hostname and also modify the /etc/hosts file accordingly.

Is it necessary to update the hostname in the /etc/hosts file? 

Yes, it is essential to update the new hostname in the /etc/hosts file to ensure proper name resolution and prevent any issues related to local hostname resolution.

Can I use spaces or special characters in the hostname on Ubuntu 22.04?

No, it is not recommended to use spaces or special characters in the hostname. Stick to alphanumeric characters and hyphens.

Can I change the hostname remotely on Ubuntu 22.04 using SSH? 

Yes, you can change the hostname remotely via an SSH session by following the same steps outlined for changing the hostname locally.

Conclusion

You have learned how to easily change the hostname on Ubuntu 22.04 installation in this tutorial.

You might need to change the hostname for a number of reasons. The hostname is most commonly automatically specified when an instance is created.

If you have any queries, feel free to leave a comment below, and we'll be happy to help.

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.