netstat
netstat
是一个用于显示网络相关信息的命令行工具。它可以显示当前系统的网络连接状态、路由表、接口统计信息等。
下面是一些常见的 netstat
命令选项和用法:
- 显示所有活动的网络连接:
netstat -a
- 显示所有正在监听的端口:
netstat -l
- 显示所有 TCP 连接:
netstat -t
- 显示所有 UDP 连接:
netstat -u
- 显示 PID 和进程名称:
netstat -p
- 显示路由表信息:
netstat -r
- 显示接口统计信息:
netstat -i
- 显示网络连接的数字格式 IP 地址和端口号:
netstat -n
- 显示协议栈的统计信息
netstat -s
如,
查看所有的tcp连接
显示协议栈统计信息
pcl@robot:~/cProject$ netstat -s
Ip:
Forwarding: 2
976 total packets received
0 forwarded
0 incoming packets discarded
974 incoming packets delivered
840 requests sent out
Icmp:
0 ICMP messages received
0 input ICMP message failed
ICMP input histogram:
0 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
Tcp:
29 active connection openings
4 passive connection openings
9 failed connection attempts
0 connection resets received
4 connections established
563 segments received
476 segments sent out
28 segments retransmitted
0 bad segments received
2 resets sent
Udp:
394 packets received
0 packets to unknown port received
0 packet receive errors
340 packets sent
0 receive buffer errors
0 send buffer errors
IgnoredMulti: 18
UdpLite:
TcpExt:
3 TCP sockets finished time wait in fast timer
7 delayed acks sent
Quick ack mode was activated 2 times
135 packet headers predicted
91 acknowledgments not containing data payload received
189 predicted acknowledgments
TCPLostRetransmit: 21
TCPTimeouts: 28
TCPDSACKOldSent: 1
TCPRcvCoalesce: 5
TCPAutoCorking: 12
TCPSynRetrans: 28
TCPOrigDataSent: 333
TCPDelivered: 353
IpExt:
InMcastPkts: 117
OutMcastPkts: 57
InBcastPkts: 18
OutBcastPkts: 6
InOctets: 423174
OutOctets: 112543
InMcastOctets: 10821
OutMcastOctets: 5673
InBcastOctets: 1220
OutBcastOctets: 284
InNoECTPkts: 1186
ss
netstat
与 ss
命令查看的信息都差不多,但是如果在生产环境中要查看这类信息的时候,尽量不要使用 netstat
命令,因为它的性能不好,在系统比较繁忙的情况下,如果频繁使用 netstat
命令则会对性能的开销雪上加霜,所以更推荐你使用性能更好的 ss
命令。
ss命令选项与netstat命令基本相同,如下所示
查看socket信息
显示协议栈统计信息
可以看到,ss命令显示的协议栈统计信息比netstat要少