首页主机资讯centos hostname设置步骤

centos hostname设置步骤

时间2025-10-01 00:26:03发布访客分类主机资讯浏览1022
导读:CentOS Hostname Setting Steps Hostname is a critical identifier for Linux systems, used by applications and services to...

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 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
    

CentOS 7/8

  • 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
    

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/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.

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: centos hostname设置步骤
本文地址: https://pptw.com/jishu/714990.html
centos hostname错误怎么办 CentOS中如何配置Compton以支持多显示器

游客 回复需填写必要信息