Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Change Hostname and Hosts File in Linux

Published

LinuxSystem Administration

Managing hostnames and the hosts file is a core administrative responsibility in Linux environments. These settings influence how your system identifies itself on networks, resolves IP addresses locally, and integrates with internal and external infrastructure. Understanding and properly configuring your hostname and hosts file ensures effective communication, strengthens network security, and prevents naming conflicts.

This comprehensive guide details why and when to change your hostname, how to safely update it across modern and legacy Linux distributions, and how to edit and manage the /etc/hosts file for robust local name resolution. By following these instructions and best practices, you will maintain a consistent, reliable, and secure Linux environment.

Prerequisites

  • Administrative (root or sudo) access to your Linux system
  • Familiarity with command-line tools and a text editor such as nano or vim
  • Awareness of your organization’s hostname conventions and networking requirements

What Is a Hostname?

A hostname is the unique identifier assigned to a computer on a network. It distinguishes your system from others and is used by network protocols, administrative scripts, and users for connections, logging, and monitoring. Hostnames are essential for:

  • Network discovery and communication
  • SSH and remote access
  • Service and application configuration
  • Monitoring and auditing

Hostname Types

  • Static Hostname: The persistent name set for the system, usually stored in configuration files.
  • Transient Hostname: The temporary name assigned at runtime, often changed by DHCP or other network services.
  • Pretty Hostname: An optional, descriptive name with spaces and special characters, used in graphical environments.

What Is the Hosts File?

The /etc/hosts file is a plain text file used for static hostname-to-IP address mappings. It enables local name resolution, bypassing the need for a DNS server for specified hosts. The hosts file is processed before DNS lookups, allowing you to override or supplement DNS for development, testing, or local networking.

Typical entries in /etc/hosts:

127.0.0.1   localhost
127.0.1.1   myhost
192.168.1.10 webserver.local webserver

Why Change the Hostname or Hosts File?

Changing the hostname or hosts file may be necessary when:

  • Setting up a new server or workstation
  • Integrating a system into a new network or domain
  • Renaming systems for organizational consistency
  • Testing or developing locally with custom hostnames
  • Resolving network conflicts or addressing security requirements

Checking Your Current Hostname

You can check the current hostname using several commands:

$ hostname

Displays the system’s current hostname.

$ hostnamectl

Shows static, transient, and pretty hostnames.

$ cat /etc/hostname

Displays the persistent (static) hostname stored in the configuration file.

Changing the Hostname

Using hostnamectl (Recommended for Modern Distributions)

Most current Linux distributions (Ubuntu 16.04+, CentOS 7+, Debian 9+, Fedora 15+) use systemd, which provides the hostnamectl utility for hostname management.

Set a new hostname:

$ sudo hostnamectl set-hostname new-hostname

Replace new-hostname with your desired hostname. This approach updates both the static and transient hostnames and persists across reboots.

Optional: Set a pretty (descriptive) hostname:

$ sudo hostnamectl set-hostname "My Server" --pretty

Verify the change:

$ hostnamectl
$ hostname

[!NOTE] Hostname changes take effect immediately but may require a restart of some services or a system reboot for all applications to reflect the update.

Using the hostname Command (Temporary Change)

For a temporary change valid until the next reboot:

$ sudo hostname temporary-hostname

This command changes the transient hostname. The static hostname remains unchanged, and the system reverts to the original name after a reboot.

Changing the Hostname via Configuration Files (Legacy Method)

On older systems or when fine-tuning is required, edit the relevant configuration files directly.

Ubuntu/Debian

Edit /etc/hostname:
$ sudo nano /etc/hostname
Replace the current name with your new hostname.

Edit /etc/hosts:
$ sudo nano /etc/hosts
Update the line beginning with 127.0.1.1 to reflect the new hostname:
127.0.1.1   new-hostname

Save and close the file.

Apply the change:

$ sudo systemctl restart systemd-logind
or reboot:
$ sudo reboot

CentOS/RHEL

Depending on your version, update the /etc/hostname file or the network configuration file:

Edit /etc/hostname:
$ sudo nano /etc/hostname
Update the file with the new hostname.

For older versions, edit /etc/sysconfig/network:
$ sudo nano /etc/sysconfig/network
Modify or add the line:
HOSTNAME=new-hostname

Update /etc/hosts as described above. Restart the system or network services for changes to take effect.

Editing the Hosts File

The /etc/hosts file lets you map hostnames to IP addresses for local resolution, which is particularly useful for development, private networks, or when DNS services are unavailable.

Hosts File Format

Each line consists of an IP address followed by one or more hostnames and optional aliases:

IP_address   canonical_hostname   [aliases...]

Example Entries

127.0.0.1   localhost
127.0.1.1   myserver
192.168.1.10 webserver.local webserver
10.0.0.5   db.internal

Editing the Hosts File

Open the hosts file:

$ sudo nano /etc/hosts

Add, modify, or remove entries as needed.

  • Map new hostnames to IP addresses.
  • Update the entry for your system’s new hostname.
  • Add aliases for easy reference.

Save and close the file.

[!WARNING] Do not remove or alter the 127.0.0.1 localhost entry-it is critical for system processes.

Applying and Testing Changes

After changing the hostname and hosts file:

Verify the hostname:

$ hostname
$ hostnamectl

Test local name resolution:

$ ping new-hostname

or test alias resolution:

$ ping webserver

Use getent to check resolution through all name services:

$ getent hosts new-hostname

For networked systems, ensure DNS and other hostname-related services reflect the new name, especially if your system participates in Active Directory, LDAP, or other centralized authentication. If you modified the hosts file for services like Apache or Nginx, restart those services to apply changes.

Hostname and Hosts File Best Practices

  • Choose Hostnames Carefully: Use informative, consistent names that follow organizational standards. Avoid special characters and spaces. Most hostnames support only alphanumeric characters and hyphens (-).
  • Keep the Hosts File Organized: Use comments (lines starting with #) to clarify purpose and ownership of entries. Group related entries together.
  • Document Changes: Track hostname and hosts file changes in your configuration management or documentation system for auditing and troubleshooting.
  • Automate Where Possible: Use tools such as Ansible, Puppet, or Chef to manage hostnames and hosts files across multiple systems, reducing human error.
  • Restart Services as Needed: Some applications cache the hostname or hosts entries on startup. Restart network, web, or database services after making changes.
  • Monitor for Conflicts: Ensure no two systems on the same network share the same hostname. Duplicate hostnames can cause connectivity and security issues.

Handling Hostname Changes in Networked and Cloud Environments

Many Linux systems run in environments where the hostname is managed externally by cloud platforms, DHCP servers, or orchestration tools (such as Kubernetes or OpenStack).

Cloud Instances: Use your provider’s interface or metadata API to change hostnames, as manual changes may be overwritten on reboot.

DHCP-Assigned Hostnames: The DHCP server may set the transient hostname at boot. To persist your static hostname, ensure your network configuration disables hostname updates via DHCP.

Configuration Management: Integrate hostname and hosts file changes into automated deployment scripts to maintain consistency across large fleets of servers.

Troubleshooting Common Issues

Hostname Change Not Persisting

Confirm you edited the correct configuration file for your distribution. Make sure no external tools (cloud-init, DHCP, etc.) are overwriting your changes at boot. Use hostnamectl on modern systems for persistence.

Name Resolution Fails

Check /etc/hosts for correct syntax and absence of typos. Ensure the relevant entry exists for your hostname and critical services. Test with ping, getent, or nslookup to diagnose.

SSH or Service Access Issues

Restart SSH, web, or other network services that rely on hostname or hosts file. Check that your firewall or SELinux/AppArmor policies allow connections to the new hostname.

Conclusion

Changing the hostname and maintaining the /etc/hosts file are key administrative skills for any Linux user or system administrator. These tasks affect how your system presents itself on a network, how it locates resources, and how services function in distributed environments. By following the detailed steps in this guide, you ensure that changes are applied safely and consistently, avoid common pitfalls, and support efficient, secure system operations.

[!NOTE] For complex scenarios-such as systems managed by orchestration tools or integrated into enterprise networks-consult your organization’s documentation or platform-specific guidance before making manual changes.

Related guides

Account services are unavailable until the production backend is configured.