在 Linux 中,ifconfig(interface configurator)是一个用于查看和配置网络接口的经典命令。不过要注意:在多数现代 Linux 发行版中,ifconfig 已被标记为“过时”,推荐使用 ip 命令。但如果你仍在使用它(或维护老系统),下面是最常见用法。
ifconfig
或
ifconfig -a
输出示例:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.100 netmask 255.255.255.0
inet6 fe80::20c:29ff:fe12:3456 prefixlen 64
ether 00:0c:29:12:34:56 txqueuelen 1000
...
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
ifconfig eth0
sudo ifconfig eth0 up
sudo ifconfig eth0 down
sudo ifconfig eth0 192.168.1.100
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
sudo ifconfig eth0 broadcast 192.168.1.255
⚠️ 需要先把网卡 down 掉
sudo ifconfig eth0 down
sudo ifconfig eth0 hw ether 00:11:22:33:44:55
sudo ifconfig eth0 up
sudo ifconfig eth0 promisc # 开启
sudo ifconfig eth0 -promisc # 关闭
说明系统没安装 net-tools:
sudo apt update
sudo apt install net-tools
sudo yum install net-tools
| 功能 | ifconfig | ip 命令 |
|---|---|---|
| 查看 IP | ifconfig |
ip addr |
| 启用网卡 | ifconfig up |
ip link set eth0 up |
| 设置 IP | ifconfig eth0 IP |
ip addr add IP dev eth0 |
示例:
ip addr
sudo ip link set eth0 up
sudo ip addr add 192.168.1.100/24 dev eth0
ifconfig 适合快速查看和临时配置,生产环境建议用
ip命令。
如果你是想:
可以告诉我你的具体场景,我可以给你最合适的命令示例。