Change Hostname on CentOS 7 Linux
Enjoying this content? Subscribe to the Channel!
Mastering CentOS 7: The Simple Guide to Changing Your Server’s Hostname
Hello and welcome back to Darren’s Tech Tutorials! We’re diving into a fundamental skill today that is crucial for managing any Linux server: changing the hostname on CentOS 7.
Whether you are setting up a new server, reorganizing your network, or just replacing that confusing default name assigned during installation, giving your system a meaningful hostname is essential for clear communication and management.
This tutorial walks you through the straightforward process of editing the necessary configuration file, saving your changes, and verifying that your new hostname has taken effect. Let’s get started!
Why Is Changing the Hostname Important?
A hostname is the unique identifier for your system on a network. If you have multiple servers running (e.g., a web server, a database server, and a monitoring server), giving each a distinct, descriptive name (like webserver-prod or db-primary) makes monitoring, logging, and troubleshooting infinitely easier. By default, CentOS sometimes assigns a generic name or one based on your DHCP configuration, which we definitely want to change!
Step-by-Step: Changing the Hostname on CentOS 7
We are going to use the traditional method of editing the core configuration file. Make sure you are logged in as a user with root or sudo privileges before proceeding.
## Step 1: Locate the Hostname Configuration File
The hostname is typically stored in a simple text file located in the /etc/ directory.
To edit this file, we will use a text editor like vi or nano (if installed). We recommend vi as it is generally available by default on minimal installs:
vi /etc/hostname
## Step 2: Edit and Assign the New Hostname
Once you open the file, you will likely see the old, existing hostname.
- Delete the existing hostname.
- Type your desired, new hostname on the first line of the file.
Important Note: Hostnames should typically use lowercase letters, numbers, and hyphens (-). Avoid spaces or special characters.
Example: If the file previously contained localhost.localdomain, you would replace it with:
my-new-centos-server
## Step 3: Save and Exit the File
After entering your new hostname:
- If using
vi: PressESC, type:wq, and hitEnter(Write and Quit). - If using
nano: PressCtrl+X, pressYto confirm saving, and hitEnter.
## Step 4: Apply the Changes by Rebooting CentOS
While newer Linux distributions often allow you to change the hostname instantly using the hostnamectl command without a reboot, for this specific, traditional configuration file method on CentOS 7, the simplest and most reliable way to ensure the change is permanent and globally recognized by all system services is to perform a quick reboot.
Issue the following command:
sudo reboot
Wait a minute or two for your CentOS machine to restart fully.
## Step 5: Verify the New Hostname
Once your system is back online and you log back in, it’s time to check if our change took effect.
You can quickly verify the hostname by typing the following command:
hostname
The output should display your brand-new hostname (e.g., my-new-centos-server).
Bonus Verification: For a detailed look at the static and transient hostname settings, you can use the more powerful hostnamectl utility:
hostnamectl
You should see your new hostname listed under the Static hostname: field. Congratulations—you’re done! Your CentOS 7 server is now properly identified on your network.
Summary and Next Steps
That’s all there is to it! In just five simple steps, you have successfully configured and verified a custom, meaningful hostname for your CentOS 7 system. This is an essential step toward better server management and organization.
Did this tutorial help you get your server sorted? Let me know in the comments below!
If you enjoyed this clear, practical guide, make sure you hit that Like button and Subscribe to Darren’s Tech Tutorials for more accessible Linux and tech walkthroughs every week.
And don’t forget to grab your Free Linux Cheat Sheet—it’s packed with commands that will make managing your server even easier! Click here to get your free cheat sheet!
Happy configuring, and I’ll see you in the next video!