Mastering Hostname Changes in Linux

If you‘re a Linux system administrator, being able to smoothly change the hostname on a server is one of those essential skills you‘ll use all the time.

Whether you‘re setting up new infrastructure, migrating systems, or just fixing some minor configuration errors, knowing the ins and outs of updating hostnames on Linux will save you hours of effort down the road.

In this comprehensive guide, I‘ll provide everything you need to know to be a hostname changing expert on any Linux machine. You‘ll learn:

  • Key background on the Linux hostname resolution process
  • Step-by-step walkthroughs for changing hostnames across all distros
  • Advanced considerations like automation scripts and DNS configuration
  • Tips and best practices picked up from years as a Linux admin

And plenty more about this critical sysadmin skill! Let‘s get started.

An Introduction to Linux Hostnames

Before we jump straight to the steps for changing your server‘s identity, it‘s important to level-set on what hostnames mean in a Linux environment.

At a basic level, the hostname is just a unique name assigned to identify an individual machine on a network.

But there‘s more to it than that simple definition when we dig into the Linux network stack.

The Role of a Hostname

First and foremost – your hostname allows you to provide systems on your network with friendly, recognizable names rather than just relying on more complex IP addresses everywhere.

It maps a simple label like webserver1 to coords like a address.

But more importantly, a hostname gives context and meaning about a server‘s purpose and role – rather than an opaque IP which tells you nothing.

Those descriptive names are essential as you build out complex environments with dozens or hundreds of systems.

Hostname Resolution Process

When you make a request to connect to a hostname like ftpserver, here is what happens under the hood:

  1. Client queries the DNS to resolve ftpserver to an IP
  2. DNS server checks hostname records and returns the IP address
  3. Initial TCP handshake connects client to server IP
  4. Client includes intended hostname in request
  5. Server validates client intent and completes connection

linux hostname resolution process

Having this hostname resolution flow allows connection flexibility but also tighter access controls on the server side.

We can see hostnames play an important role in the Linux networking stack. Now let‘s look at why we‘d need to change them.

Common Reasons to Change the Hostname

Over the lifetime of a Linux server, any number of scenarios may come up that require changing the hostname to something new.

Here are some of the most common cases:

  • The hostname was misconfigured during provisioning
  • You need to rename servers to match an organizational naming convention
  • Hostnames occasionally need regular changes for security purposes
  • Mergers, splits, reorgs, etc lead to infrastructure reshuffling

In my experience, I‘ve needed to rename systems after accidents like typos, but also frequently when standing up new environments.

Getting hostname schemes rationalized early on saves headaches with scale.

Now that you know why hostnames matter and see the situations for changing them, let‘s dig into the actual process for updating the name.

Step-by-Step Guide to Changing the Hostname

While Linux offers the hostname and hostnamectl commands for simple hostname changes, that only updates the transient name.

To permanently alter the system‘s identity, we have to dive deeper and modify the underlying Linux configuration files.

Follow this reliable 4 step process to ensure your new hostname sticks:

Step 1 – Update /etc/hostname

The first file to modify is the dedicated hostname config file. Simply open it up in your preferred editor:

sudo vim /etc/hostname 

And replace the existing name with your new desired hostname.

For example, changing from webserver1:


Save the update and you‘ve revised the fundamental hostname record.

Step 2 – Modify /etc/hosts

The next file to tackle is the hosts mapping.

This binds IP addresses to their corresponding hostnames for name resolution. Open up the file:

sudo vim /etc/hosts

You‘ll want to replace any references to the old name with the new hostname. webserver2   webserver2

Be thorough – the search domain references or localhost mapping may also have the old hostname configured.

Step 3 – Distro-Specific Files

Some Linux distros have additional hostname files that should get updated.

RHEL/CentOS systems store the hostname in /etc/sysconfig/network. Edit it to make your change:

sudo vim /etc/sysconfig/network


For Ubuntu/Debian the /etc/cloud/cloud.cfg file controls the assigned name.

And various other distros have their own specific config files as well. Consult your documentation.

Step 4 – Apply Hostname Change

With the underlying Linux configuration updated to your new hostname, we actually need to apply those lower-level changes to take effect immediately.

Use the hostnamectl command to set the live hostname:

sudo hostnamectl set-hostname webserver2

You may also need to restart networking and other services to have the name propagate correctly:

sudo systemctl restart networking dhcpcd
sudo systemctl restart sshd 

Now you should see your new hostname active on the command line and in request headers.

But we still have some additional steps to ensure it persists properly after reboots.

Confirming Hostname Changes Post-Reboot

Once you modify all the appropriate configuration files and set your new hostname with hostnamectl, you‘ll want to double check that it sticks after a system restart.

Changes to the transient name won‘t always persist past a reboot if the underlying config still has the old value.

After an initial restart, verify your prompt reflects the updated name:


And check it resolves in name lookups:

ping webserver2

You may even want to do a second reboot just to be absolutely sure the change holds.

Getting the hostname update to persist past restarts is critical.

Automating Hostname Changes at Scale

When you‘re changing hostnames across dozens or hundreds of Linux systems, doing these steps manually is tedious and error-prone.

That‘s where automation scripts come in handy.

Here is an example bash script that takes a hostname argument and updates all the required config files automatically:


# Set new hostname from argument

# Update config files
echo $NEW_HOSTNAME > /etc/hostname  
sed -i "s/.*/$NEW_HOSTNAME/" /etc/hosts

# Refresh hostname
hostnamectl set-hostname $NEW_HOSTNAME
systemctl restart networking

Running this on each server will standardize the updates.

You can enhance it further by adding validation checks, OS detection, reporting, and more robust error handling.

But even in its simple form, automation empowers you to smoothly rename 100+ systems.

Additional Hostname Change Considerations

We‘ve covered the core process for configuring new hostnames on Linux. But there are some other considerations when changing system names.

A few important ones to be aware of:

  • FQDNs – The full DNS names also need to be updated in addition to short hostnames. This ensures proper name resolution.
  • Application configs – Many apps reference the system hostname. Be prepared to restart them or propagate the change.
  • Naming conventions – Establish a clear hostname scheme early on to allow for controlled name changes.
  • Access management – Changes may require updating access controls, like trusted subnets in SSH configs that reference old names.

And there are likely a number of other dependency updates to make like load balancer pools, databases, scripts, etc.

The key is understanding the implications of a change across all integrated systems.

Mastering the Hostname Change Process

We‘ve covered a ton of ground around the hostname change process in Linux here.

Here‘s a quick summary of what we learned:

  • Hostnames provide recognizable labels for systems and contextual meaning
  • Changes are needed for misconfigurations, migrations, consolidations etc
  • Updating /etc/hostname and /etc/hosts anchors the new name
  • hostnamectl set-hostname applies updates immediately
  • Reboots and service restarts confirm persistence
  • Script all the things for large scale changes!

Hopefully you now feel armed with the knowledge to smoothly handle Linux hostname changes in your environment.

Whether you‘re working on a single machine or an enterprise Linux infrastructure, this critical sysadmin skill will serve you well.

Let me know if you have any other hostname change questions!