温馨提示×

怎么用linux ifconfig

小樊
41
2026-08-27 01:12:55
栏目: 智能运维

在 Linux 中,ifconfiginterface configurator)是一个用于查看和配置网络接口的经典命令。不过要注意:在多数现代 Linux 发行版中,ifconfig 已被标记为“过时”推荐使用 ip 命令。但如果你仍在使用它(或维护老系统),下面是最常见用法。


一、查看网络接口信息

1️⃣ 查看所有网络接口

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

2️⃣ 查看指定网卡

ifconfig eth0

二、启用 / 禁用网卡

启用网卡

sudo ifconfig eth0 up

禁用网卡

sudo ifconfig eth0 down

三、配置 IP 地址

1️⃣ 设置 IP 地址

sudo ifconfig eth0 192.168.1.100

2️⃣ 设置 IP + 子网掩码

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

四、设置广播地址

sudo ifconfig eth0 broadcast 192.168.1.255

五、修改 MAC 地址(临时)

⚠️ 需要先把网卡 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  # 关闭

七、常见问题

❓ ifconfig 命令找不到?

说明系统没安装 net-tools

Ubuntu / Debian

sudo apt update
sudo apt install net-tools

CentOS / RHEL

sudo yum install net-tools

八、推荐使用的新命令(ip)

功能 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 命令。

如果你是想:

  • ✅ 配静态 IP
  • ✅ 虚拟机网络
  • ✅ Docker / 服务器网络
  • ✅ 排查网络不通

可以告诉我你的具体场景,我可以给你最合适的命令示例。

0