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.
1. View Current Hostname
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+)
2. Temporary Hostname Change (CentOS 6/7/8)
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.
3. Permanent Hostname Change
For a lasting change, modify system configuration files and update the /etc/hosts file to avoid resolution issues.
CentOS 6
-
Modify
/etc/sysconfig/network:
Open the file with root privileges and update theHOSTNAMEline:sudo vi /etc/sysconfig/networkChange
HOSTNAME=localhost.localdomaintoHOSTNAME=new-hostname. Save and exit. -
Update
/etc/hosts:
Edit the hosts file to map the new hostname to127.0.0.1:sudo vi /etc/hostsLocate the line starting with
127.0.0.1and 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
CentOS 7/8
-
Use
hostnamectl(Recommended):
Thehostnamectlcommand simplifies permanent hostname changes by updating the static hostname and/etc/hostnameautomatically:sudo hostnamectl set-hostname new-hostnameThis command updates the static hostname (used at boot) and ensures consistency across reboots.
-
Manually Update
/etc/hostname(Optional):
For explicit control, edit the/etc/hostnamefile:sudo vi /etc/hostnameReplace the existing content with
new-hostname. Save and exit. -
Update
/etc/hosts:
CentOS 7/8 require manual updates to/etc/hoststo prevent hostname resolution errors. Add the new hostname to the127.0.0.1line:sudo vi /etc/hostsModify 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
4. Verify Hostname Change
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+)
Key Notes
- Avoid Special Characters: Hostnames should use alphanumeric characters and hyphens only (no spaces or underscores).
- Network Impact: Changing the hostname may disrupt services relying on the old name. Restart affected services after the change.
- Consistency: Always update both
/etc/hostnameand/etc/hostsfor permanent changes to avoid resolution issues.
By following these steps, you can safely configure and manage hostnames in CentOS systems of any version.
以上就是关于“centos hostname设置步骤”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm