How to Change the Hostname Ubuntu 24.04


The hostname is the device identifier within a specific network. These are human-readable names and are used in place of IP addresses too. While working in a network, you might need to change the hostname to differentiate your device from others, within the network.

This post will address the possible methods to change the hostname on Ubuntu 24.04 alongside the basic details of hostnames.

Outline:

Classification of Hostnames in Ubuntu

Before we start, it is necessary to have a look at the classes of the hostnames in Ubuntu. There are three classes of hostnames which are described/differentiated below:

Pretty Hostname (Found in /etc/machine-info file)

It is mostly a user’s choice. The user sets it for its ease/likeness. The pretty hostname may contain special characters too. Since it is user-specific it has no naming restrictions.

Static Hostname (Found in /etc/hostname file)

It is the most used hostname. The static hostname can be set manually and also the pretty hostname reflects upon it, i.e., the extra characters in pretty hostnames are removed by the system automatically and a refined hostname is called a static hostname. The static name could be “64 characters” and the first character should be a digit/letter. However, it may include underscores(_), dots(.), or hyphens(-).

Transient Hostname

The transient hostname is the reflection of the static hostname. It is managed by the Kernel and can be changed dynamically by the Kernel itself. It has the same naming rules as static.

The Ubuntu system checks for the pretty hostname first and then static followed by the transient. That’s how Ubuntu allocates the hostnames.

How to Change the Hostname in Ubuntu 24.04

The hostname can be changed using the hostnamectl, hostname, nmtui, nmcli, and GUI settings. Moreover, you can also set the hostnames by modifying the files associated with these hostnames, i.e., /etc/machine-info and /etc/hostname.

Let’s start with the hostnamectl command:

hostnamectl Command

The hostnamectl command changes any type of hostname as per the requirement, i.e., static, pretty, and transient. Here are the syntaxes and examples to change the hostnames using the hostnamectl command:

  • For Static Hostname

The static hostname set using the hostnamectl command is persistent. It remains the same after reboots/multiple changes. The following hostnamectl command changes the static hostname:

sudo hostnamectl set-hostname <new-hostname> --static

To confirm the changes, refresh the session or use the following command:

hostnamectl --static

  • For Pretty Hostname
sudo hostnamectl set-hostname <new-hostname> --pretty
  • For Transient Hostname
sudo hostnamectl set-hostname <new-hostname> --transient

You can see that the syntax is the same except for the option you use, i..e, static, pretty, transient.

hostname Command

Likewise hostnamectl, you can also change the hostname using the “hostname” command. The hostname command changed using the hostname command is not persistent, i.e., the hostname is restored after the reboot:

Here’s the command to change the hostname using the hostname command:

sudo hostname <new-hostname>

Let’s get into the nmcli command.

nmcli Command

The nmcli command provides the command line interface to manage the Network Manager, i.e., static IP, activate/deactivate specific interface, etc. Since the hostname is also associated with the network (identify the device over the network), thus you can change the hostname via the nmcli command as well:

Here is the command to change the hostname (static only) using the nmcli command:

sudo nmcli general hostname <new-hostname>

Let’s move to the nmtui command, which is another way to manage hostname via the network configuration.

nmtui | Terminal User Interface

NMTUI is the terminal interface of the Network Manager. It allows you to configure network settings and the hostname as well. Launch the nmtui via the command:

nmtui

Select the “Set system hostname”:

Write the new hostname and process with the “OK”. If you choose the hostname that contains the

Authenticate the process by providing the user password:

Here’s the prompt that confirms your hostname has been changed:

You will see the newly set hostname in the fresh terminal sessions.

sysctl Command | For the Transient Hostname Only

The sysctl command allows you to modify the kernel parameters. The “hostname” is the kernel parameter that refers to the transient hostname. Thus, with the use of sysctl command, you can change the transient hostname name via the following syntax:

sudo sysctl kernel.hostname=<new-hostname>

Modifying the /etc/machine-info File | For the Pretty Hostname Only

The primary location of the pretty hostname is “/etc/machine-info”. You can manually edit it and change the Pretty hostname.

To change the hostname, open the file in any editor:

You can see the current pretty hostname in the parenthesis. Let’s change it to “Genie 24 04”:

Save the changes and exit out of the editor. To confirm it, you can use the following hostnamectl command:

That’s how you can manage the pretty hostname from the machine info file.

Modifying the /etc/hostname File | For the Static Hostname Only

The static hostname is fetched from the “/etc/hostname” file. You can change the static hostname by editing the “/etc/hostname” file:

Open the file in the edit mode and you will find the current static name, i.e., Linux in our case:

Now, change the hostname to whatever you want, i.e., we changed it to “ubuntu2404”:

Here’s the change:

These were all the possible methods to change the hostname via Command Line Interface. Let’s get into the last method of this guide, which is GUI based.

Ubuntu Settings | GUI

Open the Ubuntu System Settings and navigate to the “About” section:

Inside “About”, change the “Device Name”, which actually is the pretty hostname:

You can verify it by opening a new terminal session or just using the hostnamectl command:

Note: If you observe, the capital letters are changed into small letters, and the space is replaced with a hyphen. That’s how your system translates the Pretty hostname into static.

Bottom Line

First, you need to identify which hostname you want to change, i.e., static, pretty, or transient. Most of the time, you have to deal with the static hostname which can be changed using various commands, i.e., hostnamectl, hostname, nmcli, nmtui, or by modifying the /etc/hostname file.

Among all, the hostnamectl command provides more control over hostname-related operations, i.e., can change all types of hostnames. That’s all about changing the hostname on Ubuntu 24.04.

Print Friendly, PDF & Email
Categories