以下七個指令涵蓋了日常 Linux 網路設定與排查的大部分場景。
設定 eth0 的 IP 並啟用該裝置:
#ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
設定別名裝置 eth0:1 並加上路由:
#ifconfig eth0 192.168.1.3
#route add -host 192.168.1.3 dev eth0:1
啟用與停用裝置:
#ifconfig eth0 up
#ifconfig eth0 down
檢視單一介面或全部介面:
#ifconfig eth0
#ifconfig
加到主機的路由:
#route add -host 192.168.1.2 dev eth0:0
#route add -host 10.20.30.148 gw 10.20.30.40
加到網路的路由:
#route add -net 10.20.30.40 netmask 255.255.255.248 eth0
#route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
#route add -net 192.168.1.0/24 eth1
新增預設閘道與檢視路由表:
#route add default gw 192.168.1.1
#route
刪除路由:
#route del -host 192.168.1.2 dev eth0:0
#route del -host 10.20.30.148 gw 10.20.30.40
#route del -net 10.20.30.40 netmask 255.255.255.248 eth0
#route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
#route del -net 192.168.1.0/24 eth1
#route del default gw 192.168.1.1
上面兩類工作也可以這樣一次完成:
Ifconfig eth0 172.16.19.71 netmask 255.255.255.0
Route 0.0.0.0 gw 172.16.19.254
Service network restart
#traceroute www.sina.com.cn
#ping www.sina.com.cn
#ping -c 4 192.168.1.12
-c 可指定封包數,排查時比讓它一直跑更好用。
顯示網路介面狀態:
#netstat -i
顯示所有監聽中的 Socket 與使用中的程式:
#netstat -lpe
顯示核心路由表:
#netstat -r
#netstat -nr
顯示 TCP/UDP 連線狀態:
#netstat -t
#netstat -u
#hostname myhost
檢視快取:
#arp
新增一筆 IP 與 MAC 的對應:
#arp -s 192.168.33.15 00:60:08:27:CE:B2
刪除一筆對應:
#arp -d 192.168.33.15
把這七個指令串起來,基本排障順序就成形了:先用 ping 確認是否通,再用 traceroute 看在哪一跳斷掉,接著用 route 與 netstat -r 檢查路由,用 ifconfig 確認介面狀態,最後用 arp 與 netstat -i 排查區域網層的問題。