Change Hostname on CentOS 7 Linux
Enjoying this content? Subscribe to the Channel!
Mastering Linux: How to Change Your Hostname on CentOS 7 (Step-by-Step Guide)
Welcome back to Darren’s Tech Tutorials!
If you’re managing a CentOS 7 server, one of the most fundamental tasks for proper network identification is setting a clear, descriptive hostname. Whether you’re setting up a new server, moving a machine to a new location, or simply need to rename an existing instance, knowing where and how to make this change is essential for clean system administration.
This comprehensive guide takes you through the exact process shown in our video tutorial. We’ll show you exactly which configuration file to edit, how to save your changes, and the necessary step to ensure your new hostname takes effect immediately.
Let’s dive right in and get your CentOS 7 server properly named!
Prerequisites
Before starting, ensure you have:
- Access to your CentOS 7 machine.
- A user account with
sudoor root privileges. - A text editor installed (we will use
viin this tutorial, butnanoworks just as well).
Step 1: Locating the Hostname Configuration File
In modern Linux distributions like CentOS 7, the system hostname is managed through a specific, simple configuration file. This is where we need to make our change.
The file we are looking for is /etc/hostname.
To begin editing this file, use your preferred text editor with superuser privileges:
sudo vi /etc/hostname
Step 2: Changing and Saving the New Hostname
Once you open the /etc/hostname file, you will likely see the existing hostname listed on the first line.
Action: Delete the existing entry and replace it with your desired new hostname.
-
Example: If your current hostname is
localhost.localdomain, and you want to rename it tocentral-server-01, your file should look like this after editing:central-server-01
Important Note: Hostnames should typically be alphanumeric, use hyphens instead of underscores, and avoid special characters.
Once you have entered the new hostname, save and exit the file (if using vi, press Esc, then type :wq and press Enter).
Step 3: Applying the Change by Rebooting CentOS 7
While some modern Linux systems allow for the hostname change to be applied dynamically using the hostnamectl command, the most reliable and foolproof method, especially after directly editing the core configuration file, is a full system reboot. This ensures the new hostname is correctly loaded by all dependent services upon startup.
Execute the following command to reboot your system:
sudo reboot
The system will shut down and restart. Give it a few moments to come back online.
Step 4: Verifying the New Hostname
Once your CentOS 7 system has successfully rebooted and you log back in, the final (and most satisfying!) step is to verify that the hostname change has taken effect.
You can check the new hostname using the simple hostname command, or the more detailed hostnamectl command:
Option A: Using the hostname command
hostname
If successful, the output will immediately show your newly configured name:
central-server-01
Option B: Using the hostnamectl command
This command provides a comprehensive view of the system’s configuration:
hostnamectl
Look for the Static hostname: field. It should reflect the name you entered in Step 2.
Congratulations! Your CentOS 7 server now has a fully updated and correctly configured hostname, ready for seamless network interaction.
Summary and Next Steps
Updating system configuration files, saving your work, and verifying the result is the bread and butter of Linux administration—and you just mastered a fundamental skill! Changing the hostname on CentOS 7 is quick and easy once you know the exact file location (/etc/hostname).
We hope this tutorial made this technical process clear and accessible.
If you found this guide helpful, please Like this post and Subscribe to Darren’s Tech Tutorials on YouTube! We release new, practical technology guides every week to help you manage your systems better.
Happy learning!
P.S. Don’t forget to grab your Free Linux Cheat Sheet to keep essential commands at your fingertips!