netstat 命令详解

NAME
netstat – Print network connections, routing tables, interface statistics, masquerade connections, and
multicast memberships

SYNOPSIS
netstat [address_family_options] [–tcp|-t] [–udp|-u] [–raw|-w] [–listening|-l] [–all|-a]
[–numeric|-n] [–numeric-hosts][–numeric-ports][–numeric-ports] [–symbolic|-N]
[–extend|-e[–extend|-e]] [–timers|-o] [–program|-p] [–verbose|-v] [–continuous|-c] [delay]

netstat {–route|-r} [address_family_options] [–extend|-e[–extend|-e]] [–verbose|-v] [–numeric|-n]
[–numeric-hosts][–numeric-ports][–numeric-ports] [–continuous|-c] [delay]

netstat {–interfaces|-i} [iface] [–all|-a] [–extend|-e[–extend|-e]] [–verbose|-v] [–program|-p]
[–numeric|-n] [–numeric-hosts][–numeric-ports][–numeric-ports] [–continuous|-c] [delay]

netstat {–groups|-g} [–numeric|-n] [–numeric-hosts][–numeric-ports][–numeric-ports] [–continu-
ous|-c] [delay]

netstat {–masquerade|-M} [–extend|-e] [–numeric|-n] [–numeric-hosts][–numeric-ports][–numeric-
ports] [–continuous|-c] [delay]

netstat {–statistics|-s} [–tcp|-t] [–udp|-u] [–raw|-w] [delay]

netstat {–version|-V}

netstat {–help|-h}

address_family_options:

[–protocol={inet,unix,ipx,ax25,netrom,ddp}[,…]] [–unix|-x] [–inet|–ip] [–ax25] [–ipx]
[–netrom] [–ddp]

中文解释:
-t 显示tcp 连接
-u 显示udp连接
-x 显示unix域协议的连接
–ip 显示ip协议的连接,是和unix域协议相对而言的,但和-x选项不冲突,可同时使用,包括了tcp协议的连接和udp协议的连接
-l 显示listen状态的连接
-a 显示所有状态的连接,是从状态上来讲的all,默认只显示established状态的连接
-p 显示端口对应的程序(program)和pid
-n 以数字的形式显示主机和端口,可以只针对主机名或端口号,–numeric-hosts 或 –numeric-ports
-s 统计网络的状态,此时也可以用-t指定只统计tcp的 -u只统计udp的
delay 参数可以指定延迟多长时间重新执行该命令
-V 显示netstat的版本号

 

注:
1. 这里的连接包括listen ,-t -u可同时使用
2. ip协议有tcp udp两种类型,unix域协议也有stream和dgram 两种类型
3. 上述翻译仅供参考,本人E文有限,不要轻信啊

 

下面说几种 netstat的用法:
1. 查看服务器的TCP状态:
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
ESTABLISHED 1423
FIN_WAIT1 1
FIN_WAIT2 262
SYN_SENT 1
TIME_WAIT 962

2.netstat中tcp连接状态描述:
状态 描述
CLOSED: 无连接是活动的或正在进行
LISTEN: 服务器在等待进入呼叫
SYN_RECV: 一个连接请求已经到达,等待确认
SYN_SENT: 应用已经开始,打开一个连接
ESTABLISHED: 正常数据传输状态
FIN_WAIT1: 应用说它已经完成
FIN_WAIT2: 另一边已同意释放
ITMED_WAIT: 等待所有分组死掉
CLOSING: 两边同时尝试关闭
TIME_WAIT: 另一边已初始化一个释放
LAST_ACK: 等待所有分组死掉

3. 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.
      // 是发送出去,但是还没有被确认的数据

   Local Address
       Address and port number of the local end of the socket.  Unless the –numeric (-n) option is specified, the  socket
       address  is  resolved  to  its canonical host name (FQDN), and the port number is translated into the corresponding
       service name.

   Foreign Address
       Address and port number of the remote end of the socket.  Analogous to "Local Address."

   State
       The state of the socket. Since there are no states in raw mode and usually no states used in UDP, this  column  may
       be left blank. Normally this can be one of several values:

       ESTABLISHED
              The socket has an established connection.

       SYN_SENT
              The socket is actively attempting to establish a connection.

       SYN_RECV
              A connection request has been received from the network.

       FIN_WAIT1
              The socket is closed, and the connection is shutting down.

       FIN_WAIT2
              Connection is closed, and the socket is waiting for a shutdown from the remote end.

       TIME_WAIT
              The socket is waiting after close to handle packets still in the network.

       CLOSED The socket is not being used.

       CLOSE_WAIT
              The remote end has shut down, waiting for the socket to close.

       LAST_ACK
              The remote end has shut down, and the socket is closed. Waiting for acknowledgement.

       LISTEN The  socket  is  listening for incoming connections.  Such sockets are not included in the output unless you
              specify the –listening (-l) or –all (-a) option.

       CLOSING
              Both sockets are shut down but we still don’t have all our data sent.

       UNKNOWN
              The state of the socket is unknown.

   User
       The username or the user id (UID) of the owner of the socket.

   PID/Program name
       Slash-separated pair of the process id (PID) and process name of the  process  that  owns  the  socket.   –program
       causes  this column to be included.  You will also need superuser privileges to see this information on sockets you
       don’t own.  This identification information is not yet available for IPX sockets.

   Timer
       (this needs to be written)

   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
       normal 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.

       SOCK_RDM
              This one serves reliably-delivered messages.

       SOCK_SEQPACKET
              This is a sequential packet socket.

       SOCK_PACKET
              Raw interface access socket.

       UNKNOWN
              Who ever knows what the future will bring us – just fill in here 🙂

   State
       This field will contain one of the following Keywords:

       FREE   The socket is not allocated

       LISTENING
              The socket is listening for a connection request.  Such sockets are only included in the output if you spec-
              ify the –listening (-l) or –all (-a) option.

       CONNECTING
              The socket is about to establish a connection.

       CONNECTED
              The socket is connected.

       DISCONNECTING
              The socket is disconnecting.

       (empty)
              The socket is not connected to another one.

       UNKNOWN
              This state should never happen.

   PID/Program name
       Process  ID (PID) and process name of the process that has the socket open.  More info available in Active Internet
       connections section written above.

   Path
       This is the path name as which the corresponding processes attached to the socket.

留下评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据