溫馨提示×

centos hostname設置步驟

小樊
39
2025-09-24 12:08:53
欄目: 智能運維

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.

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女