ifconfig(interface configuration)是傳統網路介面配置工具,雖然 ip 指令(iproute2 套件)已成為現代標準,但 ifconfig 仍廣泛用於舊系統、容器環境、嵌入式裝置與網路設備。熟練掌握其輸出解讀與操作技巧,是網路故障排除的基本功。
ifconfig [介面] [選項]
# 需 root 權限或 CAP_NET_ADMIN capability
# 僅查看可用一般使用者
[root@localhost ~]# ifconfig eth0
eth0: flags=4163 mtu 1500
inet 192.168.120.204 netmask 255.255.255.0 broadcast 192.168.120.255
inet6 fe80::250:56ff:febf:2620 prefixlen 64 scopeid 0x20
ether 00:50:56:bf:26:20 txqueuelen 1000 (Ethernet)
RX packets 8700857 bytes 596390239 (568.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 31533 bytes 2886956 (2.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
| 欄位 | 含義 | 關鍵指標 |
|---|---|---|
| flags | 介面標誌位 | UP=啟用, RUNNING=有載波(網線連接), MULTICAST=支援多播 |
| mtu | 最大傳輸單元 | 預設 1500,巨型幀 9000 |
| inet | IPv4 位址 | 含 netmask(子網遮罩), broadcast(廣播位址) |
| inet6 | IPv6 位址 | link-local 以 fe80:: 開頭,global 以 2000::/3 開頭 |
| ether | MAC 位址 | 硬體位址,可用 hw ether 修改 |
| txqueuelen | 傳輸佇列長度 | 高延遲鏈路可調大 |
| RX/TX packets | 收發封包計數 | 流量基準 |
| RX/TX bytes | 收發位元組數 | 流量統計 |
| errors/dropped/overruns | 錯誤/丟棄/溢位計數 | 非零即異常,需深入排查 |
| collisions | 碰撞計數 | 半雙工/共享媒介才會出現,全雙工應為 0 |
| carrier | 載波錯誤 | 實體層訊號問題(網線、埠口、光模組) |
| 功能 | ifconfig (舊) | ip (新,推薦) |
|---|---|---|
| 顯示所有介面 | ifconfig -a | ip link show |
| 顯示 IP 位址 | ifconfig | ip addr show |
| 啟用介面 | ifconfig eth0 up | ip link set eth0 up |
| 配置 IP | ifconfig eth0 1.2.3.4/24 | ip addr add 1.2.3.4/24 dev eth0 |
| 刪除 IP | 不直接支援 | ip addr del 1.2.3.4/24 dev eth0 |
| 修改 MAC | ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx | ip link set eth0 address xx:xx:xx:xx:xx:xx |
| 設定 MTU | ifconfig eth0 mtu 9000 | ip link set eth0 mtu 9000 |
| 路由管理 | route | ip route |
| 鄰居/ARP 表 | arp | ip neigh |
eth0: flags=4099 # 缺少 RUNNING
表示網卡驅動已載入、介面已啟用,但無實體連結(網線未插、交換器埠關閉、光模組故障)。
CRC errors:網線品質差、水晶頭氧化、交換器埠故障overruns:系統處理不過來(中斷風暴、驅動問題)carrier errors:實體層訊號不穩collisions:半雙工模式不匹配排查步驟:換網線 → 換埠口 → 檢查雙工模式 ethtool eth0 → 更新驅動
# 使用 arping 偵測 IP 衝突(需安裝 iputils-arping)
arping -D -I eth0 -c 3 192.168.1.100
# DAD (Duplicate Address Detection) 模式
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes
NM_CONTROLLED=no
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 114.114.114.114
# /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 114.114.114.114]

ifconfig 作為經典工具,其輸出格式直觀、行為可預期。雖然 ip 指令功能更強大、語法更一致,但在排查舊系統、閱讀歷史文檔、操作不支援 iproute2 的嵌入式設備時,ifconfig 依然不可或缺。建議雙工具並行掌握,以 ip 為主、ifconfig 為輔。