# Linux ifconfig命令怎么用
## 一、ifconfig命令概述
### 1.1 什么是ifconfig
`ifconfig`(interface configuration)是Linux系統中用于配置和顯示網絡接口參數的核心命令行工具。該命令允許管理員查看、配置、啟用或禁用網絡接口,設置IP地址、子網掩碼、廣播地址等網絡參數。
### 1.2 歷史與現狀
- 起源于BSD Unix系統
- 傳統Linux發行版的標配工具
- 逐漸被`ip`命令取代(Red Hat系推薦使用`ip`)
- 仍廣泛用于腳本編寫和快速網絡診斷
### 1.3 基本語法格式
```bash
ifconfig [interface] [options] [address]
which ifconfig
# 或
ifconfig --version
sudo apt install net-tools
sudo yum install net-tools
ifconfig -a
典型輸出示例:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:3f:5a:1c txqueuelen 1000 (Ethernet)
RX packets 12045 bytes 10234567 (9.7 MiB)
TX packets 8932 bytes 5678901 (5.4 MiB)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
ifconfig eth0
sudo ifconfig eth0 up # 啟用
sudo ifconfig eth0 down # 禁用
sudo ifconfig eth0 192.168.1.100
sudo ifconfig eth0 netmask 255.255.255.0
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
sudo ifconfig eth0 broadcast 192.168.1.255
sudo ifconfig eth0 hw ether 00:0c:29:3f:5a:1c
sudo ifconfig eth0 mtu 1492
sudo ifconfig eth0:0 192.168.1.101
ifconfig eth0 | grep "RX packets"
sudo ifconfig eth0 -arp
sudo ifconfig eth0 promisc # 啟用
sudo ifconfig eth0 -promisc # 禁用
ifconfig | grep "RUNNING"
ifconfig eth0 | grep "dropped"
結合watch命令:
watch -n 1 ifconfig eth0
ifconfig eth0 | grep mtu
功能 | ifconfig | ip命令 |
---|---|---|
顯示接口信息 | ? | ip addr show |
設置IP地址 | ? | ip addr add |
啟用/禁用接口 | ? | ip link set |
路由管理 | ? | ip route |
ARP緩存管理 | ? | ip neigh |
ifconfig適用場景:
ip命令適用場景:
# 解決方案:
sudo apt install net-tools # Debian/Ubuntu
sudo yum install net-tools # RHEL/CentOS
需要將配置寫入網絡配置文件: - Debian系:/etc/network/interfaces - RHEL系:/etc/sysconfig/network-scripts/
pre-up ifconfig eth0 hw ether 00:11:22:33:44:55
建議使用iwconfig配合ifconfig使用:
sudo iwconfig wlan0 essid "MyWiFi" key s:password
sudo ifconfig wlan0 up
修改MAC地址可能違反網絡策略
建議通過sudo限制普通用戶使用
sudo ifconfig eth0 10.0.0.100 netmask 255.255.255.0 up
sudo ifconfig eth0:1 192.168.2.100/24
ifconfig eth0
# 檢查:
# 1. UP狀態
# 2. 正確IP地址
# 3. 無異常錯誤計數
ip addr show # 替代ifconfig
ip link set # 接口控制
ip route # 路由管理
nmcli device show
nmcli connection modify
功能 | 命令示例 |
---|---|
查看所有接口 | ifconfig -a |
啟用接口 | sudo ifconfig eth0 up |
設置IP地址 | sudo ifconfig eth0 192.168.1.2 |
設置子網掩碼 | sudo ifconfig eth0 netmask 255.255.255.0 |
創建虛擬接口 | sudo ifconfig eth0:0 10.0.0.1 |
查看特定接口 | ifconfig eth0 |
設置MAC地址 | sudo ifconfig eth0 hw ether 00:11:22:33:44:55 |
啟用混雜模式 | sudo ifconfig eth0 promisc |
注意:本文基于Linux內核5.x版本和net-tools 2.x編寫,不同發行版可能略有差異。 “`
(注:實際字數約2500字,完整4000字版本需要擴展每個章節的詳細說明、增加更多示例和故障排查場景)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。