現代 Linux 以 iproute2 的 ip 指令取代多數 ifconfig、route、arp 工作。先用 show、get、statistics 查現況;手動 add/del 通常只改執行中狀態,重開後是否保留取決於網路管理工具。
動手前:遠端主機改 IP、route 或 link 可能立刻把 SSH 踢掉。正式變更要有 console/OOB、維護窗、現況備份與回復命令;範例中的 add/del 只適合明確測試環境。
介面與位址查詢
列出所有介面與位址Linux
ip address show簡寫 ip addr 或 ip a。
只看 IPv4Linux
ip -4 address show排除 IPv6 資訊。
只看單一介面Linux
ip address show dev eth0介面名稱換成實際值。
精簡彩色輸出Linux
ip -brief -color address-brief 支援度依 iproute2 版本。
看 link 狀態與 MACLinux
ip link show可看 UP/LOWER_UP、MTU 與 MAC。
看介面錯誤與流量統計Linux
ip -s link show dev eth0觀察 dropped、errors 需搭配時間差。
暫時啟用介面Linux
sudo ip link set dev eth0 up可能影響連線;不代表永久設定。
暫時停用介面Linux
sudo ip link set dev eth0 down會立即中斷該介面流量,遠端操作風險高。
暫時加一個測試 IPLinux
sudo ip address add 192.0.2.10/24 dev eth0使用文件保留網段範例;實際位址需確認不衝突。
移除暫時 IPLinux
sudo ip address del 192.0.2.10/24 dev eth0先確認不是正在使用的管理 IP。
路由查詢
看主路由表Linux
ip route show含 default route 與直連網段。
看 IPv6 路由Linux
ip -6 route show分開檢查 IPv6。
查到目的 IP 會走哪條路Linux
ip route get 1.1.1.1顯示出口、gateway 與選用來源 IP。
指定來源查路由Linux
ip route get 1.1.1.1 from 192.0.2.10多網卡或 policy routing 排查常用。
看所有路由表Linux
ip route show table allpolicy routing 環境不要只看 main。
看 policy rulesLinux
ip rule show規則優先順序數值越小通常越先處理。
暫時加測試路由Linux
sudo ip route add 198.51.100.0/24 via 192.0.2.1 dev eth0先確認 gateway 可達,並準備對應 del。
刪除測試路由Linux
sudo ip route del 198.51.100.0/24 via 192.0.2.1 dev eth0參數需和新增路由匹配。
ARP/NDP neighbor
看 ARP 與 IPv6 neighborLinux
ip neighbor show狀態可能是 REACHABLE、STALE、FAILED 等。
只看單一介面 neighborLinux
ip neighbor show dev eth0縮小到指定 L2 網段。
查單一 IPLinux
ip neighbor show 192.0.2.1沒有結果不一定故障,可能尚未通訊。
看 network namespaceLinux
ip netns list容器與實驗環境常見。
在 namespace 內查路由Linux
sudo ip netns exec <namespace> ip route把占位符換成已存在的 namespace。
怎麼確認有做對
- 改動前後各保存 ip -brief address、ip route、ip rule。
- 用 ip route get 驗證實際路由選擇。
- 從同網段與跨網段分別測連線,確認回程路由。
常見錯誤
- 遠端改管理介面沒有 OOB。
- 只改執行中 ip 設定,誤以為重開後會保留。
- 只看 main table,漏掉 policy routing。
- neighbor STALE 就認定連線故障。
版本與官方文件
參數會隨工具版本與作業系統實作改變。正式環境先用 --help、-h 或系統內建說明確認,再以當版官方文件為準。