How to Change a Hostname on Ubuntu 24.04

Learn how to check and permanently change the hostname on Ubuntu 24.04 using hostnamectl or configuration files.

A hostname is a unique, human-readable name assigned to a device on a network, such as a server, computer, or IoT device. Unlike IP addresses, which are numeric and intended for machines, hostnames offer a more memorable and meaningful way to identify devices. They often reflect the device’s role, location, or owner. Updating the hostname helps align with organizational standards, adhere to naming conventions and simplify device identification in multi-server environments. A clear and consistent naming system also enhances visibility in logs and monitoring tools.

This article explains how to check and update your hostname on an Ubuntu 24.04 server. You’ll learn how to view your server hostname, set a new one permanently, and ensure that the changes persist across reboots.

Prerequisites

Before you begin, you need to:

  • Have access to an Ubuntu 24.04 workstation as a non-root sudo user.

Verify the Active Hostname

In this section, you check the hostname of your Ubuntu server using the hostname command.

Run the below command to retrieve the hostname.

console
$ hostname

Your output should be similar to the one below:

vultr-server
Change the Hostname Using Hostnamectl

In this section, you’ll change your server hostname by using a command line utility and by editing the /etc/hosts file. Follow the steps below to update the hostname.

  1. To update the hostname, use the hostnamectl command.
    console
    $ sudo hostnamectl set-hostname prod-web-eu
    

    Replace prod-web-eu with your desired hostname. Hostnames should contain only letters, numbers, and hyphens. They must not contain spaces or special characters.

    Note

    If the command above returns no output, it means the change was applied. The command updates the static hostname, ensuring it persists after reboots.

  2. Update the /etc/hosts file.
    console
    $ sudo nano /etc/hosts
    
  3. Modify the line starting with 127.0.1.1 and update it to reflect the new hostname.
    ini
    127.0.1.1 prod-web-eu
    

    Save and exit the file. Updating the /etc/hosts file ensures that the system and local applications can resolve the new hostname to the server’s loopback IP address.

     

     

     

    Change the Hostname by updating /etc/hostname

    In this section, you’ll update the hostname of your Ubuntu server by manually editing the configuration files. This method is useful in minimal environments where tools like hostnamectl may not be available. By modifying the /etc/hostname and /etc/hosts files, you can ensure that the hostname change persists across reboots and is recognized by local services.

    1. Open the /etc/hostname file.
      console
      $ sudo nano /etc/hostname
      
    2. Replace the existing name with your new hostname.
      ini
      prod-web-eu
      

      Replace prod-web-eu with your preferred hostname. Valid hostnames may contain only lowercase letters, numbers, and hyphens. Avoid spaces or special characters.

    3. Update the /etc/hosts file.
      console
      $ sudo nano /etc/hosts
      
    4. Modify the line starting with 127.0.1.1 and update it to reflect the new hostname.
      ini
      127.0.1.1 prod-web-eu
      

      Save and exit the file. This ensures that local applications and system processes can resolve the hostname using the loopback IP address. The new hostname takes effect only after a reboot, as the system reads the /etc/hostname file during startup.

      Verify the New Hostname

      In this section, you’ll confirm that the hostname change was applied without errors and is now active on your server. Follow the steps below to verify the new hostname after restarting your server to apply all changes.

      1. Reboot the server to apply changes.
        console
        $ sudo reboot
        
      2. To retrieve the hostname, use the hostname command.
        console
        $ hostname
        

        Your output should be similar to the one below:

        prod-web-eu

      Conclusion

      You have updated the hostname on your Ubuntu 24.04 server using either the hostnamectl command or by manually editing the /etc/hostname and /etc/hosts files. This configuration ensures the hostname is persistent and resolvable by local services. A meaningful hostname enhances system log readability, streamlines server management, and helps enforce consistent naming conventions across your infrastructure.

Learn how to check and permanently change the hostname on Ubuntu 24.04 using hostnamectl or configuration files. A hostname is a unique, human-readable name assigned to a device on a network, such as a server, computer, or IoT device. Unlike IP addresses, which are numeric and intended for machines, hostnames…

Learn how to check and permanently change the hostname on Ubuntu 24.04 using hostnamectl or configuration files. A hostname is a unique, human-readable name assigned to a device on a network, such as a server, computer, or IoT device. Unlike IP addresses, which are numeric and intended for machines, hostnames…

Leave a Reply

Your email address will not be published. Required fields are marked *