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
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...
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 down
ifconfig eth0 up
ifconfig eth0 promisc # 啟用
ifconfig eth0 -promisc # 停用
現象:ifconfig 設定的 IP、MAC、MTU 等,**重啟網路服務或重開機後全部失效**。
原因:ifconfig 僅修改核心運行時狀態,不寫入磁碟配置檔。
解法:編輯對應發行版的網路配置檔:
/etc/sysconfig/network-scripts/ifcfg-eth0/etc/network/interfaces/etc/sysconfig/network/ifcfg-eth0nmcli(NetworkManager)或 ip 指令配合 netplan/systemd-networkdip 指令速查| 任務 | ifconfig | ip(推薦) |
|---|---|---|
| 顯示所有介面 | ifconfig -a | ip link show |
| 顯示 IP 位址 | ifconfig | ip addr show |
| 新增 IP | ifconfig eth0:0 1.2.3.4/24 | ip addr add 1.2.3.4/24 dev eth0 |
| 刪除 IP | ifconfig eth0:0 down | ip addr del 1.2.3.4/24 dev eth0 |
| 啟用介面 | ifconfig eth0 up | ip link set eth0 up |
| 修改 MAC | ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX | ip link set eth0 address XX:XX:XX:XX:XX:XX |
ifconfig 是網路配置的「經典語言」,ip 是「現代標準語」。維護舊系統必須精通前者;新架構請直接投擁後者。核心原則:**運行時配置僅供臨時驗證,持久化配置必須寫入磁碟配置檔或聲明式 API**。