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 Change or Set Hostname on CentOS 8/RHEL 8?
In computer networking, a hostname is a human-readable label used to identify devices connected to a network. Instead of remembering complex IP addresses, hostnames provide an intuitive way to access and communicate with network devices, making network administration and resource sharing much more manageable.
Hostnames are essential for network communication because devices use them to identify themselves during communication. This simplifies network operations and makes it easier for users to connect to services and resources across the network.
Checking the Current Hostname
Before modifying the hostname, you should first check the current configuration. On CentOS 8/RHEL 8, use the hostnamectl command to display comprehensive system information including the current hostname.
hostnamectl
The output displays the current hostname along with other system details such as operating system version, kernel version, and architecture
Static hostname: oldhostname Icon name: computer-vm Chassis: vm Machine ID: 4fd5f58XXXXXXXXXXXXXXXXXXXXXXXXXXXXX Boot ID: 06a7ee1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Virtualization: kvm Operating System: CentOS Linux 8 (Core) CPE OS Name: cpe:/o:centos:centos:8 Kernel: Linux 4.18.0-80.el8.x86_64 Architecture: x86-64
The "Static hostname" field shows the current permanent hostname configuration.
Changing the Hostname Temporarily
For temporary hostname changes (useful for testing or temporary network configurations), use the hostname command
sudo hostname newhostname
This change persists only until the next system reboot. The temporary hostname affects only the local system and doesn't propagate through the network, so other systems will continue using the original hostname when communicating with this device.
Note: While you can modify the /etc/hosts file to add hostname aliases, this approach can lead to inconsistencies across different systems and should be used sparingly.
Changing the Hostname Permanently
For permanent hostname changes, you need to modify system configuration files and restart the appropriate service.
Method 1: Using hostnamectl (Recommended)
The simplest way to permanently change the hostname is using the hostnamectl command
sudo hostnamectl set-hostname newhostname
This command automatically updates all necessary configuration files and applies the changes immediately.
Method 2: Manual File Editing
Step 1: Edit the /etc/hostname file
sudo vim /etc/hostname
Replace the existing hostname with your desired new hostname and save the file.
Step 2: Edit the /etc/hosts file
sudo vim /etc/hosts
Locate the line containing your old hostname and replace it with the new one. This file maps hostnames to IP addresses for local hostname resolution.
Step 3: Restart the systemd-hostnamed service
sudo systemctl restart systemd-hostnamed
Verification
Verify the hostname change using the hostnamectl command
hostnamectl
The output should display your new hostname in the "Static hostname" field.
Troubleshooting Common Issues
Permission Denied Errors
When editing system configuration files, you need root privileges. Always use sudo before commands that modify system files
sudo vi /etc/hostname sudo vi /etc/hosts
Hostname Changes Not Taking Effect
If the hostname doesn't change after following the steps
Check DNS settings Ensure your DNS configuration matches the new hostname domain.
Restart networking service Refresh network connections with
sudo systemctl restart NetworkManagerReboot the system A complete reboot ensures all services recognize the new hostname.
Network Connectivity Issues
After changing the hostname, update DNS records if the system is part of a managed network environment. Contact your network administrator to ensure proper DNS resolution for the new hostname.
Conclusion
Changing the hostname on CentOS 8/RHEL 8 can be accomplished through the hostnamectl command or by manually editing configuration files. The hostnamectl method is recommended as it handles all necessary updates automatically. Proper hostname configuration is crucial for network identification and communication between devices.
