Changing the hostname on a Linux system keeps machine names aligned with their roles, locations, or inventory records. Clear hostnames make it easier to identify systems in SSH prompts, monitoring dashboards, and log files, especially in multi-server environments.

On modern systemd-based distributions, hostname information is managed by systemd-hostnamed and stored primarily in /etc/hostname. The hostnamectl utility communicates with this service to set the static hostname, and optionally pretty and transient hostnames, so that shells, daemons, and network tools agree on how the system identifies itself.

The procedure assumes a systemd-based Linux distribution and access to a user account with sudo privileges. Changing a hostname can temporarily confuse services that cache names or rely on reverse DNS, so adjustments to DNS records, SSH known-hosts, and configuration management should accompany the change on production systems.

Steps to change hostname in Linux:

  1. Open a terminal.
  2. Check the current hostname.
    $ hostname
    host.example.net

    Shows the short system name used in prompts and log files.

  3. Display detailed hostname information using hostnamectl.
    $ hostnamectl status
     Static hostname: host.example.net
           Icon name: computer-container
             Chassis: container ☐
          Machine ID: dfae20ca11e742c090f9a9f100772b43
             Boot ID: aa8df42ac7eb431c9412beba3d46fae4
      Virtualization: docker
    Operating System: Ubuntu 24.04.3 LTS
              Kernel: Linux 6.12.54-linuxkit
        Architecture: arm64

    Confirms that systemd manages the hostname and shows the current static value.

  4. Set a new static hostname using hostnamectl.
    $ sudo hostnamectl set-hostname host.example.net --static

    Changing the hostname on a production system without updating DNS, monitoring, and configuration management can cause failed name lookups and confusing log entries.

    Some container runtimes block static hostname changes; use a VM or host system if hostnamectl reports a static hostname error.

  5. Confirm that the running session reports the new hostname.
    $ hostname
    host.example.net
  6. Verify that the new hostname is stored in /etc/hostname.
    $ cat /etc/hostname
    host.example.net

    An incorrect entry in /etc/hostname can cause inconsistent prompts or service identification after reboot.

  7. Open a new terminal session to ensure prompts and logins display the updated hostname.
    $ hostname
    host.example.net

    Opening a fresh session ensures remote shells, SSH connections, and login banners show the new system name.