VMware ESXi and vSphere Cluster Management

Configure the Hostname on Linux

Learn how to view, temporarily change, and permanently configure a Linux hostname, including /etc/hostname, /etc/hosts, hostnamectl, and verification.

What a hostname is

A hostname is the name assigned to a Linux machine. Linux uses it in shell prompts, system logs, network identification, and local name resolution. For example, a prompt such as admin@old-host:~$ displays the system hostname as old-host.

A short hostname is a simple local name such as web-01. A fully qualified domain name (FQDN) includes a domain, such as web-01.example.com. An FQDN is useful when the machine participates in a domain and the corresponding DNS or local name-resolution configuration exists.

Use a conventional hostname made from letters, digits, and hyphens. Avoid spaces, underscores, and unusual special characters. Lowercase names such as database-01 are a practical choice. A hostname should not depend on a name that your network cannot resolve.

Check the current hostname

To display the hostname currently active in the running system, use:

hostname

To inspect the traditional persistent hostname file, use:

cat /etc/hostname

On a system using systemd hostname management, hostnamectl status displays the static, transient, and, when configured, pretty hostname, along with related operating-system information.

hostnamectl status

A static hostname is the persistent hostname configured for the system. A transient hostname is a runtime name that may be supplied by the network or changed temporarily. A pretty hostname is an optional human-readable label supported by some systemd-based systems.

Temporary versus permanent hostname changes

Method | Takes effect immediately | Survives reboot | Typical use case

sudo hostname new-hostname | Yes | No, normally | A short-lived test or runtime change

sudo hostnamectl set-hostname new-hostname | Yes | Yes | Persistent configuration on systemd-based systems

Editing /etc/hostname | After the setting is applied or rebooted | Yes | Traditional Debian/Ubuntu-style configuration

Change the hostname temporarily

To change only the hostname of the running system, run:

sudo hostname new-hostname

The new name becomes active immediately, so a new shell prompt or a subsequent hostname command may show it. This command does not necessarily update the persistent configuration. The temporary name is normally lost after a reboot, or when a hostname service reapplies its stored configuration.

Use a temporary change when testing software or checking how an application responds to a different machine name. Do not use it as the only step when the hostname must remain changed.

Configure a permanent hostname

Traditional Debian and Ubuntu-style configuration

On traditional Debian/Ubuntu-style installations, the persistent local hostname is stored in /etc/hostname. Open the file with a text editor as an administrator. For example:

sudo nano /etc/hostname

Replace the existing value with the intended hostname:

new-hostname

Save the file and exit the editor. It should contain one hostname value, normally followed by a newline, rather than several unrelated names or shell commands.

Systemd-based configuration with hostnamectl

Modern Linux systems commonly provide hostnamectl, a systemd utility for viewing and setting hostname properties. To set the persistent static hostname:

sudo hostnamectl set-hostname new-hostname

This usually updates the active hostname and the system's persistent hostname configuration. Verify the result rather than assuming every distribution stores or applies the setting in exactly the same way:

hostnamectl status
cat /etc/hostname

Some systems also support a pretty hostname. It is intended for human-readable display and is separate from the short hostname used by many network tools. Set one only when you understand how the local system uses it.

Update /etc/hosts

/etc/hosts is a local static mapping between IP addresses and names. The local hostname entry should agree with the new value in /etc/hostname or the static hostname reported by hostnamectl. Keeping these values consistent prevents programs from failing when they try to resolve the machine's own name.

On Debian-derived systems, a common file looks like this:

127.0.0.1 localhost
127.0.1.1 new-hostname

127.0.0.1 is a loopback address commonly assigned to localhost. A loopback address refers back to the local system. Debian-derived systems commonly use 127.0.1.1 for the machine's own hostname, although other distributions and configurations may use a different arrangement.

Edit the file as an administrator:

sudo nano /etc/hosts

Replace the old local hostname on the appropriate 127.0.1.1 line, while preserving the localhost mapping:

127.0.0.1 localhost
127.0.1.1 new-hostname

Do not accidentally remove or rename localhost. If the old line contains aliases, preserve only the aliases that are still correct for your system.

Hostname-related files and commands

Item | Purpose | When to use it | Important caution

hostname | Displays or changes the active runtime name | Checking the current name or making a temporary change | A change made with this command normally is not persistent

hostnamectl status | Reports hostname properties and system information | Inspecting systemd-managed hostname state | Availability depends on the operating system and init system

hostnamectl set-hostname | Sets a persistent static hostname on systemd-based systems | Applying a permanent change through the supported utility | Verify the resulting files and status

/etc/hostname | Stores the traditional persistent local hostname | Editing file-based Debian/Ubuntu-style configuration | Keep one intended hostname value in the file

/etc/hosts | Provides local hostname-to-address mappings | Making the machine's own name resolve locally | Preserve the localhost entry

Common local hosts entries

Address | Typical name | Purpose

127.0.0.1 | localhost | IPv4 loopback name used to refer to the local system

127.0.1.1 | The machine's short hostname | Common Debian-derived mapping for the local machine hostname

::1 | localhost | IPv6 loopback mapping when IPv6 is enabled

Apply and verify the change

Rebooting is the most consistent way to apply hostname-related configuration and allow boot-time services to use the new value:

sudo reboot

If a reboot is not appropriate, hostnamectl set-hostname normally applies the systemd-managed change immediately. A direct hostname command can apply only a runtime change. Avoid mixing methods without checking which service controls the hostname.

After the change, verify the active hostname:

hostname
hostnamectl status

Check both persistent files:

cat /etc/hostname
grep -E '(^|[[:space:]])(localhost|new-hostname)([[:space:]]|$)' /etc/hosts

Test local resolution with a single ping:

ping -c 1 new-hostname

A successful result indicates that the name resolved to an address and that the system received a response. If the name resolves but ping fails, the problem may be firewall or network behavior rather than hostname configuration. Another lookup tool can help separate name resolution from connectivity testing.

Distribution and environment differences

Classic file-based setup reads the hostname from /etc/hostname and commonly uses /etc/hosts for local resolution. Systemd-based setup provides hostnamectl and may distinguish static, transient, and pretty hostnames. The exact files, services, and boot behavior can vary by distribution.

Containers, cloud images, DHCP clients, virtualization platforms, cloud-init, and configuration-management systems may set or overwrite the hostname during boot. If a hostname returns to its old value, the local files may not be the only source of configuration. In a managed environment, change the hostname through the relevant management layer, image configuration, orchestration system, or provisioning settings instead of editing local files alone.

Troubleshooting

The hostname returns after reboot

You may have changed only the runtime hostname, or /etc/hostname may still contain the old value. Inspect both sources:

hostname
hostnamectl status
cat /etc/hostname

If they are correct before reboot but the old name returns afterward, investigate cloud-init, DHCP, container startup settings, virtualization configuration, or configuration-management policies.

The machine hostname cannot be resolved locally

Compare the active hostname and the two local configuration locations:

hostname
cat /etc/hostname
grep -E '127\.0\.1\.1|localhost' /etc/hosts

Correct the hostname on the appropriate 127.0.1.1 entry and preserve the 127.0.0.1 localhost mapping.

ping does not work as expected

First determine whether the name is present in /etc/hosts. If it is not, the system may be trying DNS, which requires a DNS record and suitable resolver configuration. If the name resolves but no response arrives, check network connectivity and firewall rules. A ping failure alone does not prove that the hostname is configured incorrectly.

The hostname contains spaces or unusual characters

Use a short lowercase name containing letters, digits, and hyphens, such as app-server-01. Use an FQDN such as app-server-01.example.com only when the domain and name-resolution configuration support it.

Practical examples

Temporary change from old-host to new-host

hostname
sudo hostname new-host
hostname

The final command should show new-host during the current runtime. This change is not a substitute for updating persistent configuration.

Permanent change on a Debian/Ubuntu-style system

sudo nano /etc/hostname
sudo nano /etc/hosts
sudo reboot

Put new-host alone in /etc/hostname. In /etc/hosts, retain the localhost line and change the local machine mapping to 127.0.1.1 new-host. After the reboot, run hostname and ping -c 1 new-host.

Persistent systemd workflow

sudo hostnamectl set-hostname new-host
hostnamectl status
cat /etc/hostname
ping -c 1 new-host

This workflow uses the system's hostname management utility and then verifies both reported state and local resolution.

Key exam notes

  • Hostname: the name assigned to a Linux machine.
  • Short hostname: a local name such as web-01; FQDN: a complete name such as web-01.example.com.
  • hostname changes or displays the active runtime value; a direct change normally does not survive reboot.
  • /etc/hostname traditionally stores the persistent hostname.
  • hostnamectl is the usual systemd utility for inspecting and setting hostname properties.
  • /etc/hosts should contain a matching local hostname mapping, commonly on 127.0.1.1 on Debian-derived systems.
  • Never remove the localhost mapping accidentally.
  • Managed cloud, container, DHCP, and provisioning environments can override local hostname settings.

For the related procedure, see Configure Hostname.