什么是netstat?

如果你手头有linux系统,你直接输入man netstat,就可以得到帮助信息。man对于netstat的解释非常言简意赅,只有一句简短的描述:

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

中文意思是:netstat可以用来显示网络连接、路由表、接口统计、伪连接和组播成员。

netstat命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检查本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能够提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。

如果你的计算机有时候收到的数据报导致出错数据或故障,你不必感到奇怪,TCP/IP可以容许这些类型的错误,并能够自动重发数据报。但如果累计的出错情况数目占到所接受到的IP数据报相当大的百分比,或者它的数目正在迅速增长,那么你就应该使用netstat查一查为什么会出现这种情况了。

【命令格式】

netstat [-cCeFghilMnNoprstuvVwx] [-A<网络类型>][--ip]

【命令功能】

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

【命令参数】

-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”参数相同。

【实例展示】

  • netstat

1
2
3
4
5
6
7
8
9
[root@node1 ~] # netstat
Active Internet connections (w /o  servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State  
tcp        0     52 node1.magedu.com: ssh         172.16.18.2:50297           ESTABLISHED
Active UNIX domain sockets (w /o  servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  14     [ ]         DGRAM                    9828    /dev/log
unix  2      [ ]         DGRAM                    7786   @ /org/kernel/udev/udevd
unix  2      [ ]         DGRAM                    10754  @ /org/freedesktop/hal/udev_event

     本选项从整体上看,netstat的输出结果可以分为两个部分:

一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。

另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。

Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。

状态说明:

LISTEN:                 侦听来自远方的TCP端口的连接请求

SYN-SENT:            再发送连接请求后等待匹配的连接请求(如果有大量这样的状态包,检查是否中招了)

SYN-RECEIVED:    再收到和发送一个连接请求后等待对方对连接请求的确认(如有大量此状态,估计被flood攻击了)

ESTABLISHED:     代表一个打开的连接

FIN-WAIT-1:           等待远程TCP连接中断请求,或先前的连接中断请求的确认

FIN-WAIT-2:           从远程TCP等待连接中断请求

CLOSE-WAIT:        等待从本地用户发来的连接中断请求

CLOSING:              等待远程TCP对连接中断的确认

LAST-ACK:            等待原来的发向远程TCP的连接中断请求的确认(不是什么好东西,此项出现,检查是否被攻击)

TIME-WAIT:           等待足够的时间以确保远程TCP接收到连接中断请求的确认

CLOSED:               没有任何连接状态

  • netstat -s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[root@node1 ~] # netstat -s
Ip:
     125329 total packets received
     14383 with invalid addresses
     0 forwarded
     0 incoming packets discarded
     97126 incoming packets delivered
     33554 requests sent out
     10 dropped because of missing route
Icmp:
     457 ICMP messages received
     72 input ICMP message failed.
     ICMP input histogram:
         destination unreachable: 457
     373 ICMP messages sent
     0 ICMP messages failed
     ICMP output histogram:
         destination unreachable: 373
IcmpMsg:
         InType3: 457
         OutType3: 373
Tcp:
     139 active connections openings
     20 passive connection openings
     31 failed connection attempts
     23 connection resets received
     1 connections established
     91423 segments received
     32616 segments send out
     73 segments retransmited
     0 bad segments received.
     23 resets sent
Udp:
     56 packets received
     0 packets to unknown port received.
     0 packet receive errors
     492 packets sent
UdpLite:
TcpExt:
     145 packets pruned from receive queue because of socket buffer overrun
     8 ICMP packets dropped because they were out-of-window
     3 TCP sockets finished  time  wait  in  fast timer
     183 delayed acks sent
     5 delayed acks further delayed because of locked socket
     Quick ack mode was activated 8  times
     28 packets directly queued to recvmsg prequeue.
     4 packets directly received from prequeue
     81707 packets header predicted
     1925 acknowledgments not containing data received
     708 predicted acknowledgments
     TCPDSACKUndo: 10
     1 congestion windows recovered after partial ack
     0 TCP data loss events
     1 retransmits  in  slow start
     42 other TCP timeouts
     17054 packets collapsed  in  receive queue due to low socket buffer
     9 DSACKs sent  for  old packets
     12 DSACKs received
     1 connections reset due to unexpected data
     19 connections reset due to early user close
     3 connections aborted due to timeout
     TCPSackShiftFallback: 1
     TCPBacklogDrop: 16
IpExt:
     InMcastPkts: 35
     InBcastPkts: 5155
     InOctets: 135205393
     OutOctets: 2321147
     InMcastOctets: 1120
     InBcastOctets: 800085

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

  • netstat -e

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@node1 ~] # netstat -e
Active Internet connections (w /o  servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       User       Inode 
tcp        0     52 node1.magedu.com: ssh         172.16.18.2:50297           ESTABLISHED root       64806  
Active UNIX domain sockets (w /o  servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  14     [ ]         DGRAM                    9828    /dev/log
unix  2      [ ]         DGRAM                    7786   @ /org/kernel/udev/udevd
unix  2      [ ]         DGRAM                    10754  @ /org/freedesktop/hal/udev_event
unix  2      [ ]         DGRAM                    64850
unix  2      [ ]         DGRAM                    64713
unix  3      [ ]         STREAM     CONNECTED     57258   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     57257
unix  2      [ ]         DGRAM                    41768
unix  3      [ ]         STREAM     CONNECTED     11830   /var/run/dbus/system_bus_socket

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

  • netstat -r

1
2
3
4
[root@node1 ~] # netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
172.16.0.0      *               255.255.0.0     U         0 0          0 eth0

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

  • netstat -a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@node1 ~] # netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State  
tcp        0      0 *:sunrpc                    *:*                         LISTEN  
tcp        0      0 *:56818                     *:*                         LISTEN  
tcp        0      0 *: ssh                        *:*                         LISTEN  
tcp        0      0 localhost:ipp               *:*                         LISTEN  
tcp        0      0 localhost:smtp              *:*                         LISTEN  
tcp        0      0 localhost:x11- ssh -offset    *:*                         LISTEN  
tcp        0      0 node1.magedu.com: ssh         172.16.18.2:50297           ESTABLISHED
tcp        0      0 *:sunrpc                    *:*                         LISTEN  
tcp        0      0 *:49906                     *:*                         LISTEN  
tcp        0      0 *: ssh                        *:*                         LISTEN  
tcp        0      0 localhost:ipp               *:*                         LISTEN  
tcp        0      0 localhost:smtp              *:*                         LISTEN  
tcp        0      0 localhost:x11- ssh -offset    *:*                         LISTEN  
udp        0      0 *:sunrpc                    *:*                                 
udp        0      0 *:ipp                       *:*                                 
udp        0      0 *:con                       *:*                                 
udp        0   1536 *:ntp                       *:*                                 
udp        0      0 *:817                       *:*                                 
udp        0      0 *:50398                     *:*                                 
udp        0      0 *:sunrpc                    *:*                                 
udp        0      0 *:con                       *:*                                 
udp        0      0 *:ntp                       *:*                                 
udp        0      0 *:33805                     *:*                                 
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     10732  @ /var/run/hald/dbus-LUTxU382Gz
unix  2      [ ACC ]     STREAM     LISTENING     10727  @ /var/run/hald/dbus-blwR0z9jUZ
unix  2      [ ACC ]     STREAM     LISTENING     10063   /var/run/rpcbind .sock
unix  2      [ ACC ]     STREAM     LISTENING     11336  public /cleanup
unix  2      [ ACC ]     STREAM     LISTENING     10163   /var/run/dbus/system_bus_socket

     本选项显示一个所有的有效连接信息列表,包含已建立的连接(ESTABLISHED),也包含监听连接请求的哪些连接(LISTENING)。

  • netstat -n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@node1 ~] # netstat -n
Active Internet connections (w /o  servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State  
tcp        0     52 172.16.18.7:22              172.16.18.2:50297           ESTABLISHED
Active UNIX domain sockets (w /o  servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  14     [ ]         DGRAM                    9828    /dev/log
unix  2      [ ]         DGRAM                    7786   @ /org/kernel/udev/udevd
unix  2      [ ]         DGRAM                    10754  @ /org/freedesktop/hal/udev_event
unix  2      [ ]         DGRAM                    64850
unix  2      [ ]         DGRAM                    64713
unix  3      [ ]         STREAM     CONNECTED     57258   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     57257
unix  2      [ ]         DGRAM                    41768
unix  3      [ ]         STREAM     CONNECTED     11830   /var/run/dbus/system_bus_socket

    本选项是显示所有已建立的有效连接。

找出程序运行的端口

1
2
3
4
5
6
7
[root@node1 ~] # netstat -ap | grep ssh
tcp        0      0 *: ssh                        *:*                         LISTEN      1688 /sshd       
tcp        0      0 localhost:x11- ssh -offset    *:*                         LISTEN      20222 /sshd      
tcp        0     52 node1.magedu.com: ssh         172.16.18.2:50297           ESTABLISHED 20222 /sshd      
tcp        0      0 *: ssh                        *:*                         LISTEN      1688 /sshd       
tcp        0      0 localhost:x11- ssh -offset    *:*                         LISTEN      20222 /sshd      
unix  2      [ ]         DGRAM                    64850  20222 /sshd

查找运行在指定端口的进程

1
2
[root@node1 ~] # netstat -anpt | grep 80
tcp        0      0 :::80                       :::*                        LISTEN      20841 /httpd


更多的示例这里就不在一一举出了,更多内容将持续更新!!