CentOS/RHEL 6/7 手動設定靜態網路參數的標準流程,適用於伺服器環境部署。
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…vi /etc/resolv.conf
加入:
nameserver 8.8.8.8
nameserver 114.114.114.114
建議保留兩組以上 DNS,提高可用性。
service network restart
# 或
systemctl restart network
ip addr show eth0
ping -c 3 192.168.0.1
ping -c 3 8.8.8.8
ping -c 3 www.google.com
# 查看連線
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 |