+(91)70149-37521Subscribe Now

How to Change the Hostname in Linux

To change the hostname in Linux, use sudo hostnamectl set-hostname new-name. This updates the persistent system hostname on modern Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and other systemd-based distributions. This guide explains short names, fully qualified domain names, /etc/hosts, cloud-init, verification, and common problems. Replace web01.example.com with the name planned for your server. What […]

Linuxpanda change hostname linux

To change the hostname in Linux, use sudo hostnamectl set-hostname new-name. This updates the persistent system hostname on modern Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and other systemd-based distributions.

This guide explains short names, fully qualified domain names, /etc/hosts, cloud-init, verification, and common problems. Replace web01.example.com with the name planned for your server.

What is a Linux hostname?

A hostname identifies a computer on a network. A short hostname looks like web01. A fully qualified domain name (FQDN) includes its DNS domain, such as web01.example.com.

A hostname change does not create or update public DNS records. If clients must resolve the new name, add the correct DNS record separately.

Check the current hostname

hostnamectl
hostname
hostname --fqdn

hostnamectl shows the static, transient, and pretty names when available. hostname --fqdn depends on name resolution and may fail when DNS or /etc/hosts is not configured correctly.

Choose a valid hostname

For a DNS-compatible name, use letters, numbers, hyphens, and dots. Do not begin or end a label with a hyphen. Keep each label to 63 characters or fewer. Simple lowercase names are easier to manage consistently.

Examples:

web01
web01.example.com

Avoid spaces in the static hostname. If you want a human-friendly label with spaces, systemd supports a separate pretty hostname.

Change the hostname with hostnamectl

sudo hostnamectl set-hostname web01.example.com

This changes the static hostname and activates the new name. On a remote SSH session, the shell prompt may continue showing the old name until you sign in again.

Set only the static hostname when you want to be explicit:

sudo hostnamectl set-hostname web01.example.com --static

Set an optional descriptive pretty name:

sudo hostnamectl set-hostname "Production Web Server 01" --pretty

Update /etc/hosts

Review the local host mapping:

cat /etc/hosts

On many Debian and Ubuntu servers, a local mapping uses 127.0.1.1:

127.0.0.1 localhost
127.0.1.1 web01.example.com web01

On a server with a stable address and network design that expects it, the entry may use the server IP:

192.0.2.10 web01.example.com web01

Do not replace the standard localhost line. Do not add a public IP blindly on DHCP or cloud systems; follow the provider’s name-resolution design. Duplicate or conflicting entries can produce a wrong FQDN.

Edit the file carefully:

sudo nano /etc/hosts

Verify the new hostname

hostnamectl status
hostnamectl status --static
hostname
getent hosts web01.example.com

If the server needs public DNS, also query it from a client or trusted resolver:

dig +short web01.example.com

The result should match the intended address. A local /etc/hosts entry affects that server only; it does not prove public DNS is correct.

Do you need to reboot?

hostnamectl applies the hostname immediately. However, some applications read it only during startup. Sign out and reconnect to refresh the shell prompt. Restart a specific affected service when you know it caches the hostname, or schedule a reboot when that is safer for the application stack.

Do not restart every production service without checking its role and maintenance requirements.

Change the hostname on RHEL-based systems

The same systemd command works on current RHEL, Rocky Linux, AlmaLinux, and Fedora:

sudo hostnamectl set-hostname web01.example.com

NetworkManager also provides:

sudo nmcli general hostname web01.example.com
nmcli general hostname

Use one supported method and verify the result. Review services that depend on the hostname before rebooting or restarting them.

Change the hostname without hostnamectl

On a system without systemd, update the distribution’s persistent hostname file and use the hostname command for the running kernel. A common pattern is:

echo 'web01.example.com' | sudo tee /etc/hostname
sudo hostname web01.example.com

The exact persistent configuration differs on older distributions. Check their supported documentation rather than adding legacy network-script settings to a modern server.

Cloud-init keeps changing the hostname back

Cloud images may receive a hostname from instance metadata, DHCP, or cloud-init. If a manual change disappears after reboot, inspect:

cloud-init status
grep -R "preserve_hostname\|create_hostname_file\|hostname:" \
  /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.d 2>/dev/null

A common cloud-init setting for preserving a manually managed hostname is:

preserve_hostname: true

Provider behavior varies. For example, some Ubuntu 24.04 Google Compute Engine images may not create /etc/hostname by default and use the cloud-init create_hostname_file setting. Follow your provider’s current image documentation before changing cloud-init.

Hostname changes and important services

Changing a hostname can affect more than the prompt:

  • TLS certificates must contain the name clients use.
  • Mail servers need correct forward and reverse DNS.
  • Monitoring, backups, and inventory may identify machines by hostname.
  • Cluster members and databases may store node names.
  • Control panels and licensed software may require their own rename process.

For a basic web server, also review the web server’s virtual-host configuration. Changing the operating-system hostname does not automatically change Apache or Nginx domain settings.

Common hostname problems

sudo says “unable to resolve host”

The new hostname and /etc/hosts probably do not match. Check hostname, then ensure a suitable local mapping contains the current short name and FQDN.

The prompt still shows the old hostname

Open a new login session. Existing shells may keep the old prompt text even though hostnamectl shows the correct active value.

The hostname changes back after reboot

Check cloud-init, DHCP, NetworkManager, provider metadata, and configuration-management tools. One of them may be applying the old value during startup.

hostname –fqdn shows the wrong name

Review DNS and the order of entries in /etc/hosts. The FQDN result depends on how the current hostname resolves; it is not simply a copy of /etc/hostname.

Hostname change checklist

  1. Record the current hostname and service dependencies.
  2. Choose a valid short name or FQDN.
  3. Run hostnamectl set-hostname.
  4. Review /etc/hosts and DNS.
  5. Check cloud-init or provider metadata.
  6. Verify with hostnamectl, getent, and DNS tools.
  7. Reconnect and test important services.

For other server changes, see our guides to changing the SSH port safely and hardening a Linux server.

Frequently asked questions

What command changes the hostname permanently?

On a modern systemd distribution, use sudo hostnamectl set-hostname new-name. Check cloud-init when using a cloud image.

Should a server hostname be a domain name?

It can use a short name or FQDN, depending on the network and application. Mail, identity, and cluster systems often require a correctly resolvable FQDN.

Does changing the hostname change the IP address?

No. Hostnames and IP configuration are separate. Update DNS if the new name must point to the existing address.

Will changing the hostname disconnect SSH?

The active session usually remains connected. The prompt and host-key entry used by your client may need attention when you begin connecting with a different DNS name.

Official references

You now know how to change the hostname in Linux and verify that it survives correctly. Remember that the system hostname, local host mapping, public DNS, and cloud metadata are related settings but are not the same thing.

Comments

Leave a Reply

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

Subscribe to Our Newsletter

Get free how-to tutorials and over 700+ courses. Seo tips, create a wordpress, or learn a new skill.