How to Change Hostname on Ubuntu

By 

Updated on

4 min read

Change the Hostname in Ubuntu

A hostname is a human-readable label assigned to a device and used to identify the device on a network. The hostname is set during the installation of the operating system or dynamically assigned to the virtual machine when it is created.

Every machine on a network should have a unique hostname. Having two or more machines with the same hostname on the same network can cause problems.

Only root or users with sudo privileges can change the system hostname. This guide shows how to change it on Ubuntu without restarting the system, using the hostnamectl command on servers or GNOME Settings on desktop systems. The steps apply to current supported Ubuntu releases, including Ubuntu 24.04 and Ubuntu 26.04.

Displaying the Current Hostname

To view the current hostname, invoke the hostnamectl command without any argument:

Terminal
hostnamectl
Ubuntu hostnamectl output

In this example, the current hostname is set to ubuntu.localdomain.

Changing the System Hostname with hostnamectl

The hostnamectl command recognizes three different classes of hostname:

  • static - The traditional hostname. It is stored in the /etc/hostname file and can be set by the user.
  • pretty - A descriptive free-form UTF-8 hostname used for presentation to the user. For example, Linuxize laptop.
  • transient - A dynamic hostname that is maintained by the kernel. DHCP or mDNS servers can change the transient hostname at run time. By default, it is the same as the static hostname.

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

Changing the system hostname is a simple process. The syntax of the hostnamectl command is as follows:

txt
hostnamectl set-hostname HOSTNAME [OPTIONS]

For example, to change the system static hostname to zoe.linuxize.com, you would use the following command:

Terminal
sudo hostnamectl set-hostname zoe.linuxize.com

Optionally, you can also set the pretty hostname:

Terminal
sudo hostnamectl set-hostname "Linuxize laptop" --pretty

hostnamectl does not produce output. On success, 0 is returned; 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.

To verify the hostname has been changed, enter the hostnamectl command:

Terminal
sudo hostnamectl

Your new hostname will be printed on the terminal:

output
   Static hostname: zoe.linuxize.com
   Pretty hostname: Linuxize laptop
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a04e3543f3da460294926b7c41e87a0d
           Boot ID: aa31b274703440dfb622ef2bd84c52cb
    Virtualization: oracle
  Operating System: Ubuntu 26.04 LTS
            Kernel: Linux 7.0.0-14-generic
      Architecture: x86-64

Changing the Hostname in the GUI

If you are running Ubuntu Desktop, you can use GNOME Settings to change your machine hostname.

Open the system menu in the top-right corner and click the Settings icon:

Ubuntu quick settings menu

In the Settings window, select “System” from the sidebar and open “About”:

Ubuntu System settings page

Click the “Device Name” field, enter the new hostname, and click the check mark to apply the change:

Ubuntu Device Name field in About settings

This will change both the static and pretty names.

The /etc/hosts File

If your system hostname does not have a public DNS name, you need to map the hostname to the local IP address.

The /etc/hosts file is used to map IP addresses to host names. This mapping is used by network applications running on the instance.

Open the file and change the old hostname to the new one:

/etc/hoststext
127.0.0.1   localhost
127.0.0.1   zoe.linuxize.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

If you run Ubuntu on a cloud instance and the cloud-init package is installed, the changed hostname may be restored after a system restart. This package is usually installed by default in the images provided by cloud providers, and it is used to handle the initialization of cloud instances.

To make the change permanent, edit the /etc/cloud/cloud.cfg file.

Terminal
sudo vim /etc/cloud/cloud.cfg

Search for preserve_hostname, and change the value from false to true:

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

Save the file and close your editor.

If the file does not exist on your system, the cloud-init package is not installed and you do not have to make any further changes.

Troubleshooting

The hostname changes back after reboot
Check whether cloud-init manages the hostname. Set preserve_hostname: true in /etc/cloud/cloud.cfg if you want to keep a manually configured hostname on a cloud instance.

The hostname command fails
Use only letters, numbers, hyphens, and dots for a static hostname. Avoid spaces and underscores in the static hostname.

Local commands still show the old hostname
Check both /etc/hostname and /etc/hosts. If the old hostname appears in /etc/hosts, replace it with the new hostname.

Conclusion

You can change the Ubuntu hostname with hostnamectl or through GNOME Settings without restarting the machine. On cloud instances, also check cloud-init so the hostname does not revert after reboot.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page