044 ifconfig命令

命令说明

ifconfig 用于查看和配置网络接口(IP、掩码、启停),属于已废弃的 net-tools 套件;新系统推荐用 ip addr/ip link,但老运维习惯与脚本中仍大量出现。

语法

ifconfig [接口] [IP 选项]
ifconfig 接口 up/down

常用参数

参数说明
(无参数)只显示已激活(UP)的接口
-a显示所有接口,包括未启用的
接口名只看指定接口,如 ifconfig eth0
IP 地址直接给接口设置 IPv4 地址
netmask 掩码设置子网掩码,如 netmask 255.255.255.0
up / down启用 / 停用接口
promisc开启混杂模式(抓包分析用)
mtu 值设置接口 MTU
txqueuelen设置发送队列长度

使用示例

$ ifconfig

查看当前活动网卡与 IP

$ ifconfig -a

查看所有网卡(含未启用)

$ ifconfig eth0

查看 eth0 详细信息

$ ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up

临时配置 IP 并启用

$ ifconfig eth0 down

停用网卡(远程操作会直接断连,慎用)

$ ifconfig lo up

启用回环接口(本地服务依赖 lo)

$ ifconfig eth0 mtu 1400

临时修改 MTU 排查分片问题

$ ifconfig eth0:0 192.168.1.200

创建别名实现单网卡多 IP(老式做法)

实用技巧

  • ifconfig 属于 net-tools,最小化安装常没有:CentOS 用 yum install net-tools;新标准命令是 ip(ip addr / ip link / ip route)。
  • ifconfig 配置的 IP 重启即失效;永久配置要写文件:RHEL 系改 /etc/sysconfig/network-scripts/ifcfg-eth0 或 nmcli,Debian 系改 /etc/network/interfaces 或 netplan。
  • ifconfig 不显示 IPv6 细节,看全部地址用 ip -6 addr 或 ip addr。
  • ifconfig 看不到 DOWN 接口加 -a;RX/TX 统计中的 errors/dropped 增长提示网卡或链路有问题。
  • 远程 SSH 操作时千万不要 ifconfig ethX down 自己正在用的网卡,否则会话直接断开且无法恢复。