掌握網路介面查看、暫時修改 IP 以及永久生效的設定方法,是 Linux 管理員的基本功。本文涵蓋 ifconfig、ip 指令與設定檔修改兩大流派。
ifconfig -a # 顯示所有介面(含 down 狀態)
ifconfig eth0 # 單一介面
ip addr show # 新式指令,功能更強
ip -br addr # 精簡輸出
# ifconfig 法
ifconfig eth0 192.168.63.27 netmask 255.255.255.0 up
route add default gw 192.168.63.1
# ip 指令法(推薦)
ip addr add 192.168.63.27/24 dev eth0
ip link set eth0 up
ip route add default via 192.168.63.1
vim /etc/sysconfig/network-scripts/ifcfg-eth0
# 靜態 IP 範例
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=114.114.114.114
套用:systemctl restart network 或 nmcli con reload && nmcli con up eth0。
echo "ifconfig eth0 192.168.63.27 netmask 255.255.255.0 up
route add default gw 192.168.63.1" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 1.1.1.1
# /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses: [192.168.1.100/24]
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
套用:netplan apply。
ping -c 3 8.8.8.8 測試連通性ip route get 8.8.8.8 確認路由選擇cat /etc/resolv.conf 檢查 DNSjournalctl -u NetworkManager 查看網路服務日誌善用 ip 指令與對應發行版的設定方式,能更精準、持久地管理網路。