Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Set or Change Hostname in CentOS 7?
The hostname plays a crucial role in identifying a server on a network and generating the fully qualified domain name (FQDN) for the system. In CentOS 7, changing the hostname involves modifying key system files and applying the changes properly.
This process requires updating the /etc/hostname file and the /etc/hosts file to ensure the system recognizes the new identifier. After making these changes, a system reboot ensures they take effect across all services and networking components.
Step 1 Check the Current Hostname
Before making changes, verify the current hostname by opening a terminal and running the following command
hostname
This displays the current hostname of the system
[root@server ~]# hostname server.tutorialpoint.com
You can also use hostnamectl to get detailed hostname information
hostnamectl status
Step 2 Change the Hostname File
Edit the hostname file located at /etc/hostname using a text editor. This file contains the current hostname of the system
sudo vi /etc/hostname
Replace the existing hostname with your desired new hostname. For example, to change from server.tutorialpoint.com to webserver.tutorialpoint.com
webserver.tutorialpoint.com
Save and exit the file by pressing Esc, then typing :wq and pressing Enter.
Alternative Method Using hostnamectl
CentOS 7 provides the hostnamectl command as a modern alternative
sudo hostnamectl set-hostname webserver.tutorialpoint.com
Step 3 Update the Hosts File
The /etc/hosts file maps hostnames to IP addresses and must be updated to include the new hostname
sudo vi /etc/hosts
Locate the line containing 127.0.0.1 and add your new hostname. The updated line should look like this
127.0.0.1 localhost.localdomain localhost webserver.tutorialpoint.com
This ensures the system can properly resolve the new hostname to its IP address, especially important in networked environments.
Step 4 Apply Changes
To apply the hostname changes without rebooting, restart the systemd-hostnamed service
sudo systemctl restart systemd-hostnamed
For a complete system restart, use
sudo reboot
Verification
After the changes are applied, verify the new hostname
hostname
Expected output
[root@webserver ~]# hostname webserver.tutorialpoint.com
Use hostnamectl for detailed verification
hostnamectl status
Key Points
Persistent Changes Modifying
/etc/hostnameensures the hostname persists across reboots.Network Resolution Updating
/etc/hostsallows proper hostname-to-IP mapping.Modern Tools The
hostnamectlcommand provides a streamlined approach for CentOS 7.Service Dependencies Some services may need restart to recognize the new hostname.
Conclusion
Changing the hostname in CentOS 7 is straightforward and involves updating the /etc/hostname and /etc/hosts files. Using modern tools like hostnamectl simplifies this process further. A properly configured hostname improves server management, prevents network conflicts, and enhances system security by providing clear server identification.
