码迷,mamicode.com
首页 > Web开发 > 详细

netstat命令

时间:2015-06-17 01:47:12      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:



查看当前tcp监听端口
[op@TIM html]$ netstat -nltp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 127.0.0.1:8307              0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 127.0.0.1:5942              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:631               0.0.0.0:*                   LISTEN      -                   
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:445                 0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:50686               0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:902                 0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:139                 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:6000                0.0.0.0:*                   LISTEN      -                   
tcp        0      0 ::1:8307                    :::*                        LISTEN      -                   
tcp        0      0 :::22                       :::*                        LISTEN      -                   
tcp        0      0 ::1:631                     :::*                        LISTEN      -                   
tcp        0      0 ::1:25                      :::*                        LISTEN      -                   
tcp        0      0 :::33945                    :::*                        LISTEN      -                   
tcp        0      0 :::8443                     :::*                        LISTEN      -                   
tcp        0      0 :::443                      :::*                        LISTEN      -                   
tcp        0      0 :::445                      :::*                        LISTEN      -                   
tcp        0      0 :::139                      :::*                        LISTEN      -                   
tcp        0      0 :::111                      :::*                        LISTEN      -                   
tcp        0      0 :::80                       :::*                        LISTEN      -                   
tcp        0      0 :::6000                     :::*                        LISTEN      -  


查看当前tcp监听端口, 需要显示监听的程序名,当不清楚mysql的监听端口时比较好用
[op@TIM html]$ netstat -tlp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 localhost:8307              *:*                         LISTEN      -                   
tcp        0      0 *:ftp                       *:*                         LISTEN      -                   
tcp        0      0 localhost:5942              *:*                         LISTEN      -                   
tcp        0      0 *:ssh                       *:*                         LISTEN      -                   
tcp        0      0 localhost:ipp               *:*                         LISTEN      -                   
tcp        0      0 localhost:smtp              *:*                         LISTEN      -                   
tcp        0      0 *:https                     *:*                         LISTEN      -                   
tcp        0      0 *:microsoft-ds              *:*                         LISTEN      -                   
tcp        0      0 *:50686                     *:*                         LISTEN      -                   
tcp        0      0 *:ideafarm-door             *:*                         LISTEN      -                   
tcp        0      0 *:netbios-ssn               *:*                         LISTEN      -                   
tcp        0      0 *:sunrpc                    *:*                         LISTEN      -                   
tcp        0      0 *:x11                       *:*                         LISTEN      -                   
tcp        0      0 localhost:8307              *:*                         LISTEN      -                   
tcp        0      0 *:ssh                       *:*                         LISTEN      -                   
tcp        0      0 localhost:ipp               *:*                         LISTEN      -                   
tcp        0      0 localhost:smtp              *:*                         LISTEN      -                   
tcp        0      0 *:33945                     *:*                         LISTEN      -                   
tcp        0      0 *:pcsync-https              *:*                         LISTEN      -                   
tcp        0      0 *:https                     *:*                         LISTEN      -                   
tcp        0      0 *:microsoft-ds              *:*                         LISTEN      -                   
tcp        0      0 *:netbios-ssn               *:*                         LISTEN      -                   
tcp        0      0 *:sunrpc                    *:*                         LISTEN      -                   
tcp        0      0 *:http                      *:*                         LISTEN      -                   
tcp        0      0 *:x11                       *:*                         LISTEN      -    






pidof mysqld


看mysql的监听端口
[op@TIM html]$ sudo netstat -nulp | grep 17734
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17734/mysqld  

显示UDP端口号的使用情况
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
udp        0      0 *:bootpc                    *:*                                     -                   
udp        0      0 *:bootpc                    *:*                                     -                   
udp        0      0 *:37974                     *:*                                     -                   
udp        0      0 localhost:1001              *:*                                     -                   
udp        0      0 *:mdns                      *:*                                     -                   
udp        0      0 *:kerberos-iv               *:*                                     -                   
udp        0      0 *:sunrpc                    *:*                                     -                   
udp        0      0 *:ipp                       *:*                                     -                   
udp        0      0 *:servstat                  *:*                                     -                   
udp        0      0 localhost:ntp               *:*                                     -                   
udp        0      0 localhost:ntp               *:*                                     -                   
udp        0      0 localhost:ntp               *:*                                     -                   
udp        0      0 localhost:ntp               *:*                                     -                   
udp        0      0 localhost:ntp               *:*                                     -                   
udp        0      0 *:ntp                       *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 localhost:netbios-ns        *:*                                     -                   
udp        0      0 *:netbios-ns                *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 localhost:netbios-dgm       *:*                                     -                   
udp        0      0 *:netbios-dgm               *:*                                     -                   
udp        0      0 *:783                       *:*                                     -                   
udp        0      0 *:45346                     *:*                                     -                   
udp        0      0 *:sunrpc                    *:*                                     -                   
udp        0      0 *:34032                     *:*                                     -                   
udp        0      0 *:servstat                  *:*                                     -                   
udp        0      0 fe80::250:56ff:fec0:1:ntp   *:*                                     -                   
udp        0      0 fe80::250:56ff:fec0:8:ntp   *:*                                     -                   
udp        0      0 fe80::d253:49ff:fe81:e26:ntp *:*                                     -                   
udp        0      0 fe80::6202:92ff:fe62:302:ntp *:*                                     -                   
udp        0      0 localhost:ntp               *:*                                     -                   
udp        0      0 *:ntp                       *:*                                     -    


显示网卡列表
# netstat -i


显示网络统计信息
# netstat -s



显示监听的套接口
# netstat -l

列出所有 tcp 端口
命令:netstat -at

统计机器中网络连接各个状态个数
命令:netstat -a | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}‘
[op@TIM html]$ netstat -a | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}‘
TIME_WAIT 3
CLOSE_WAIT 2
ESTABLISHED 13
LISTEN 26


把状态全都取出来后使用uniq -c统计后再进行排序
命令:netstat -nat |awk ‘{print $6}‘|sort|uniq -c
[op@TIM html]$ netstat -a | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}‘
TIME_WAIT 3
CLOSE_WAIT 2
ESTABLISHED 13
LISTEN 26
[op@TIM html]$ netstat -nat |awk ‘{print $6}‘|sort|uniq -c
      2 CLOSE_WAIT
      1 established)
     14 ESTABLISHED
      1 Foreign
     26 LISTEN
      1 TIME_WAIT



查看连接某服务端口最多的的IP地址
命令:netstat -nat | grep "192.168.120.20:16067" |awk ‘{print $5}‘|awk -F: ‘{print $4}‘|sort|uniq -c|sort -nr|head -20


实例16:找出程序运行的端口
命令:netstat -ap | grep ssh

在 netstat 输出中显示 PID 和进程名称
命令:netstat -pt


找出运行在指定端口的进程
命令:netstat -anpt | grep ‘:16064‘



















    查看所有tcp监听端口:netstat -ntlp
    查看所有udp监听端口:netstat -nulp
    查看所有tcp/udp监听端口:netstat -nutlp
    查看所有已经建立的连接:netstat -antp
    查看连接某服务端口最多的的IP地址:netstat -nat | grep ‘:80‘ | awk ‘{print $5}‘ | awk -F: ‘{print $1}‘ | sort | uniq -c | sort -nr | head -20
    网络连接数目:netstat -an | grep -E “^(tcp)” | cut -c 68- | sort | uniq -c | sort -n
    按状态查看连接连接数量:netstat -an|awk ‘/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}‘或netstat -nat|awk ‘{print $6}‘|sort|uniq -c|sort -rn
    按IP查看连接数量:netstat -ntu | awk ‘{print $5}‘ | cut -d: -f1 | sort | uniq -c | sort -n或netstat -antu | awk ‘$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}‘
    查看80端口的连接,并排序:netstat -an -t | grep ‘:80‘ | grep ESTABLISHED | awk ‘{printf "%s %s\n",$5,$6}‘ | sort
    查看当前的memcache连接:netstat -n | grep :11211
    查看占用端口8080的进程:netstat -tnlp | grep 8080或lsof -i:8080
    查看所有php进程:netstat -anp | grep php | grep ^tcp
    查看所有nginx进程:netstat -anp | grep nginx | grep ^tcp
    查看80端口连接数最多的20个IP:netstat -anlp|grep 80|grep tcp|awk ‘{print $5}‘|awk -F: ‘{print $1}‘|sort|uniq -c|sort -nr|head -n20
    查找较多time_wait连接:netstat -n|grep TIME_WAIT|awk ‘{print $5}‘|sort|uniq -c|sort -rn|head -n20
    找查较多的SYN连接:netstat -an | grep SYN | awk ‘{print $5}‘ | awk -F: ‘{print $1}‘ | sort | uniq -c | sort -nr | more
    输出正在占用UDP和TCP端口的所有程序连带计时器信息:netstat -putona:
    查看指定IP的连接:netstat -nat | grep "192.168.1.222":



netstat –s
本选项能够按照各个协议分别显示其统计数据。如果我们的应用程序(如Web浏览器)运行速度比较慢,或者不能显示Web页之类的数据,那么我们就可以用本选项来查看一下所显示的信息。我们需要仔细查看统计数据的各行,找到出错的关键字,进而确定问题所在。

·netstat –e
本选项用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量)。

·netstat –r
本选项可以显示关于路由表的信息,类似于后面所讲使用route print命令时看到的 信息。除了显示有效路由外,还显示当前有效的连接。

·netstat –a
本选项显示一个所有的有效连接信息列表,包括已建立的连接(ESTABLISHED),也包括监听连接请(LISTENING)的那些连接。

·netstat –n
显示所有已建立的有效连接。









其实我常用的是 netstat -tnl | grep 443  (查看443端口是否被占用),如果有当前是root用户,我喜欢用netstat -pnl | grep 443 (还可显示出占用本机443端口的进程PID)。
netstat
功能说明:显示网络状态。
语  法:netstat [-acCeFghilMnNoprstuvVwx] [-A<网络类型>][--ip]
补充说明:利用netstat指令可让你得知整个Linux系统的网络情况。
参  数:
  -a或--all   显示所有连线中的Socket。
  -A<网络类型>或--<网络类型>   列出该网络类型连线中的相关地址。
  -c或--continuous   持续列出网络状态。
  -C 或--cache   显示路由器配置的快取信息。
  -e或--extend   显示网络其他相关信息。
  -F或 --fib   显示FIB。
  -g或--groups   显示多重广播功能群组组员名单。
  -h或--help   在线帮助。
  -i 或--interfaces   显示网络界面信息表单。
  -l或--listening   显示监控中的服务器的Socket。
  -M 或--masquerade   显示伪装的网络连线。
  -n或--numeric   直接使用IP地址,而不通过域名服务器。
  -N 或--netlink或--symbolic   显示网络硬件外围设备的符号连接名称。
  -o或--timers   显示计时器。
  -p 或--programs   显示正在使用Socket的程序识别码和程序名称。
  -r或--route   显示 Routing Table。
  -s或--statistice   显示网络工作信息统计表。
  -t或--tcp   显示TCP 传输协议的连线状况。
  -u或--udp   显示UDP传输协议的连线状况。
  -v或--verbose   显示指令执行过程。
  -V 或--version   显示版本信息。
  -w或--raw   显示RAW传输协议的连线状况。
  -x或--unix   此参数的效果和指定"-A unix"参数相同。
  --ip或--inet   此参数的效果和指定"-A inet"参数相同。

2.网络连接状态详解
共有12中可能的状态,前面11种是按照TCP连接建立的三次握手和TCP连接断开的四次挥手过程来描述的。
1)、LISTEN:首先服务端需要打开一个socket进行监 听,状态为LISTEN./* The socket is listening for incoming connections. 侦听来自远方TCP端口的连接请求 */

2)、SYN_SENT:客户端通过应用程序调用connect进行active open.于是客户端tcp发送一个SYN以请求建立一个连接.之后状态置为SYN_SENT./*The socket is actively attempting to establish a connection. 在发送连接请求后等待匹配的连接请求 */

3)、SYN_RECV:服务端应发出ACK确认客户端的 SYN,同时自己向客户端发送一个SYN. 之后状态置为SYN_RECV/* A connection request has been received from the network. 在收到和发送一个连接请求后等待对连接请求的确认 */

4)、ESTABLISHED: 代表一个打开的连接,双方可以进行或已经在数据交互了。/* The socket has an established connection. 代表一个打开的连接,数据可以传送给用户 */

5)、FIN_WAIT1:主动关闭(active close)端应用程序调用close,于是其TCP发出FIN请求主动关闭连接,之后进入FIN_WAIT1状态./* The socket is closed, and the connection is shutting down. 等待远程TCP的连接中断请求,或先前的连接中断请求的确认 */

6)、CLOSE_WAIT:被动关闭(passive close)端TCP接到FIN后,就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT./* The remote end has shut down, waiting for the socket to close. 等待从本地用户发来的连接中断请求 */

7)、FIN_WAIT2:主动关闭端接到ACK后,就进入了 FIN-WAIT-2 ./* Connection is closed, and the socket is waiting for a shutdown from the remote end. 从远程TCP等待连接中断请求 */

8)、LAST_ACK:被动关闭端一段时间后,接收到文件结束 符的应用程序将调用CLOSE关闭连接。这导致它的TCP也发送一个 FIN,等待对方的ACK.就进入了LAST-ACK ./* The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原来发向远程TCP的连接中断请求的确认 */

9)、TIME_WAIT:在主动关闭端接收到FIN后,TCP 就发送ACK包,并进入TIME-WAIT状态。/* The socket is waiting after close to handle packets still in the network.等待足够的时间以确保远程TCP接收到连接中断请求的确认 */

10)、CLOSING: 比较少见./* Both sockets are shut down but we still don’t have all our data sent. 等待远程TCP对连接中断的确认 */

11)、CLOSED: 被动关闭端在接受到ACK包后,就进入了closed的状态。连接结束./* The socket is not being used. 没有任何连接状态 */

12)、UNKNOWN: 未知的Socket状态。/* The state of the socket is unknown. */
SYN: (同步序列编号,Synchronize Sequence Numbers)该标志仅在三次握手建立TCP连接时有效。表示一个新的TCP连接请求。
ACK: (确认编号,Acknowledgement Number)是对TCP请求的确认标志,同时提示对端系统已经成功接收所有数据。
FIN: (结束标志,FINish)用来结束一个TCP回话.但对应端口仍处于开放状态,准备接收后续数据。

PS:在windows下有个小工具挺好的,TCPView is a Windows program that will show you detailed listings of all TCP and UDP endpoints on your system, including the local and remote addresses and state of TCP connections.见 http://technet.microsoft.com/en-us/sysinternals/bb897437 ; 当然如果要详细分析数据包,可选用sniffer、Wireshark等更强大的工具。

参考资料:
http://linux.sheup.com/linux/4/31225.html
http://hi.baidu.com/mqbest_come_on/blog/item/18526dcef73d791a00e928e5.html
http://www.daxigua.com/archives/1355

netstat命令

标签:

原文地址:http://www.cnblogs.com/x113/p/4559187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!