温馨提示×

linux ifconfig如何配置网卡

小樊
40
2026-09-20 06:40:06
栏目: 智能运维

在 Linux 中,ifconfig 是一个传统的网络配置命令(来自 net-tools 包),用于查看和临时配置网卡。需要注意的是:

  • ifconfig 配置是临时生效的,重启网络或系统后会失效
  • 现代 Linux 更推荐使用 ip 命令或 nmcli / 网络配置文件

下面按常见场景说明如何用 ifconfig 配置网卡。


一、查看网卡信息

ifconfig

或查看指定网卡:

ifconfig eth0

二、启用 / 禁用网卡

启用网卡

ifconfig eth0 up

禁用网卡

ifconfig eth0 down

三、配置 IP 地址(IPv4)

1. 配置静态 IP

ifconfig eth0 192.168.1.100 netmask 255.255.255.0

2. 同时指定广播地址(可选)

ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255

四、配置子网掩码

ifconfig eth0 netmask 255.255.255.0

五、配置网关(ifconfig 不能直接配网关)

ifconfig 不能设置默认网关,需要用 routeip

route add default gw 192.168.1.1

或使用 ip 命令(推荐):

ip route add default via 192.168.1.1

六、配置 MAC 地址

ifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up

七、开启 / 关闭混杂模式(抓包用)

ifconfig eth0 promisc   # 开启
ifconfig eth0 -promisc  # 关闭

八、配置 MTU

ifconfig eth0 mtu 1500

九、永久配置(重要说明)

ifconfig 无法实现永久配置,常见方式:

CentOS / RHEL

编辑:

/etc/sysconfig/network-scripts/ifcfg-eth0

Ubuntu(老版本)

/etc/network/interfaces

Ubuntu 18.04+

使用 netplan

/etc/netplan/*.yaml

十、现代替代命令(推荐)

ip addr show
ip link set eth0 up
ip addr add 192.168.1.100/24 dev eth0
ip route add default via 192.168.1.1

如果你愿意,可以告诉我:

  • 使用的 Linux 发行版
  • 临时调试还是永久配置
  • 网卡名称(如 eth0ens33

我可以给你更精确的配置示例。

0