netstat
命令可以用来查询整个系统的网络状态。百度百科的定义如下:
netstat的定义是: netstat是在内核中访问网络连接状态及其相关信息的程序,它能提供tcp连接,tcp和udp监听,进程内存管理的相关报告。
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
上面的输出包含两个部分:
1、active internet connections 有源tcp连接,其中"recv-q"和"send-q"指接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
2、active unix domain sockets 有源unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
对于internet connections部分输出参数,做下重点介绍
tcp
、udp
和tcp6
等;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
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论