Linux ip/ifconfig 查詢網路介面資訊
在 Linux 上查詢 IP 位址,以前大家都會教 ifconfig。
但其實 ifconfig 已經被標記為 Deprecated (過時) 了,現代 Linux 發行版 (如 Ubuntu 20.04+) 內建的是 ip 指令 (來自 iproute2 套件)。
雖然 ifconfig 還是可以用 (需安裝 net-tools),但建議儘早習慣使用強大的 ip 指令。
查詢 IP 位址
舊派:ifconfig
ifconfig
現代:ip addr
ip addr
# 或者簡寫
ip a
你會看到類似 eth0 或 ens33 這樣的網路介面名稱,底下的 inet 後面的數字就是你的 IPv4 位址。
查詢路由表 (Route)
舊派:route
route -n
現代:ip route
ip route
# 或者簡寫
ip r
這可以用來檢查預設閘道 (Default Gateway) 是指向哪裡。
啟用/停用網路介面
假設網卡名稱是 eth0。
舊派
sudo ifconfig eth0 up
sudo ifconfig eth0 down
現代
sudo ip link set eth0 up
sudo ip link set eth0 down
總結
| 功能 | 舊指令 (net-tools) | 新指令 (iproute2) |
|---|---|---|
| 查 IP | ifconfig | ip a |
| 查路由 | route -n | ip r |
| 啟用網卡 | ifconfig eth0 up | ip link set eth0 up |
| 查 ARP | arp -n | ip neigh |
盡量練習使用 ip,打起來也比較短!