netstat
命令能够用来查询整个系统的网络状态。百度百科的定义以下:html
Netstat的定义是: Netstat是在内核中访问网络链接状态及其相关信息的程序,它能提供TCP链接,TCP和UDP监听,进程内存管理的相关报告。bash
Netstat是控制台命令,是一个监控TCP/IP网络的很是有用的工具,它能够显示路由表、实际的网络链接以及每个网络接口设备的状态信息。Netstat用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,通常用于检验本机各端口的网络链接状况。服务器
从上面的百科介绍咱们能够看出,netstat
命令在查询网络问题的时候十分有用。下面就来详细介绍下netstat
的用法。网络
netstat [-acCeFghilMnNoprstuvVwx][-A<网络类型>][--ip]
[root@SHA-L0161171 arthas]$ netstat -an Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:8004 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:11052 0.0.0.0:* LISTEN tcp 0 0 172.24.248.108:8004 192.168.202.31:57588 ESTABLISHED tcp 0 0 127.0.0.1:51092 127.0.0.1:8091 TIME_WAIT tcp 0 0 172.24.248.108:8004 192.168.202.38:32283 ESTABLISHED tcp 0 0 172.24.248.108:8004 192.168.202.38:58923 TIME_WAIT tcp 0 0 172.24.248.108:8004 192.168.202.32:39983 ESTABLISHED tcp 0 0 172.24.248.108:8004 192.168.202.37:38230 ESTABLISHED tcp 0 0 172.24.248.108:8004 192.168.202.34:5081 ESTABLISHED tcp 0 0 172.24.248.108:8004 192.168.202.32:17240 ESTABLISHED tcp 0 0 127.0.0.1:38784 127.0.0.1:12050 TIME_WAIT ... Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 3 [ ] DGRAM 18442 /run/systemd/notify unix 2 [ ] DGRAM 18444 /run/systemd/cgroups-agent unix 2 [ ] DGRAM 23822 /var/run/chrony/chronyd.sock unix 8 [ ] DGRAM 18455 /run/systemd/journal/socket unix 18 [ ] DGRAM 18457 /dev/log unix 2 [ ] DGRAM 14151 /var/run/nscd/socket unix 2 [ ] DGRAM 584 /run/systemd/shutdownd unix 3 [ ] STREAM CONNECTED 124439388 /run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 42312 /run/systemd/journal/stdout unix 3 [ ] STREAM CONNECTED 39909
上面的输出包含两个部分:dom
一、Active Internet connections 有源TCP链接,其中"Recv-Q"和"Send-Q"指接收队列和发送队列。这些数字通常都应该是0。若是不是则表示软件包正在队列中堆积。这种状况只能在很是少的状况见到。socket
二、Active UNIX domain sockets 有源Unix域套接口(和网络套接字同样,可是只能用于本机通讯,性能能够提升一倍)。tcp
对于Internet connections部分输出参数,作下重点介绍工具
Proto:表示链接使用的协议,常见的有tcp
、udp
和tcp6
等;性能
Recv-Q:指接收队列,这个数字通常都应该是0,若是不是则表示数据包正在队列中堆积。拿Tomcat服务器举个列子:当Tcp链接创建后,Tomcat服务器中会有一个accepter线程取出这个链接让worker线程处理,如今假如链接创建的速度远远大于accepter线程取链接的速度,那么链接就在队列中堆积,此时Recv-Q就会大于0;还有一种可能就是系统遭遇到了Dos攻击,已经不能再响应外部请求;spa
Send-Q:发送队列,对方没有收到的数据或者说没有Ack的,仍是本地缓冲区,若是发送队列Send-Q不能很快的清零,多是有应用向外发送数据包过快,或者是对方接收数据包不够快;
Local Address:表示本地地址,这个字段通常有三种形式的值:
Foreign Address:和本机通讯的外部IP地址,显示规则和Local Address相似,再也不赘述了;
State:表示链接状态,常见的链接状态以下:
LISTEN :The socket is listening for incoming connections (侦听来自远方TCP端口的链接请求) SYN_SENT:The socket is actively attempting to establish aconnection. (在发送链接请求后等待匹配的链接请求) SYN_RECV:A connection request has been received from the network. (在收到和发送一个链接请求后等待对链接请求的确认) ESTABLISHED:The socket has an established connection. (表明一个打开的链接,数据能够传送给用户) FIN_WAIT1: The socket is closed, and the connection is shutting down. (等待远程TCP的链接中断请求,或先前的链接中断请求的确认 ) CLOSE_WAIT:The remote end has shut down, waiting for the socketto close. (等待从本地用户发来的链接中断请求) FIN_WAIT2:Connection is closed, and the socket is waiting for a shutdownfrom the remote end. (从远程TCP等待链接中断请求 ) LAST_ACK: The remote end has shut down, and the socket is closed. Waiting foracknowledgement. (等待原来发向远程TCP的链接中断请求的确认) TIME_WAIT:Thesocket is waiting after close to handle packets still in the network (等待足够的时间以确保远程TCP接收到链接中断请求的确认) CLOSING: Bothsockets are shut down but we still don’t have all our datasent. (等待远程TCP对链接中断的确认) CLOSED:The socket is not being used. (没有任何链接状态 ) UNKNOWN:Thestate of the socket is unknown。
备注
1. 列出全部信息
netstat -a # 其中n表示使用IP地址表示机器信息,而不是使用域名 netstat -an
这个命令配合grep
最常使用。
2. 只显示监听端口
netstat -l
3. 显示PID和进程名称
netstat -anp
4. 持续输出状态信息
netstat -anpc
5. 查看链接某服务端口最多的的IP地址(前20个)
netstat -nat | grep "xx.xx.xx.xx:port" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20
输出
4 192.168.202.38 4 192.168.202.37 4 192.168.202.36 4 192.168.202.35 3 192.168.202.34 3 192.168.202.33 3 192.168.202.32 2 192.168.202.31
原文出处:https://www.cnblogs.com/54chensongxia/p/12144520.html