Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS 手動配置靜態 IP、DNS、網關完整步驟

編輯:Linux 教學

CentOS/RHEL 6/7 手動設定靜態網路參數的標準流程,適用於伺服器環境部署。

1. 編輯網卡設定檔

vi /etc/sysconfig/network-scripts/ifcfg-eth0

關鍵參數:

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.77
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
  • BOOTPROTO=static:指定靜態 IP(預設為 dhcp)。
  • 多張網卡分別編輯 ifcfg-eth1、ifcfg-eth2…

2. 設定 DNS 解析

vi /etc/resolv.conf

加入:

nameserver 8.8.8.8
nameserver 114.114.114.114

建議保留兩組以上 DNS,提高可用性。

3. 重啟網路服務生效

service network restart
# 或
systemctl restart network

4. 驗證連通性

ip addr show eth0
ping -c 3 192.168.0.1
ping -c 3 8.8.8.8
ping -c 3 www.google.com

CentOS 7 / RHEL 7 新工具:nmcli

# 查看連線
nmcli con show

# 修改靜態 IP
nmcli con mod "System eth0" ipv4.addresses 192.168.0.77/24
nmcli con mod "System eth0" ipv4.gateway 192.168.0.1
nmcli con mod "System eth0" ipv4.dns "8.8.8.8 114.114.114.114"
nmcli con mod "System eth0" ipv4.method manual
nmcli con up "System eth0"

nmcli 修改即時生效且持久化,推薦用於 CentOS 7+ 環境。

常見問題排查

現象可能原因檢查項目
IP 設定不生效NetworkManager 衝突停用 NM:systemctl stop NetworkManager; systemctl disable NetworkManager
無法上網網關錯誤 / 防火牆攔截iptables -L -n、route -n
DNS 不生效/etc/resolv.conf 被覆寫設定 PEERDNS=no 或用 nmcli
Copyright © Windows教程網 All Rights Reserved