Ubuntu 22.04 net-tools(ifconfig/netstat/route) 安装与命令参数详解

在 Ubuntu 22.04 上安装 net-tools(ifconfig/netstat/route/arp)并逐参数实测各命令的输入输出,涵盖网卡查看、套接字排障与路由管理。

Linux 网络诊断与调试net-toolsLinux命令行网络诊断2026/10/06

Ubuntu 22.04 net-tools(ifconfig/netstat/route) 安装与命令参数详解

目标:在 Ubuntu 22.04 上安装 net-tools,并逐参数实测 ifconfig、netstat、route、arp 四个传统网络工具的输入与输出。本文命令与输出均为 Ubuntu 22.04(net-tools 2.10-alpha)实测回显。

环境信息

  • 操作系统:Ubuntu 22.04 LTS
  • net-tools 版本:2.10-alpha
  • 网卡:eth0(veth),IP 172.22.0.2/16,MAC 72:18:8c:51:b4:75
  • 网关:172.22.0.1
  • 对端主机:172.22.0.3(CentOS 8),本机作为 HTTP 服务端监听 22/3306/6379/8000/8080
  • 说明:Ubuntu 22.04 默认不预装 net-tools,需手动安装

工具概述

net-tools 是 Linux 传统网络工具包,包含:

  • ifconfig:查看/配置网卡地址与状态(被 ip addr 取代)
  • netstat:显示网络连接、路由表、接口与协议统计(被 ss 取代)
  • route:查看/操作路由表(被 ip route 取代)
  • arp:查看/操作 ARP 邻居表(被 ip neigh 取代)

这些工具虽被 iproute2 取代,但仍在大量旧脚本与排查场景中广泛使用,掌握其输出便于兼容旧环境。

安装

在线安装(apt)

Ubuntu 22.04 默认不预装 net-tools,需显式安装:

sudo apt update
sudo apt install -y net-tools

安装后验证:

netstat --version
net-tools 2.10-alpha
Fred Baumgarten, Alan Cox, Bernd Eckenfels, Phil Blundell, Tuan Hoang, Brian Micek and others
+NEW_ADDRT +RTF_IRTT +RTF_REJECT +FW_MASQUERADE +I18N +SELINUX
AF: (inet) +UNIX +INET +INET6 +IPX +AX25 +NETROM +X25 +ATALK +ECONET +ROSE -BLUETOOTH
HW:  +ETHER +ARC +SLIP +PPP +TUNNEL -TR +AX25 +NETROM +X25 +FR +ROSE +ASH +SIT +FDDI +HIPPI +HDLC/LAPB +EUI64

离线安装(.deb 包)

# 在可联网机器下载(含依赖)
cd /opt/netdiag-offline
apt-get download net-tools

# 目标机离线安装
sudo dpkg -i net-tools_*.deb

ifconfig 命令实测

查看所有活动网卡(默认)

用途:列出已启用的网卡地址、掩码、广播地址与收发统计。

ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.0.2  netmask 255.255.0.0  broadcast 172.22.255.255
        ether 72:18:8c:51:b4:75  txqueuelen 0  (Ethernet)
        RX packets 11462  bytes 90050638 (90.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6199  bytes 351041 (351.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 10  bytes 2255 (2.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 2255 (2.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

字段解读:flags 为接口标志;inet 为 IPv4 地址与掩码;ether 为 MAC;RX/TX packets|bytes 收发统计;errors/dropped 错误与丢包(0 表示健康)。

查看指定网卡

用途:只看某一块网卡。

ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.0.2  netmask 255.255.0.0  broadcast 172.22.255.255
        ether 72:18:8c:51:b4:75  txqueuelen 0  (Ethernet)
        RX packets 11463  bytes 90050708 (90.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6199  bytes 351041 (351.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

查看全部网卡(含未启用/无地址的接口)

用途:-a 显示所有接口,包括处于 down 状态或无地址的接口(本环境仅有 eth0/lo)。

ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.0.2  netmask 255.255.0.0  broadcast 172.22.255.255
        ether 72:18:8c:51:b4:75  txqueuelen 0  (Ethernet)
        RX packets 11463  bytes 90050708 (90.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6199  bytes 351041 (351.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 10  bytes 2255 (2.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 2255 (2.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

精简列表模式(短格式)

用途:-s 输出接口/MTU/收发统计的紧凑表格,便于脚本解析。

ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0      1500    11463      0      0 0          6199      0      0      0 BMRU
lo       65536       10      0      0 0            10      0      0      0 LRU

Flg 列:B 广播、M 组播、R 运行、U 启用、L 回环。

管道截取输出

用途:配合其他命令过滤关键信息。

ifconfig eth0 | grep 'inet '
        inet 172.22.0.2  netmask 255.255.0.0  broadcast 172.22.255.255

netstat 命令实测

默认输出(活动连接 + UNIX 套接字)

用途:显示当前活动 TCP 连接(不含监听)。

netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 netdiag-ubuntu:8000     netdiag-centos8.n:48476 TIME_WAIT
tcp        0      0 netdiag-ubuntu:8000     netdiag-centos8.n:48488 TIME_WAIT
tcp        0      0 netdiag-ubuntu:8000     netdiag-centos8.n:48520 TIME_WAIT
tcp        0      0 netdiag-ubuntu:8000     netdiag-centos8.n:48510 TIME_WAIT
tcp        0      0 netdiag-ubuntu:8000     netdiag-centos8.n:48504 TIME_WAIT
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path

只看 TCP 连接

netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 netdiag-ubuntu:8000     netdiag-centos8.n:48520 TIME_WAIT
tcp        0      0 netdiag-ubuntu:8000     netdiag-centos8.n:48510 TIME_WAIT

查看全部套接字(含监听)

用途:-a 显示服务器与已建立连接。

netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
tcp        0      0 localhost:34717         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN
udp        0      0 localhost:52928         0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path

只看监听(服务端)套接字

netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
tcp        0      0 localhost:34717         0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN
udp        0      0 localhost:52928         0.0.0.0:*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path

监听端口数值化清单

用途:-t -l -n 以数字形式列出监听 TCP 端口。

netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.11:34717        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN

监听端口 + 进程信息(最常用排查组合)

用途:-tulnp 组合,一屏列出所有监听端口及占用进程(含 UDP)。

netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3219/python3
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      3219/python3
tcp        0      0 127.0.0.11:34717        0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      3219/python3
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      3219/python3
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      3219/python3
udp        0      0 127.0.0.11:52928        0.0.0.0:*                           -

⚠️ -p 需要足够权限才能看到进程名;非 root 下该列显示 -。

数值化 + 进程 + 全部套接字(排查常用组合)

用途:完整展示所有连接(含对外)的端口与进程。

netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      3219/python3
...

查看内核路由表

用途:-rn 以数字形式显示路由表(等价 route -n)。

netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.22.0.1      0.0.0.0         UG        0 0          0 eth0
172.22.0.0      0.0.0.0         255.255.0.0     U         0 0          0 eth0

查看网卡收发统计(短格式)

netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0      1500    11624      0      0 0          6346      0      0      0 BMRU
lo       65536       32      0      0 0            32      0      0      0 LRU

查看各协议栈统计(类 SNMP 计数)

用途:-s 显示 IP/ICMP/TCP/UDP 等协议层累计计数,排查丢包、重传。

netstat -s
Ip:
    11624 total packets received
    0 forwarded
    0 incoming packets discarded
    11624 incoming packets delivered
    6346 requests sent out
    OutTransmits: 6346
Icmp:
    0 ICMP messages received
    ...
Tcp:
    11624 active connections openings
    ...

route 命令实测

查看路由表(默认,主机名反解)

route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         ops             0.0.0.0         UG    0      0        0 eth0
172.22.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

查看路由表(数值模式,不反解)

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.0.1      0.0.0.0         UG    0      0        0 eth0
172.22.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

添加/删除网段路由(演示,实测后已回滚)

route add -net 10.99.99.0/24 gw 172.22.0.1 eth0
route -n
route del -net 10.99.99.0/24

添加后确认:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.0.1      0.0.0.0         UG    0      0        0 eth0
10.99.99.0      172.22.0.1      255.255.255.0   UG    0      0        0 eth0
172.22.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

删除回滚后(route del 无输出、退出码 0),路由表恢复原状。

添加/删除主机路由(演示,实测后已回滚)

route add -host 10.99.0.20 dev eth0
route -n
route del -host 10.99.0.20

添加后确认(主机路由 Genmask 为 255.255.255.255、标志 UH):

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.0.1      0.0.0.0         UG    0      0        0 eth0
10.99.0.20      0.0.0.0         255.255.255.255 UH    0      0        0 eth0
172.22.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

arp 命令实测

用途:查看 ARP 邻居表。需先有流量填充缓存。

# 先产生对端流量,再查 ARP
curl -s -m3 http://172.22.0.3:8000/index.html >/dev/null 2>&1
arp -a
netdiag-centos8.netdiag-net (172.22.0.3) at 72:18:8c:51:b4:75 [ether] on eth0

帮助输出

ifconfig --help(节选)

Usage:
  ifconfig [-a] [-v] [-s] <interface> [<AF>] [<address>]
  ...

netstat --help(节选)

usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version} {-h|--help}
       netstat [-vWnNcaeolIx] [<Socket> ...]  netstat {}
       netstat [-vWeenNac] -i                netstat {-r|--route} {-a|--all}
...
-a, --all              display all sockets (default: connected)
-l, --listening        display listening server sockets
-n, --numeric          don't resolve names
-p, --programs         display PID/Program name for sockets
-t, --tcp              display only TCP sockets
-u, --udp              display only UDP sockets
-s, --statistics       display networking statistics

常见问题

  1. ifconfig: command not found:Ubuntu 22.04 不预装 net-tools,按上文安装即可。
  2. 端口被反解为服务名:netstat 不加 -n 会按 /etc/services 反解端口(如 8080→http-proxy)。排查建议恒加 -n。
  3. -p 看不到进程名:需要 root 权限;非 root 下进程列显示 -。
  4. net-tools 与 iproute2 并存:两者功能重叠可共存,ip/ss 是官方推荐的新工具,net-tools 用于兼容旧脚本。

参考链接