# 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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。