Debian 系统中 ifconfig 命令使用实例详解

|| 技术教程 | 2025-06-04

ifconfig(interface configuration)是 Linux 系统中用于 配置和显示网络接口信息 的经典命令。在 Debian 及其衍生系统(如 Ubuntu)中,ifconfig 属于 net-tools 软件包,部分新系统默认未安装,需手动安装。


1. 安装 ifconfig(如未自带)

sudo apt update
sudo apt install net-tools  # 安装 ifconfig

2. 基本用法

(1) 查看所有网络接口信息

ifconfig

输出示例

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:11:22:33:44:55  txqueuelen 1000  (Ethernet)
        RX packets 1234  bytes 123456 (123.4 KB)
        TX packets 567  bytes 78901 (78.9 KB)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
  • eth0:物理网卡(有线)。

  • lo:本地回环接口(用于本地通信)。

  • wlan0:无线网卡(如果有)。

(2) 查看特定网卡信息

ifconfig eth0  # 仅显示 eth0 的配置

3. 配置网络接口

(1) 启用/禁用网卡

sudo ifconfig eth0 up    # 启用 eth0
sudo ifconfig eth0 down  # 禁用 eth0

(2) 设置 IP 地址和子网掩码

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

验证

ifconfig eth0 | grep "inet "

(3) 设置网关(需配合 route 命令)

sudo route add default gw 192.168.1.1  # 设置默认网关

(4) 修改 MAC 地址(临时生效)

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

注意:重启后恢复原 MAC,永久修改需编辑 /etc/network/interfaces


4. 高级用法

(1) 查看所有接口(包括未激活的)

ifconfig -a

(2) 显示简要信息(仅 IP 和 MAC)

ifconfig | grep -E "eth0|lo|wlan0" -A1 | grep "inet\|ether"

(3) 监控网络流量

watch -n 1 ifconfig eth0  # 每 1 秒刷新 eth0 流量

关键字段

  • RX packets:接收的数据包。

  • TX packets:发送的数据包。


5. 常见问题

(1) ifconfig 命令不存在?

  • 未安装 net-tools,运行:

    sudo apt install net-tools
  • 或使用 ip 命令(现代替代方案):

    ip a  # 查看所有接口
    ip link set eth0 up  # 启用网卡

(2) 修改 IP 后无法联网?

  • 检查网关是否正确:

    route -n  # 查看网关
  • 重启网络服务:

    sudo systemctl restart networking

6. ifconfig vs ip 命令对比

功能 ifconfig 命令 ip 命令(推荐)
查看所有接口 ifconfig ip a 或 ip addr
启用/禁用网卡 ifconfig eth0 up/down ip link set eth0 up/down
修改 IP ifconfig eth0 192.168.1.100 ip addr add 192.168.1.100/24 dev eth0
查看路由表 route -n ip route

在Debian系统中,ifconfig命令主要用于配置、启用和检查网络设备状态。以下是一些常见的ifconfig命令示例:

显示所有网络设备的状态:

复制AI写代码

1

ifconfig

显示特定网络设备的信息(如eth0):

复制AI写代码

1

ifconfig
eth0

启动网络设备(如eth0):

复制AI写代码

1

sudo
ifconfig eth0 up

停止网络设备(如eth0):

复制AI写代码

1

sudo
ifconfig eth0 down

为网络设备分配IP地址(如eth0,IP地址为192.168.1.100,子网掩码为255.255.255.0):

复制AI写代码

1

sudo
ifconfig eth0 192.168.1.100 netmask
255.255.255.0

清除网络设备的IP地址(如eth0):

复制AI写代码

1

sudo
ifconfig eth0 release

设置网络设备的广播地址(如eth0,广播地址为192.168.1.255):

复制AI写代码

1

sudo
ifconfig eth0 broadcast 192.168.1.255

调整网络设备的最大传输单元(如eth0,MTU值为1500):

复制AI写代码

1

sudo
ifconfig eth0 mtu 1500

查询网络设备的物理地址:

复制AI写代码

1

ifconfig
eth0 | grep hw

需要注意的是,在一些较新的Debian版本中,ifconfig命令可能已经被移除,建议改用ip命令代替。例如,上述命令可以通过以下ip命令实现:

显示所有网络设备的状态:

复制AI写代码

1

ip
addr

显示特定网络设备的信息(如eth0):

复制AI写代码

1

ip
addr show eth0

启动网络设备(如eth0):

复制AI写代码

1

sudo
ip link set eth0 up

停止网络设备(如eth0):

复制AI写代码

1

sudo
ip link set eth0 down

为网络设备分配IP地址(如eth0,IP地址为192.168.1.100,子网掩码为255.255.255.0):

复制AI写代码

1

sudo
ip addr add 192.168.1.100/24 dev eth0

清除网络设备的IP地址(如eth0):

复制AI写代码

1

sudo
ip addr del 192.168.1.100/24 dev eth0

设置网络设备的广播地址(如eth0,广播地址为192.168.1.255):

复制AI写代码

1

sudo
ip addr add broadcast 192.168.1.255 dev
eth0

调整网络设备的最大传输单元(如eth0,MTU值为1500):

复制AI写代码

1

sudo
ip link set eth0 mtu 1500

查询网络设备的物理地址:

复制AI写代码

1

ip
link show eth0 | grep link/ether

以上就是Debian系统中ifconfig命令使用实例的详细内容

想了解更多精彩内容,请关注艾特安卓网!