Ever seen your server show up with a confusing or generic name and wondered how to make it your own? Updating your CentOS 7 hostname isn’t just about appearances—it helps with organization, easier network management, and avoiding confusion when working with multiple systems.

This article walks you through the simple steps to change your CentOS 7 hostname, highlights the reasons behind each step, and offers practical tips to ensure a smooth process. Let’s get started!

Related Video

How to Change the Hostname in CentOS 7: A Step-by-Step Guide

Changing the hostname on CentOS 7 is a straightforward task, yet it’s crucial for both identification and efficient management of your server or system. This guide breaks down the process into easy-to-follow steps, explains different methods, provides practical tips, and covers essential best practices. By the end, you’ll know exactly how to set or update your CentOS 7 hostname without needing to reboot your machine.


What Is a Hostname and Why Change It?

A hostname is the unique name assigned to a computer or device on a network. It helps you and your network easily identify which machine is which.

You might want to change your hostname for several reasons:


How to Set or Change Hostname in CentOS and Rocky Linux - phoenixNAP - change hostname in centos 7

  • To give your server a more descriptive name.
  • To meet specific naming conventions within your organization.
  • To avoid conflicts after cloning virtual machines.
  • For better clarity in monitoring and logging.

Now, let’s walk through how you can set or change it safely in CentOS 7.


Methods to Change the Hostname in CentOS 7

There are multiple ways you can change the hostname on CentOS 7. The operating system supports a dynamic (transient), static (persistent), and pretty (display) hostname. Here, we’ll focus on setting the static hostname, as it’s the most commonly needed and ensures your change persists after reboots.

1. Using the hostnamectl Command (Recommended)

CentOS 7 introduced hostnamectl—a powerful tool designed to make managing hostnames easy.

Steps to Change the Hostname:

  1. Check the Current Hostname

Open your terminal and type:
hostnamectl status
This will show your current static, transient, and pretty hostnames.

  1. Set a New Hostname

Replace new-hostname with the name you want:
sudo hostnamectl set-hostname new-hostname
– The change applies instantly.
– No system reboot required.


How To Set Or Change A Hostname In CentOS 7 - SysAdminXpert - change hostname in centos 7

  1. Verify the Change

Run:
hostnamectl status
or just
hostname
Both should now display your new hostname.

Tips:

  • Hostnames should be short, memorable, and devoid of special characters except hyphens (-).
  • Avoid names that conflict with other machines on your network.

2. Editing the /etc/hostname File Manually

If you prefer manual file editing, you can update the configuration file directly.

Steps:

  1. Open the Hostname File in an Editor
    sudo vi /etc/hostname
    or
    sudo nano /etc/hostname

  2. Replace the Existing Hostname

Delete the old name, type your new hostname, save, and exit the editor.

  1. Apply the Change

To avoid rebooting, either log out and back in or run:
sudo hostnamectl set-hostname new-hostname
In some cases, you may need to manually run:
sudo systemctl restart systemd-hostnamed


3. Modifying the /etc/hosts File (Recommended Additional Step)

This step is often overlooked but highly recommended. The /etc/hosts file maps hostnames to IP addresses, helping various services resolve local hostnames.

Steps:

  1. Edit the Hosts File
    sudo vi /etc/hosts

  2. Update the Relevant Line

Change any lines referring to your old hostname to your new one. For example:
127.0.0.1 localhost new-hostname
::1 localhost new-hostname

This step ensures all local services recognize the updated hostname.


4. Using nmtui (Text User Interface)

If you prefer an interactive, menu-driven interface, nmtui is handy.

Steps:

  1. Start the Tool
    sudo nmtui

  2. Navigate to ‘Set Hostname’

Enter your new hostname, confirm changes, and exit.

  1. Restart the Systemd Hostnamed Service
    sudo systemctl restart systemd-hostnamed

5. Rebooting the System (If Needed)

Most methods above do not require a reboot, particularly when using hostnamectl. However, after making changes, especially via manual editing, a reboot or restarting relevant services ensures all applications recognize the new hostname.

  • To reboot:
    sudo reboot

Best Practices and Pro Tips

  • Consistency: After changing the hostname, keep /etc/hostname and /etc/hosts in sync for reliable local resolution.
  • Documentation: Log every change in your organization’s admin notes. This helps track why and when hostnames were altered.
  • DNS Registration: If your server is registered in a DNS system, update the records to reflect the new hostname.
  • FQDN Consideration: For some enterprise environments, you may want to use a fully qualified domain name (FQDN), like server.example.com. This is especially important for mail servers and web hosts.
  • Length and Characters: Keep hostnames under 63 characters, using only letters, digits, and hyphens. No spaces or special symbols.

Troubleshooting Common Challenges

Changing a hostname can sometimes create issues if certain steps are skipped or if dependant services rely on the old hostname.

  • Issue: Local services fail to recognize the new hostname.

Solution: Confirm /etc/hosts was updated, and restart or reload affected services.

  • Issue: Remote clients can’t resolve the server by its new hostname.

Solution: Check your DNS records and update them globally.

  • Issue: Applications or scripts hardcoded with the old hostname may break.

Solution: Identify such dependencies ahead of time and update configurations accordingly.


Cost Considerations

Fortunately, changing a hostname in CentOS 7 is free—no licensing costs or additional software required.

  • Zero Software Costs: All tools mentioned (hostnamectl, nmtui, text editors) ship with CentOS by default.
  • No Downtime Required: In most cases, no system reboot is necessary, keeping critical services online and operational.
  • Shipping/Service Costs: Not applicable, as this is a local configuration change.

Practical Workflow Example

Let’s say you’re setting up a new server initially named localhost, and you want to change it to webserver01.

  1. Run:
    sudo hostnamectl set-hostname webserver01
  2. Edit /etc/hosts and make sure it looks something like:
    127.0.0.1 localhost webserver01
  3. Confirm with:
    hostnamectl
    or just
    hostname

You’re done! This is all you need for most environments.


Summary

Changing the hostname in CentOS 7 is a simple yet important administrative task. While the hostnamectl command is the easiest and safest tool, alternatives like manual file editing and nmtui are available. Don’t forget to update /etc/hosts and, if necessary, DNS records to avoid confusion or connectivity issues. With these clear steps and best practices, you can confidently manage hostnames on your CentOS 7 systems without downtime or hassle.


Frequently Asked Questions (FAQs)

1. Can I change the hostname in CentOS 7 without rebooting?
Yes! The hostnamectl command applies the new hostname immediately without needing a system reboot.


2. What is the difference between static, transient, and pretty hostnames?
– Static: Persistent and set in configuration files; survives reboots.
– Transient: Temporary, typically assigned at boot; may change with network events.
– Pretty: A more “human-readable” display name; optional for most setups.


3. Do I need to update the /etc/hosts file after changing the hostname?
It’s strongly recommended. Updating /etc/hosts ensures services and scripts on your system resolve the correct hostname locally, which can prevent various errors.


4. Should I use a Fully Qualified Domain Name (FQDN) as my hostname?
If your server will be accessed via DNS, especially for mail or web hosting, using the FQDN is best. For internal servers or testing, a simple name is sufficient.


5. How can I revert to the old hostname if needed?
Simply repeat the change process, replacing the current hostname with your previous one using hostnamectl, edit /etc/hosts, and verify the update as described in the steps above.


With these insights and step-by-step methods, managing your CentOS 7 hostnames is now effortless and safe.