How to Change Hostname on Ubuntu 24.04

A hostname is a human-readable name used to identify a specific device. Suppose you are connected to a server, the hostname will help the server owner identify your device and allow you access. A hostname is just like an IP address, but it is much easier to remember for device identification. Also, it’s safer to share it as a complement to the IP address.

The default hostnames are not unique, and you get a generic name if you did not set them while installing your machine. Changing the default hostname helps you avoid conflicts with other devices over a network. This brief article will cover different methods for changing the hostname on an Ubuntu 24.04 machine.

Table of Contents:

  1. How to Check Ubuntu 24.04 Hostname
  2. How to Temporarily Change Ubuntu 24.04 Hostname
  3. How to Permanently Change the Hostname in Ubuntu 24.04
  4. How to Change Ubuntu 24.04 Hostname Using GUI Method
  5. Conclusion

1. How to Check Ubuntu 24.04 Hostname

Before you can modify or update the current hostname, it’s better to first check the current hostname using the hostname command. You can run this command, and it will display your computer’s current hostname on the screen:

hostname
Change Hostname on Ubuntu 24.04 a

There’s one more command you can use to check the system hostname, hostnamectl command. For this, you can run:

hostnamectl

This command will display the Ubuntu hostname along with other related system information. The static hostname displayed in the image is your system hostname.

Change Hostname on Ubuntu 24.04 b

Before we continue towards the changing of system hostname, first you must know the types of hostnames a Linux system has.

In the Linux system, we typically have three types of hostnames that are as follows:

  • Static hostname: This is the user-configured hostname, the one that gets set during system initialisation and is often found in the /etc/hostname file.
  • Transient hostname: This is a temporary hostname assigned by network configuration and is used as a fallback if no static hostname is set.
  • Pretty hostname: This is a more descriptive or user-friendly version of the hostname, which includes spaces or special characters.

Now that we understand the three hostname types, let’s explore how to modify them.

2. How to Temporarily Change Ubuntu 24.04 Hostname

To temporarily change your system hostname, you can use the hostname command. This command will set up a new temporality hostname for your device. The hostname change is not persistent and will be lost upon reboot of the Ubuntu machine.

You can use this command to set a new hostname, remember to replace the new-hostname flag with the hostname you want to set:

sudo hostname new-hostname
Change Hostname on Ubuntu 24.04 c

Upon successful completion, this will not display any output. To check if the name is set, run the hostname command:

hostname
Change Hostname on Ubuntu 24.04 d

3. How to Permanently Change the Hostname in Ubuntu 24.04

If you want to permanently change the Ubuntu 24.04 hostname so that it doesn’t get lost upon reboot, you can try two different methods. Either use the hostnamectl command or edit the hostname inside the configuration files.

Now, we will cover these methods of permanently changing hostnames here.

3.1. Using set-hostname Command

The simplest method for permanently changing the hostname is using the set-hostname command. You just have to enter the below command, which will change the hostname accordingly.

hostnamectl set-hostname new-hostname

Remember to replace the new-hostname flag with the actual hostname you want to see.

Change Hostname on Ubuntu 24.04 e

Like the hostname command, the set-hostname command also does not show any output and to confirm the change you have to run the hostnamectl command:

hostnamectl
Change Hostname on Ubuntu 24.04 f

You can also change your system’s pretty hostname. As mentioned earlier, the pretty hostname is not displayed to other systems on a network; in fact, it is only displayed to the system owner. The name that other computers see is the static hostname.

To change the pretty hostname, you can use the same above command but just have to add the –pretty flag in it:

hostnamectl set-hostname "new-hostname" --pretty
Change Hostname on Ubuntu 24.04 g

Finally, confirm the changes made by using the hostnamectl command:


hostnamectl
Change Hostname on Ubuntu 24.04 h

The pretty hostname is stored in the /etc/machine-info system file. You can also edit this file to modify the pretty hostname.

3.2. Editing the Configuration Files

The second way of permanently modifying the hostname is by editing the configuration files. We can edit the two configuration files, /etc/hostname and /etc/hosts file.

First, open the /etc/hostname file using any text editor such as nano:

sudo nano /etc/hostname
Change Hostname on Ubuntu 24.04 i

Locate the line containing the current hostname. Completely replace the current hostname with your new preferred hostname. Save and exit the file.

Change Hostname on Ubuntu 24.04 j

We’ll now edit the /etc/hosts file to manage hostname to IP address mappings. This file allows you to define custom associations between names and their corresponding IP addresses.

sudo nano /etc/hosts
Change Hostname on Ubuntu 24.04 k

Once you open the file, search for the specific hostname you want to modify. Replace the existing hostname with your desired choice.

Change Hostname on Ubuntu 24.04 l

After making the changes, save the updated file within the text editor. Finally, exit the editor to finalize the modifications.

The last step is to reboot the system so all the new changes are applied to it:

sudo systemctl reboot

That’s it; we have successfully modified the Ubuntu 24.04 hostname.

4. How to Change Ubuntu 24.04 Hostname Using GUI Method

Sometimes, you might not prefer the terminal for these tasks, or you might not remember the commands needed for all these operations. In this case, using the GUI-based method is preferable.

First, open your system Settings using the application menu.

Change Hostname on Ubuntu 24.04 m

After that, navigate to the About section.

Change Hostname on Ubuntu 24.04 n

Now edit the Device name field.

Change Hostname on Ubuntu 24.04 o

This will successfully change your Ubuntu 24.04 hostname. Lastly, give your system a reboot, so all processes go smoothly.

Conclusion

Hostname on Ubuntu is like a home address that helps systems and servers identify your device. It is like an IP address, with fewer security concerns. To change the IP address on Ubuntu, you can try the hostname or set-hostname command. Both these commands change the Ubuntu system hostname. If you prefer the GUI method, you can edit the hostname from the device’s About section settings.