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 指令完整解析:網卡設定、IP 配置、MAC 修改、多網卡管理實戰

編輯:Linux 教學

ifconfig (interface configure) 是 Linux 系統管理員配置、檢視、除錯網路介面的核心指令。雖然現代發行版推薦使用 ip 指令 (iproute2 套件),但 ifconfig 仍廣泛存在於各類腳本、教學、嵌入式系統中。本文詳細解析 ifconfig 輸出欄位含義、常用參數、實戰設定範例與進階技巧。

指令語法

ifconfig [介面名稱]
ifconfig 介面名稱 [地址族] 選項 | 位址 ...
  • 無參數執行:顯示所有已啟用的網路介面狀態。
  • ifconfig -a:顯示所有介面(含未啟用)。
  • ifconfig eth0:顯示指定介面詳細資訊。

輸出欄位詳細解讀 (以 eth0 為例)

eth0 Link encap:Ethernet  HWaddr 52:54:AB:DD:6F:61
     inet addr:210.34.6.89  Bcast:210.34.6.127  Mask:255.255.255.128
     UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     RX packets:46299 errors:0 dropped:0 overruns:0 frame:189
     TX packets:3057 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:100
     Interrupt:5 Base address:0xece0
欄位含義
Link encap:Ethernet封裝協定:乙太網路
HWaddrMAC 位址 (硬體位址),廠商燒錄,全球唯一
inet addrIPv4 位址
Bcast廣播位址
Mask子網路遮罩
UP介面已啟用
BROADCAST支援廣播
RUNNING介面正在運作 (有纜線連接/載波偵測)
MULTICAST支援多播
MTU:1500最大傳輸單元 (Bytes)
Metric:1路由度量值
RX packets接收封包數
errors/dropped/overruns/frame接收錯誤/丟棄/溢位/幀錯誤計數
TX packets傳送封包數
carrier載波錯誤 (媒介故障指標)
collisions碰撞次數 (半雙工環境)
txqueuelen傳送佇列長度
Interrupt/Base address硬體中斷號/IO 埠位址

lo (Loopback) 介面 同理,inet addr:127.0.0.1、Mask:255.0.0.0、LOOPBACK RUNNING。

常用設定實戰

1. 臨時設定 IP 位址 (重開機失效)

# 設定 IP/子網路遮罩
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# 同時設定廣播位址
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255

# 啟用/停用介面
ifconfig eth0 up
ifconfig eth0 down

2. 修改 MAC 位址 (偽裝/繞過 MAC 綁定)

# 臨時修改 (重開機/網卡重置失效)
ifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up

# 永久修改:編輯 /etc/sysconfig/network-scripts/ifcfg-eth0 (RHEL/CentOS)
# 新增/修改:HWADDR=00:11:22:33:44:55
# 或在 /etc/rc.d/rc.local 新增上述 ifconfig 指令

3. 多網卡 / 虛擬介面 (IP Alias) 設定

# 檢視所有介面
ifconfig -a

# 為 eth0 新增虛擬介面 eth0:0 (同一張實體網卡綁定多 IP)
ifconfig eth0:0 192.168.1.200 netmask 255.255.255.0
ifconfig eth0:1 10.0.0.5 netmask 255.0.0.0

# 移除虛擬介面
ifconfig eth0:0 down

4. 啟用/停用混雜模式 (抓包/橋接必需)

ifconfig eth0 promisc   # 啟用
ifconfig eth0 -promisc  # 停用

5. 設定 MTU (巨型幀/VPN 優化)

ifconfig eth0 mtu 9000

永久生效設定 (各發行版差異)

發行版系列設定檔路徑關鍵參數
RHEL / CentOS / Fedora/etc/sysconfig/network-scripts/ifcfg-eth0BOOTPROTO=static, IPADDR, NETMASK, GATEWAY, ONBOOT=yes
Debian / Ubuntu (傳統)/etc/network/interfacesauto eth0, iface eth0 inet static, address, netmask, gateway
Debian / Ubuntu (新版 netplan)/etc/netplan/*.yamlnetwork: {version: 2, ethernets: {eth0: {addresses: [...]}}}
openSUSE/etc/sysconfig/network/ifcfg-eth0類似 RHEL

ifconfig vs ip 指令對照

現代系統建議使用 ip 指令 (iproute2) 取代 ifconfig (net-tools):

常用網路除錯指令組合

# 完整網路狀態檢查
ifconfig -a
ip route show
cat /etc/resolv.conf
ping -c 3 8.8.8.8
ping -c 3 www.google.com

# 監控即時流量
watch -n 1 'ifconfig eth0 | grep -E "RX packets|TX packets"'

總結

ifconfig 是 Linux 網路管理的基石指令,掌握其輸出解讀與常用參數,對系統管理、網路除錯、自動化腳本撰寫至關重要。雖 ip 指令功能更強大且為未來標準,但 ifconfig 仍將長期共存,熟練雙雙為佳。

Copyright © Windows教程網 All Rights Reserved
功能ifconfig (舊)ip (新)
顯示介面ifconfig -aip link show / ip addr show
設定 IPifconfig eth0 1.2.3.4/24ip addr add 1.2.3.4/24 dev eth0
啟用介面ifconfig eth0 upip link set eth0 up
路由表route -n / netstat -rnip route show
鄰居表 (ARP)arp -nip neigh show