How to Change a Hostname on Ubuntu 24.04

Updated on 16 July, 2025
Learn how to check and permanently change the hostname on Ubuntu 24.04 using hostnamectl or configuration files.
How to Change a Hostname on Ubuntu 24.04  header image

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:

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.

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.

Comments

No comments yet.