Linux command - netstat
nestat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
1 | netstat [options] |
netstat 常见用法:
- (none): By default, netstat displays a list of open sockets.
-a, --all
: Show both listening and non-listening sockets.-l, --listening
: Show only listening sockets. (these are omitted by default.)-t, --tcp
: 显示 TCP 协议连接的 socket。-u, --udp
: 显示 UDP 协议连接的 socket。-n, --numeric
: 显示数字形式的地址,而不用解析主机、端口和用户名。-p, --program
: 显示 socket 连接程序的 PID 和名称。-c, --continuous
: 每秒钟打印一次输出结果。-s, --statistics
: Display summary statistics for each protocol. 显示每种协议收发数据包的统计结果。-g, --groups
: Display multicast group membership information for IPv4 and IPv6. 显示 IPv4 和 IPv6 的 IGMP 组播组成员关系信息。-i, --interfaces
: Display a table of all network interfaces. 显示网络接口信息。-r, --route
: Display the kernel routing tables. 显示内核路由表信息。-M, --masquerade
: Display a list of masqueraded connections.-A, --protocol
: 指定要显示哪些的地址族的连接,多个协议用逗号分隔。常见地址族,如:inet, inet6, unix, ipx, ax25, netrom, econet, ddp, bluetooth。也可以作为参数使用: –inet|-4, –inet6|-6, –unix|-x, –ipx, –ax25, –netrom, –ddp, –bluetooth。
输出结果:
- Proto: 协议
- Recv-Q: 此 socket 连接的应用程序未拷贝的字节数。
- Send-Q: 远程主机未确认的字节数。
- Local Address: socket 连接的本地地址信息 (本地主机名和端口)。
- Foreign Address: socket 连接的远程地址信息 (远程主机名和端口)。
- State: socket 状态 (ESTABLISHED, SYN_SENT, SYN_RECV, FIN_WAIT1, FIN_WAIT2, TIME_WAIT, CLOSED, CLOSE_WAIT, LAST_ACK, LISTEN, CLOSING, UNKNOWN)。
- User: The username or the user id (UID) of the owner of the socket.
- PID/Program_name: socket 连接的所属应用程序的 PID 和程序名称。
等等
1 | # 显示连接状态的 socket |
注意当 netstat -p
显示的程序信息看不到的时候 (如-
),可能是权限问题。使用 sudo 提权或切换 root 用户执行命令。