萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux ifconfig 指令完整教學:網路介面配置與診斷實戰

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

常用操作實戰

1. 啟用/停用介面

ifconfig eth0 up      # 啟用
ifconfig eth0 down    # 停用(遠端操作慎用!會斷開連線)

2. 配置 IPv4 位址

# 臨時配置(重啟失效)
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
ifconfig eth0 192.168.1.100/24          # CIDR 寫法

# 別名介面(單網卡多 IP)
ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0
ifconfig eth0:1 192.168.1.102 netmask 255.255.255.0

3. 配置 IPv6 位址

# 新增 IPv6 位址
ifconfig eth0 add 2001:db8::1/64

# 刪除 IPv6 位址
ifconfig eth0 del 2001:db8::1/64

4. 修改 MAC 位址

# 必須先停用介面
ifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up

# 驗證
ifconfig eth0 | grep ether

⚠️ 修改 MAC 可能違反網路政策、導致 DHCP 衝突、觸發交換器安全機制,僅限測試環境或經授權場景。

5. 設定 MTU

# 設定巨型幀(需交換器、網卡、對端全支援)
ifconfig eth0 mtu 9000

# 恢復預設
ifconfig eth0 mtu 1500

6. 啟用/停用混雜模式

ifconfig eth0 promisc    # 啟用(抓包、IDS 需求)
ifconfig eth0 -promisc   # 停用

7. 多播與廣播設定

ifconfig eth0 multicast      # 啟用多播
ifconfig eth0 broadcast 192.168.1.255  # 設定廣播位址

ifconfig vs ip 指令對照表

欄位含義關鍵指標
flags介面標誌位UP=啟用, RUNNING=有載波(網線連接), MULTICAST=支援多播
mtu最大傳輸單元預設 1500,巨型幀 9000
inetIPv4 位址含 netmask(子網遮罩), broadcast(廣播位址)
inet6IPv6 位址link-local 以 fe80:: 開頭,global 以 2000::/3 開頭
etherMAC 位址硬體位址,可用 hw ether 修改
txqueuelen傳輸佇列長度高延遲鏈路可調大
RX/TX packets收發封包計數流量基準
RX/TX bytes收發位元組數流量統計
errors/dropped/overruns錯誤/丟棄/溢位計數非零即異常,需深入排查
collisions碰撞計數半雙工/共享媒介才會出現,全雙工應為 0
carrier載波錯誤實體層訊號問題(網線、埠口、光模組)
功能ifconfig (舊)ip (新,推薦)
顯示所有介面ifconfig -aip link show
顯示 IP 位址ifconfigip addr show
啟用介面ifconfig eth0 upip link set eth0 up
配置 IPifconfig eth0 1.2.3.4/24ip addr add 1.2.3.4/24 dev eth0
刪除 IP不直接支援ip 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
路由管理routeip route
鄰居/ARP 表arpip neigh

常見故障排查場景

場景 1:介面顯示 UP 但無 RUNNING

eth0: flags=4099  # 缺少 RUNNING

表示網卡驅動已載入、介面已啟用,但無實體連結(網線未插、交換器埠關閉、光模組故障)。

場景 2:RX/TX errors 持續增長

  • CRC errors:網線品質差、水晶頭氧化、交換器埠故障
  • overruns:系統處理不過來(中斷風暴、驅動問題)
  • carrier errors:實體層訊號不穩
  • collisions:半雙工模式不匹配

排查步驟:換網線 → 換埠口 → 檢查雙工模式 ethtool eth0 → 更新驅動

場景 3:IP 衝突偵測


# 使用 arping 偵測 IP 衝突(需安裝 iputils-arping)
arping -D -I eth0 -c 3 192.168.1.100
# DAD (Duplicate Address Detection) 模式

永久配置:網路設定檔

CentOS/RHEL (/etc/sysconfig/network-scripts/ifcfg-eth0)

DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes
NM_CONTROLLED=no

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 114.114.114.114

Netplan (Ubuntu 18.04+)

# /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 輸出示意圖

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

copyright © 萬盛學電腦網 all rights reserved