ifconfig(Interface Configuration)是傳統 Linux/Unix 網路介面配置與查看工具,雖然 iproute2 套件中的 ip 命令已成為現代標準,但 ifconfig 仍廣泛存在於舊版系統、嵌入式設備、容器基礎鏡像及大量運維腳本中。本文系統整理 ifconfig 的完整用法、輸出解讀、常見操作場景與遷移指南。
ifconfig [介面名] [參數/選項]
# 查看/配置通常需要 root 權限或 CAP_NET_ADMIN capability
| 參數 | 功能 | 備註 |
|---|---|---|
up | 啟用介面 | 等同 ifconfig eth0 up |
down | 禁用介面 | 切斷流量,路由表條目不自動刪除 |
-a | 顯示所有介面(含 down 狀態) | 預設僅顯示 up 介面 |
-s | 顯示摘要統計(類似 netstat -i) | |
inet addr | 設定 IPv4 位址 | ifconfig eth0 192.168.1.10 |
netmask | 設定子網掩碼 | ifconfig eth0 netmask 255.255.255.0 |
broadcast | 設定廣播位址 | 通常自動推導,可手動指定 |
mtu | 設定最大傳輸單元 | ifconfig eth0 mtu 9000 (巨幀) |
arp/-arp | 啟用/禁用 ARP 協議 | |
promisc/-promisc | 啟用/禁用混雜模式 | 抓包/IDS 需啟用 |
allmulti/-allmulti | 啟用/禁用多播接收 | |
txqueuelen | 設定發送隊列長度 | 高延遲/高頻寬鏈路可調大 |
add/del | 添加/刪除 IPv6 位址 | ifconfig eth0 add 2001:db8::1/64 |
tunnel | 建立 IP 隧道 | 配合 dstaddr 使用 |
dstaddr | 設定點對點目標位址 |
[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
UP:介面啟用BROADCAST:支援廣播RUNNING:驅動已分配資源,纜線連接(非純軟體狀態)MULTICAST:支援多播PROMISC:混雜模式LOOPBACK:回環介面NOARP:無 ARPlo 解析
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
RX packets 68 bytes 2856 (2.7 KiB)
TX packets 68 bytes 2856 (2.7 KiB)
本機進程間通訊、本地服務綁定(如 127.0.0.1:3306)、測試網路棧無需物理網卡。MTU 65536 為高效能本地傳輸優化。
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255 up
# 或分步
ifconfig eth0 up
ifconfig eth0 192.168.1.100
ifconfig eth0 netmask 255.255.255.0
ifconfig eth0 down # 物理/邏輯斷開
ifconfig eth0 up # 重新初始化
ifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up
ifconfig eth0 add 2001:db8:1::100/64
ifconfig eth0 del 2001:db8:1::100/64
ifconfig eth0 promisc
# 關閉
ifconfig eth0 -promisc
ifconfig eth0 mtu 9000
# 驗證
ping -s 8972 -M do 192.168.1.1 # 不分片測試
ifconfig命令行修改**運行時生效,重啟網路服務或重開機即失效**。
# /etc/sysconfig/network-scripts/ifcfg-eth0
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
# IPv6
IPV6INIT=yes
IPV6ADDR=2001:db8:1::100/64
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 8.8.4.4
# IPv6
iface eth0 inet6 static
address 2001:db8:1::100
netmask 64
# /etc/systemd/network/10-eth0.network
[Match]
Name=eth0
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
Address=2001:db8:1::100/64
| 操作 | ifconfig (舊) | ip (新, 推薦) |
|---|---|---|
| 顯示所有介面 | ifconfig -a | ip link show / ip -br link |
| 顯示 IP 位址 | ifconfig | ip addr show / ip -br addr |
| 啟用介面 | ifconfig eth0 up | ip link set eth0 up |
| 禁用介面 | ifconfig eth0 down | ip link set eth0 down |
| 添加 IP | ifconfig eth0 1.2.3.4/24 | ip addr add 1.2.3.4/24 dev eth0 |
| 刪除 IP | ifconfig eth0 del 1.2.3.4 | 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 -n | ip route show |
| ARP/鄰居表 | arp -n | ip neigh show |
| 統計計數器 | ifconfig -s | ip -s link |
| 現象 | 可能原因 | 排查命令 |
|---|---|---|
| 介面無 IP | DHCP 失敗、靜態配置錯誤、介面 down | ifconfig -a, dhclient -v eth0, journalctl -u network |
| RX/TX errors 非零 | 網線/光纖劣化、交換器埠故障、驅動 Bug、MTU 不匹配 | ethtool -S eth0, dmesg -T | grep eth0 |
| collisions 非零 | 半雙工模式、共享介質、交換器背板擁塞 | ethtool eth0 | grep -i duplex |
| IPv6 無法通訊 | 未獲取全域位址、防火牆阻擋、路由缺失 | ip -6 addr, ip -6 route, ping6 |
| 介面名變動 (eth0 -> ens33) | systemd 預測性命名 | ip link, dmesg | grep -i eth, 修改 grub net.ifnames=0 |

ip:功能完整、輸出可解析 (JSON)、支援命名空間/VRF/策略路由/多隊列等現代特性。net-tools 套件提供 ifconfig、`route`、`arp`、`netstat`,但標記為 deprecated。ifconfig,直接用 ip 或 ss(替代 netstat)。/sys/class/net/、/proc/net/dev,不依賴用戶態工具。熟練掌握 ifconfig 輸出解讀,是排查網路故障、讀懂舊文檔腳本的基礎;全面擁抱 ip 命令與聲明式網路配置,才是現代 Linux 網路運維的正道。