Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
执行netstat后,其输出结果为
[vagrant@centos6 ~]$ netstat|more Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 10.0.2.15:ssh 10.0.2.2:52091 ESTABLISHED Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 11 [ ] DGRAM 11021 /dev/log unix 2 [ ] DGRAM 11556 @/org/freedesktop/hal/udev_event unix 2 [ ] DGRAM 8986 @/org/kernel/udev/udevd unix 3 [ ] STREAM CONNECTED 17777 unix 3 [ ] STREAM CONNECTED 17776
从整体上看,netstat的输出结果可以分为两个部分:
OUTPUT Active Internet connections (TCP, UDP, raw) Proto The protocol (tcp, udp, raw) used by the socket. Recv-Q The count of bytes not copied by the user program connected to this socket. Send-Q The count of bytes not acknowledged by the remote host.
一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指的是接收队列和发送队列,这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积,这种情况非常少见。
Active UNIX domain Sockets Proto The protocol (usually unix) used by the socket. RefCnt The reference count (i.e. attached processes via this socket). Flags The flags displayed is SO_ACCEPTON (displayed as ACC), SO_WAITDATA (W) or SO_NOSPACE (N). SO_ACCECPTON is used on unconnected sockets if their corresponding processes are waiting for a connect request. The other flags are not of nor- mal interest. Type There are several types of socket access: SOCK_DGRAM The socket is used in Datagram (connectionless) mode. SOCK_STREAM This is a stream (connection) socket. SOCK_RAW The socket is used as a raw socket.
另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,
RefCnt表示连接到本套接口上的进程数量,
Types显示套接口的类型,
State显示套接口当前的状态,
Path表示连接到套接口的其它进程使用的路径名。
-a (all)显示所有选项,netstat默认不显示LISTEN相关 -t (tcp)仅显示tcp相关选项 -u (udp)仅显示udp相关选项 -n 拒绝显示别名,能显示数字的全部转化成数字。(重要) -l 仅列出有在 Listen (监听) 的服務状态 -p 显示建立相关链接的程序名(macOS中表示协议 -p protocol) -r 显示路由信息,路由表 -e 显示扩展信息,例如uid等 -s 按各个协议进行统计 (重要) -c 每隔一个固定时间,执行该netstat命令。
提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到
列出所有端口: netstat -a 列出所有tcp端口: netstat -at 列出所有udp端口: netstat -au
只显示监听端口: netstat -l 只列出所有监听tcp端口: netstat -lt 只列出所有监听udp端口: netstat -lu 只列出所有监听UNIX端口: netstat -lx
显示所有端口的统计信息 netstat -s
[vagrant@centos6 ~]$ netstat -s Ip: 2081 total packets received 2 with invalid addresses 0 forwarded 0 incoming packets discarded 2079 incoming packets delivered 1319 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: 0 active connections openings 3 passive connection openings 0 failed connection attempts 0 connection resets received 1 connections established 1957 segments received 1103 segments send out 0 segments retransmited 0 bad segments received. 1 resets sent Udp: 122 packets received 0 packets to unknown port received. 0 packet receive errors 216 packets sent UdpLite: TcpExt: 9 delayed acks sent 1 packets directly queued to recvmsg prequeue. 730 packets header predicted 5 acknowledgments not containing data received 1036 predicted acknowledgments 0 TCP data loss events IpExt: InOctets: 146500 OutOctets: 132340
显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
# netstat -st # netstat -su
netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
[root@centos6 ~]# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 10.0.2.15:ssh 10.0.2.2:52964 ESTABLISHED 3008/sshd tcp 0 0 10.0.2.15:ssh 10.0.2.2:52091 ESTABLISHED 2813/sshd
当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。该参数可以加速output的输出,因为不用进行比对查询。
# netstat -an
# 如果只是不想让这三个名称中的一个被显示,使用以下命令 # netsat -a --numeric-ports # netsat -a --numeric-hosts # netsat -a --numeric-users
netstat 将每隔一秒输出网络信息。
# netstat -t -c 2
netstat --verbose
[root@centos6 ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 eth0 # 注意: 使用 netstat -rn 显示数字格式,不查询主机名称,效果等价于route -n
并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
[root@centos6 ~]# netstat -apn | grep ssh tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1438/sshd tcp 0 0 10.0.2.15:22 10.0.2.2:52964 ESTABLISHED 3008/sshd tcp 0 0 10.0.2.15:22 10.0.2.2:52091 ESTABLISHED 2813/sshd tcp 0 0 :::22 :::* LISTEN 1438/sshd unix 3 [ ] STREAM CONNECTED 18443 3008/sshd unix 3 [ ] STREAM CONNECTED 18442 3011/sshd unix 2 [ ] DGRAM 18439 3008/sshd unix 3 [ ] STREAM CONNECTED 17777 2813/sshd unix 3 [ ] STREAM CONNECTED 17776 2816/sshd unix 2 [ ] DGRAM 17773 2813/sshd
找出运行在指定端口的进程
[root@centos6 ~]# netstat -an | grep ':22' tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 10.0.2.15:22 10.0.2.2:52964 ESTABLISHED tcp 0 0 10.0.2.15:22 10.0.2.2:52091 ESTABLISHED tcp 0 0 :::22 :::* LISTEN
[root@centos6 ~]# netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 12125 0 0 0 5474 0 0 0 BMRU lo 65536 0 0 0 0 0 0 0 0 0 LRU [root@centos6 ~]# netstat -ie Kernel Interface table eth0 Link encap:Ethernet HWaddr 52:54:00:5D:A4:AF inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fe5d:a4af/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:12131 errors:0 dropped:0 overruns:0 frame:0 TX packets:5477 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7474852 (7.1 MiB) TX bytes:580343 (566.7 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
查看连接某服务端口最多的的IP地址/TCP各种状态列表:
统计TCP状态:netstat -ant | awk 'NR>=3 {++State[$6]} END {for (key in State) print key,State[key]}' ss -ant | awk 'NR>=2 {++State[$1]} END {for (key in State) print key,State[key]}' 首先是netstat内容输出: [vagrant@localhost ~]$ netstat -ant Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:39198 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 10.0.2.15:22 10.0.2.2:58263 ESTABLISHED tcp 0 0 :::51041 :::* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN 命令注释:netstat -n | awk '/^tcp/ {++state[$NF]} END {for(a in state) print a, state[a]}' /^tcp/ 滤出tcp开头的记录,屏蔽udp, socket等无关记录。 state[] 相当于定义了一个名叫state的数组 NF The number of fields in the current input record.(表示输入数据的字段总数,如上所示的记录,NF等于6) $NF 表示某个字段的值,如上所示的记录,$NF也就是$6,表示第6个字段的值,也就是TIME_WAIT state[$NF] 表示数组元素的值,如上所示的记录,就是state[TIME_WAIT]状态的连接数 ++state[$NF] 表示把某个数加一,如上所示的记录,就是把state[TIME_WAIT]状态的连接数加一 END 表示在最后阶段要执行的命令 for(key in state) 遍历数组 print key,"\t",state[key] 打印数组的键和值,中间可以用\t制表符分割,美化一下。 NR>=3 The total number of input records seen so far.(NR表示已输入数据行数目,即表示从第二行开始处理,NR和NF都是内建变量) 同理ss工具下的awk语句应该修改成:ss -ant | awk 'NR>=2 {++State[$1]} END {for (key in State) print key,State[key]}' [root@localhost ~]# ss -ant State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:6379 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* ESTAB 0 0 10.0.2.15:22 10.0.2.2:58352 ESTAB 0 0 10.0.2.15:22 10.0.2.2:57127 LISTEN 0 128 :::80 :::* LISTEN 0 32 :::21 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
统计每个IP的连接数:netstat -n | awk '/^tcp/ {print $5}' | awk -F: '{print $1}' | sort | uniq -c| sort -rn ss -n | awk '/^tcp/ {print $5}' | awk -F: '{print $1}' | sort | uniq -c| sort -rn ---------------------------------------------------(netstat -n in CentOS6)---------------------------------------------------- Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 10.0.2.15:22 10.0.2.2:58262 ESTABLISHED tcp 0 0 10.0.2.15:22 10.0.2.2:58263 ESTABLISHED ---------------------------------------------------(ss -n in CentOS7)--------------------------------------------------------- Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp ESTAB 0 0 10.0.2.15:22 10.0.2.2:58352 tcp ESTAB 0 0 10.0.2.15:22 10.0.2.2:57127 netstat -n :以数字形式显示地址信息 awk '/^tcp/ {print $5}' :筛选以tcp开头的行,并打印5th filed. awk -F: '{print $1}' :以冒号为字段分隔符,打印第一个字段(-F fs 指定行中分隔数据字段的字段分隔符) uniq -c :去除重复项目,-c 进行计数 sort -rn :进行排序,-r 反向排序 -n 使用纯数字进行排序
参考资料:https://www.cnblogs.com/echo1937/p/6677325.html