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 Configuration)是傳統網路介面配置工具,雖逐漸被 ip 指令取代,但在舊版系統、嵌入式設備、腳本相容性場景仍廣泛使用。本文詳解參數、四大實戰操作與「配置不持久」的根因與解法。

一、 核心參數與輸出解讀

參數功能
-a顯示所有介面(含 DOWN 狀態)
-s摘要模式,每介面一行
-v錯誤時顯示詳細訊息
up/down啟用/停用介面
netmask設定子網遮罩
addr設定 IP 位址
broadcast設定廣播位址
hw ether修改 MAC 位址

二、 典型輸出欄位解析(ifconfig eth0)

eth0 Link encap:Ethernet HWaddr 00:13:72:40:C5:F1
inet addr:192.168.60.13 Bcast:192.168.60.255 Mask:255.255.255.0
inet6 addr: fe80::213:72ff:fe40:c5f1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:154657999 errors:0 dropped:0 overruns:0 frame:0
TX packets:194872221 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1700497213 (1.5 GiB) TX bytes:1917599211 (1.7 GiB)
Interrupt:201
  • HWaddr:實體 MAC 位址
  • inet addr / Bcast / Mask:IPv4 位址、廣播、子網遮罩
  • UP:介面啟用;RUNNING:纜線連接;MULTICAST:支援多播
  • MTU:最大傳輸單元(預設 1500)
  • RX/TX packets/bytes/errors:收發統計,排查效能/丟包關鍵

三、 四大實戰操作

1. 單介面綁定多 IP(虛擬介面)

ifconfig eth0 192.168.60.136 netmask 255.255.255.254
ifconfig eth0:0 192.168.66.138 netmask 255.255.255.254
# 後續依序 eth0:1, eth0:2...

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

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

3. 介面重啟(套用新配置/排錯)

ifconfig eth0 down
ifconfig eth0 up

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

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

四、 致命陷阱:配置不持久化

現象:ifconfig 設定的 IP、MAC、MTU 等,**重啟網路服務或重開機後全部失效**。

原因:ifconfig 僅修改核心運行時狀態,不寫入磁碟配置檔。

解法:編輯對應發行版的網路配置檔:

  • RHEL/CentOS/Fedora:/etc/sysconfig/network-scripts/ifcfg-eth0
  • Debian/Ubuntu(舊版):/etc/network/interfaces
  • openSUSE:/etc/sysconfig/network/ifcfg-eth0
  • 現代系統:使用 nmcli(NetworkManager)或 ip 指令配合 netplan/systemd-networkd

五、 現代替代工具:ip 指令速查

任務ifconfigip(推薦)
顯示所有介面ifconfig -aip link show
顯示 IP 位址ifconfigip addr show
新增 IPifconfig eth0:0 1.2.3.4/24ip addr add 1.2.3.4/24 dev eth0
刪除 IPifconfig eth0:0 downip addr del 1.2.3.4/24 dev eth0
啟用介面ifconfig eth0 upip link set eth0 up
修改 MACifconfig eth0 hw ether XX:XX:XX:XX:XX:XXip link set eth0 address XX:XX:XX:XX:XX:XX

六、 結語

ifconfig 是網路配置的「經典語言」,ip 是「現代標準語」。維護舊系統必須精通前者;新架構請直接投擁後者。核心原則:**運行時配置僅供臨時驗證,持久化配置必須寫入磁碟配置檔或聲明式 API**。

Copyright © Windows教程網 All Rights Reserved