CentOS Hostname Setting Steps
Hostname is a critical identifier for Linux systems, used by applications and services to recognize the machine. Below are detailed steps to configure hostnames in CentOS, categorized by version and method.
Before making changes, verify the existing hostname using one of these commands (works for all CentOS versions):
hostname # Displays the current transient hostname
hostnamectl # Shows static, transient, and pretty hostnames (CentOS 7+)
This change takes effect immediately but resets after a reboot. Use it for testing:
sudo hostname new-hostname # Replace "new-hostname" with your desired name
Verify with hostname
or hostnamectl
.
For a lasting change, modify system configuration files and update the /etc/hosts
file to avoid resolution issues.
Modify /etc/sysconfig/network
:
Open the file with root privileges and update the HOSTNAME
line:
sudo vi /etc/sysconfig/network
Change HOSTNAME=localhost.localdomain
to HOSTNAME=new-hostname
. Save and exit.
Update /etc/hosts
:
Edit the hosts file to map the new hostname to 127.0.0.1
:
sudo vi /etc/hosts
Locate the line starting with 127.0.0.1
and append the new hostname (e.g., 127.0.0.1 localhost localhost.localdomain new-hostname
). Save and exit.
Apply Changes:
Restart the network service or log out and back in:
sudo service network restart
Use hostnamectl
(Recommended):
The hostnamectl
command simplifies permanent hostname changes by updating the static hostname and /etc/hostname
automatically:
sudo hostnamectl set-hostname new-hostname
This command updates the static hostname (used at boot) and ensures consistency across reboots.
Manually Update /etc/hostname
(Optional):
For explicit control, edit the /etc/hostname
file:
sudo vi /etc/hostname
Replace the existing content with new-hostname
. Save and exit.
Update /etc/hosts
:
CentOS 7/8 require manual updates to /etc/hosts
to prevent hostname resolution errors. Add the new hostname to the 127.0.0.1
line:
sudo vi /etc/hosts
Modify the line to: 127.0.0.1 localhost localhost.localdomain new-hostname
. Save and exit.
Apply Changes:
Reboot the system to apply the new hostname permanently:
sudo reboot
After applying changes (temporary or permanent), confirm the new hostname with:
hostnamectl # Displays static, transient, and pretty hostnames
hostname # Shows the current transient hostname
cat /etc/hostname # Confirms the static hostname (CentOS 7+)
/etc/hostname
and /etc/hosts
for permanent changes to avoid resolution issues.By following these steps, you can safely configure and manage hostnames in CentOS systems of any version.