网络工具 / 命令文档

Linux ifconfig 命令

$ ifconfig 14 个章节 Linux 命令详解

概述

网络接口配置与管理的核心工具

快速导航

命令简介 基本语法 常用参数 使用示例 运维场景 注意事项

一、命令简介

ifconfig(interface configuration)是Linux/Unix系统中用于配置和显示网络接口参数的传统命令工具。虽然在现代Linux发行版中逐渐被 ip 命令取代,但因其简单易用,仍然被广泛使用。

提示:ifconfig 属于 net-tools 包,某些新版Linux系统可能需要手动安装: sudo apt-get install net-tools (Debian/Ubuntu) sudo yum install net-tools (CentOS/RHEL)

二、基本语法

ifconfig [接口] [选项] [地址]

# 基本格式示例
ifconfig                    # 显示所有活动接口
ifconfig -a                 # 显示所有接口(包括未激活)
ifconfig eth0               # 显示指定接口信息
ifconfig eth0 192.168.1.100 # 设置IP地址

三、常用参数详解

参数功能说明使用示例
-a显示所有网络接口,包括未激活的ifconfig -a
up激活指定网络接口ifconfig eth0 up
down关闭指定网络接口ifconfig eth0 down
netmask设置子网掩码ifconfig eth0 netmask 255.255.255.0
broadcast设置广播地址ifconfig eth0 broadcast 192.168.1.255
mtu设置最大传输单元ifconfig eth0 mtu 1500
hw ether修改MAC地址ifconfig eth0 hw ether 00:11:22:33:44:55
-s显示摘要信息ifconfig -s

四、输出信息解读

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe8e:5a3c  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:8e:5a:3c  txqueuelen 1000  (Ethernet)
        RX packets 125432  bytes 168543210 (160.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 98765  bytes 12345678 (11.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

接口状态标志

UP: 接口已启用 BROADCAST: 支持广播 RUNNING: 接口正在运行 MULTICAST: 支持多播

网络地址信息

inet: IPv4地址 netmask: 子网掩码 broadcast: 广播地址 inet6: IPv6地址

流量统计

RX packets: 接收数据包数 TX packets: 发送数据包数 RX bytes: 接收字节数 TX bytes: 发送字节数

1. 基础查询操作

# 查看所有网络接口

ifconfig -a

# 查看指定接口详情

ifconfig eth0

# 显示简短统计信息

ifconfig -s

2. IP地址配置

# 设置IP地址

sudo ifconfig eth0 192.168.1.100

# 设置IP地址和子网掩码

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# 完整配置(IP、掩码、广播)

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255

# 添加额外IP地址(虚拟接口)

sudo ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0

3. 接口管理

# 启用网络接口

sudo ifconfig eth0 up

# 禁用网络接口

sudo ifconfig eth0 down

# 修改MTU值

sudo ifconfig eth0 mtu 9000

# 修改MAC地址(需先禁用接口)

sudo ifconfig eth0 down

sudo ifconfig eth0 hw ether 00:11:22:33:44:55

sudo ifconfig eth0 up

六、常见运维场景

场景1:网络故障排查

# 检查网络接口状态

ifconfig | grep -E "^[a-z]|inet "

# 查看是否有丢包或错误

ifconfig eth0 | grep -E "errors|dropped"

# 重启网络接口

sudo ifconfig eth0 down && sudo ifconfig eth0 up

场景2:临时IP地址变更

# 备份当前配置
ifconfig eth0 > /tmp/eth0_backup.txt

# 更改IP地址
sudo ifconfig eth0 192.168.2.100 netmask 255.255.255.0

# 验证新配置
ping -c 3 192.168.2.1

场景3:网络性能优化

# 调整MTU以优化大文件传输
sudo ifconfig eth0 mtu 9000

# 查看网络流量统计
watch -n 1 'ifconfig eth0 | grep "RX packets\|TX packets"'

# 清除接口统计信息(需要重启接口)
sudo ifconfig eth0 down && sudo ifconfig eth0 up

场景4:多IP配置(虚拟主机)

# 添加多个IP到同一网卡
sudo ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0
sudo ifconfig eth0:1 192.168.1.102 netmask 255.255.255.0
sudo ifconfig eth0:2 192.168.1.103 netmask 255.255.255.0

# 查看所有虚拟接口
ifconfig | grep "eth0:"

# 删除虚拟接口
sudo ifconfig eth0:0 down

场景5:网络安全检测

# 检测混杂模式(可能存在嗅探)
ifconfig | grep PROMISC

# 启用混杂模式(用于抓包分析)
sudo ifconfig eth0 promisc

# 禁用混杂模式
sudo ifconfig eth0 -promisc

# 检查MAC地址是否被修改
ifconfig eth0 | grep ether

七、重要注意事项

警告: 使用 ifconfig 修改的网络配置是临时的,系统重启后会失效 永久修改需要编辑配置文件(如 /etc/network/interfaces 或 /etc/sysconfig/network-scripts/) 修改网络配置可能导致连接中断,远程操作时要特别小心
最佳实践: 在生产环境操作前,先在测试环境验证命令 远程操作时,使用 screen 或 tmux 防止意外断线 修改配置前先备份当前配置:ifconfig > /tmp/network_backup_$(date +%Y%m%d).txt 考虑学习使用新的 ip 命令,它功能更强大 使用配置管理工具(如 Ansible)进行批量网络配置

ifconfig vs ip 命令对比

操作ifconfig 命令ip 命令(推荐)
显示所有接口ifconfig -aip addr show
设置IP地址ifconfig eth0 192.168.1.100ip addr add 192.168.1.100/24 dev eth0
启用接口ifconfig eth0 upip link set eth0 up
查看路由表route -nip route show

八、动手练习

练习1:基础操作

1. 查看系统所有网络接口 2. 查看lo回环接口的详细信息 3. 统计eth0接口的收发包数量

练习2:配置管理

1. 为eth0添加一个虚拟IP 2. 修改eth0的MTU为1400 3. 临时禁用再启用eth0接口

练习3:故障排查

1. 检查是否有网络错误或丢包 2. 监控实时网络流量变化 3. 验证IP配置是否正确

九、课程总结

ifconfig 是Linux系统管理员必须掌握的基础命令之一。虽然它正在被 ip 命令取代,但其简洁的语法和直观的输出仍然使它在日常运维中占有一席之地。掌握 ifconfig 不仅能帮助你快速诊断和解决网络问题,也是深入理解Linux网络栈的重要一步。

学习建议: 在虚拟机中反复练习各种命令选项 结合 ping、netstat、ss 等命令综合使用 了解不同Linux发行版的网络配置文件差异 逐步过渡到使用 ip 命令进行网络管理

同分类 · 其他命令文档