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
    linux-lab

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

  3. Display detailed hostname information using hostnamectl.
    $ hostnamectl status
     Static hostname: linux-lab
           Icon name: computer-container
             Chassis: container
          Machine ID: b7197b121c7b43be81a7c8a9fd9a6ce0
             Boot ID: 9a994286288f466ca3c6c29b344f450e
      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 linux-lab.example.net
    Could not set static hostname: Failed to set static hostname: Device or resource busy

    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
    linux-lab
  6. Verify that the new hostname is stored in /etc/hostname.
    $ cat /etc/hostname
    linux-lab

    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
    linux-lab

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