Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux ifconfig 命令詳解:網路介面配置、診斷與現代替代方案

編輯:關於 Linux

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

關鍵欄位含義

  • flags:介面標誌位(十六進制 + 符號名):
    • UP:介面啟用
    • BROADCAST:支援廣播
    • RUNNING:驅動已分配資源,纜線連接(非純軟體狀態)
    • MULTICAST:支援多播
    • PROMISC:混雜模式
    • LOOPBACK:回環介面
    • NOARP:無 ARP
  • mtu:最大傳輸單元(位元組),乙太網預設 1500,巨幀通常 9000。
  • inet:IPv4 位址、掩碼、廣播。
  • inet6:IPv6 位址、前綴長度、作用域(link/global)。
  • ether:MAC 位址(硬體位址)。
  • txqueuelen:發送隊列長度(封包數)。
  • RX/TX packets/bytes:收發封包數與位元組數(核心計數器,重啟歸零)。
  • errors/dropped/overruns/carrier/collisions:錯誤計數器,非零通常提示硬體/驅動/配置問題。

回環介面 lo 解析


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 為高效能本地傳輸優化。

實戰操作場景

1. 臨時配置靜態 IP(重啟失效)


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

2. 啟用/禁用介面(故障排除常用)


ifconfig eth0 down  # 物理/邏輯斷開
ifconfig eth0 up    # 重新初始化

3. 修改 MAC 位址(測試/繞過綁定)


ifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up

4. 配置 IPv6 位址


ifconfig eth0 add 2001:db8:1::100/64
ifconfig eth0 del 2001:db8:1::100/64

5. 啟用混雜模式(抓包/IDS)


ifconfig eth0 promisc
# 關閉
ifconfig eth0 -promisc

6. 調整 MTU(巨幀/VPN/隧道優化)


ifconfig eth0 mtu 9000
# 驗證
ping -s 8972 -M do 192.168.1.1  # 不分片測試

持久化配置:發行版差異

ifconfig 命令行修改**運行時生效,重啟網路服務或重開機即失效**。

RHEL/CentOS/Fedora (網路腳本)


# /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

Debian/Ubuntu (/etc/network/interfaces)


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

現代 systemd-networkd (.network 文件)


# /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 vs ip 命令:新舊對照速查

操作ifconfig (舊)ip (新, 推薦)
顯示所有介面ifconfig -aip link show / ip -br link
顯示 IP 位址ifconfigip addr show / ip -br addr
啟用介面ifconfig eth0 upip link set eth0 up
禁用介面ifconfig eth0 downip link set eth0 down
添加 IPifconfig eth0 1.2.3.4/24ip addr add 1.2.3.4/24 dev eth0
刪除 IPifconfig eth0 del 1.2.3.4ip addr del 1.2.3.4/24 dev eth0
修改 MACifconfig eth0 hw ether XX:XX:XX:XX:XX:XXip link set eth0 address XX:XX:XX:XX:XX:XX
修改 MTUifconfig eth0 mtu 9000ip link set eth0 mtu 9000
路由表route -nip route show
ARP/鄰居表arp -nip neigh show
統計計數器ifconfig -sip -s link

常見問題排查

現象可能原因排查命令
介面無 IPDHCP 失敗、靜態配置錯誤、介面 downifconfig -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

ifconfig輸出示例

遷移建議與最佳實踐

  1. 新腳本/工具全面採用 ip:功能完整、輸出可解析 (JSON)、支援命名空間/VRF/策略路由/多隊列等現代特性。
  2. 舊腳本兼容層:安裝 net-tools 套件提供 ifconfig、`route`、`arp`、`netstat`,但標記為 deprecated。
  3. 容器環境:基礎鏡像精簡常無 ifconfig,直接用 ip 或 ss(替代 netstat)。
  4. 監控採集:Prometheus Node Exporter 等現代採集器直接讀取 /sys/class/net/、/proc/net/dev,不依賴用戶態工具。

熟練掌握 ifconfig 輸出解讀,是排查網路故障、讀懂舊文檔腳本的基礎;全面擁抱 ip 命令與聲明式網路配置,才是現代 Linux 網路運維的正道。

Copyright © Windows教程網 All Rights Reserved