Linux netstat简介和使用

概述

  • Netstat用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。

  • 输出结果可以分为两个部分:
    一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指%0A的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
    另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
    Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。

参数

usage: netstat [-veenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vnNcaeol] [<Socket> ...]
       netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]

        -r, --route                display routing table            #显示路由表
        -I, --interfaces=<Iface>   display interface table for <Iface>  #显示网络接口表
        -i, --interfaces           display interface table  #显示网络接口表
        -g, --groups               display multicast group memberships      #显示多播组成员
        -s, --statistics           display networking statistics (like SNMP)    #显示网络统计数据,如IP/ICMP/SNMP/..各协议统计。
        -M, --masquerade           display masqueraded connections     #显示伪装的连接

        -v, --verbose              be verbose   #详细信息
        -n, --numeric              don't resolve names     #不做名字解析
        --numeric-hosts            don't resolve host names     #不做主机名解析
        --numeric-ports            don't resolve port names     #不做端口名解析
        --numeric-users            don't resolve user names     #不做用户名解析
        -N, --symbolic             resolve hardware names   
        -e, --extend               display other/more information   #显示更多信息,用户名,inode
        -p, --programs             display PID/Program name for sockets     #显示pid和程序名字
        -c, --continuous           continuous listing   #持续的列出相关信息

        -l, --listening            display listening server sockets     #显示处于监听状态的套接字
        -a, --all, --listening     display all sockets (default: connected)     #显示所有的套接字
        -o, --timers               display timers       #显示计时器
        -F, --fib                  display Forwarding Information Base (default)       #使用-rF查看路由表时,显示转发信息
        -C, --cache                display routing cache instead of FIB     #使用-rC查看路由表时,显示详细的路由缓存
        -T, --notrim               stop trimming long addresses     #停止修剪长地址
        -Z, --context              display SELinux security context for sockets     #显示套接字的SELINUX上下文

  <Iface>: Name of interface to monitor/list.
  <Socket>={-t|--tcp} {-u|--udp} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
  <AF>=Use '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25) 

状态:

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. */

常用命令实例

  1. 列出所有端口 (包括监听和未监听的)
    netstat -a
  2. 列出所有 tcp 端口
    netstat -at
  3. 只列出监听中的连接
    netstat -tnl
  4. 将显示内核路由表
    netstat -nr

通常会有很多端口列出来,如果是要查看某个端口相关的,可以配合grep命令使用:netstat -ant | grep 5327

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值