关于linux:大数据开发Linux系统入门之netstat-命令学习

42次阅读

共计 11917 个字符,预计需要花费 30 分钟才能阅读完成。

netstat 命令用于显示与 IP、TCP、UDP 和 ICMP 协定相干的统计数据,个别用于测验本机各端口的网络连接状况。netstat 是在内核中拜访网络及相干信息的程序,大数据培训它能提供 TCP 连贯,TCP 和 UDP 监听,过程内存治理的相干报告。
如果你的计算机有时候接管到的数据报导致出错数据或故障,你不用感到奇怪,TCP/IP 能够答应这些类型的谬误,并可能主动重发数据报。但如果累计的出错状况数目占到所接管的 IP 数据报相当大的百分比,或者它的数目正迅速减少,那么你就应该应用 netstat 查一查为什么会呈现这些状况了。
1.命令格局:
netstat -acCeFghilMnNoprstuvVwx[–ip]
2.命令性能:
netstat 用于显示与 IP、TCP、UDP 和 ICMP 协定相干的统计数据,个别用于测验本机各端口的网络连接状况。
3.命令参数:
-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”参数雷同。
4.应用实例:
实例 1:无参数应用
命令:
netstat
输入:
[root@localhost ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 268 192.168.120.204:ssh 10.2.0.68:62420 ESTABLISHED
udp 0 0 192.168.120.204:4371 10.58.119.119:domain ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [] DGRAM 1491 @/org/kernel/udev/udevd
unix 4 [] DGRAM 7337 /dev/log
unix 2 [] DGRAM 708823
unix 2 [] DGRAM 7539
unix 3 [] STREAM CONNECTED 7287
unix 3 [] STREAM CONNECTED 7286
[root@localhost ~]#
阐明:
从整体上看,netstat 的输入后果能够分为两个局部:
一个是 Active Internet connections,称为有源 TCP 连贯,其中 ”Recv-Q” 和 ”Send-Q” 指的是接管队列和发送队列。这些数字个别都应该是 0。如果不是则示意软件包正在队列中沉积。这种状况只能在非常少的状况见到。
另一个是 Active UNIX domain sockets,称为有源 Unix 域套接口(和网络套接字一样,然而只能用于本机通信,性能能够进步一倍)。

Proto 显示连贯应用的协定,RefCnt 示意连贯到本套接口上的过程号,Types 显示套接口的类型,State 显示套接口以后的状态,Path 示意连贯到套接口的其它过程应用的路径名。
套接口类型:
-t:TCP
-u:UDP
-raw:RAW 类型
–unix:UNIX 域类型
–ax25:AX25 类型
–ipx:ipx 类型
–netrom:netrom 类型
状态阐明:
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:没有任何连贯状态
实例 2:列出所有端口
命令:
netstat -a
输入:
[root@localhost ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:smux : LISTEN
tcp 0 0 :svn :* LISTEN
tcp 0 0 :ssh :* LISTEN
tcp 0 284 192.168.120.204:ssh 10.2.0.68:62420 ESTABLISHED
udp 0 0 localhost:syslog :
udp 0 0 :snmp :*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ACC] STREAM LISTENING 708833 /tmp/ssh-yKnDB15725/agent.15725
unix 2 [ACC] STREAM LISTENING 7296 /var/run/audispd_events
unix 2 [] DGRAM 1491 @/org/kernel/udev/udevd
unix 4 [] DGRAM 7337 /dev/log
unix 2 [] DGRAM 708823
unix 2 [] DGRAM 7539
unix 3 [] STREAM CONNECTED 7287
unix 3 [] STREAM CONNECTED 7286
[root@localhost ~]#
阐明:
显示一个所有的无效连贯信息列表,包含已建设的连贯(ESTABLISHED),也包含监听连贯请(LISTENING)的那些连贯。
实例 3:显示以后 UDP 连贯情况
命令:
netstat -nu
输入:
[root@andy ~]# netstat -nu
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 ::ffff:192.168.12:53392 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:56723 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:56480 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:58154 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:44227 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:36954 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:53984 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:57703 ::ffff:192.168.9.120:10000 ESTABLISHED
udp 0 0 ::ffff:192.168.12:53613 ::ffff:192.168.9.120:10000 ESTABLISHED
[root@andy ~]#
阐明:
实例 4:显示 UDP 端口号的应用状况
命令:
netstat -apu
输入:
[root@andy ~]# netstat -apu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 :57604 :* 28094/java
udp 0 0 :40583 :* 21220/java
udp 0 0 :45451 :* 14583/java
udp 0 0 ::ffff:192.168.12:53392 ::ffff:192.168.9.120:ndmp ESTABLISHED 19327/java
udp 0 0 :52370 :* 15841/java
udp 0 0 ::ffff:192.168.12:56723 ::ffff:192.168.9.120:ndmp ESTABLISHED 15841/java
udp 0 0 :44182 :* 31757/java
udp 0 0 :48155 :* 5476/java
udp 0 0 :59808 :* 17333/java
udp 0 0 ::ffff:192.168.12:56480 ::ffff:192.168.9.120:ndmp ESTABLISHED 28094/java
udp 0 0 ::ffff:192.168.12:58154 ::ffff:192.168.9.120:ndmp ESTABLISHED 15429/java
udp 0 0 :36780 :* 10091/java
udp 0 0 :36795 :* 24594/java
udp 0 0 :41922 :* 20506/java
udp 0 0 ::ffff:192.168.12:44227 ::ffff:192.168.9.120:ndmp ESTABLISHED 17333/java
udp 0 0 :34258 :* 8866/java
udp 0 0 :55508 :* 11667/java
udp 0 0 :36055 :* 12425/java
udp 0 0 ::ffff:192.168.12:36954 ::ffff:192.168.9.120:ndmp ESTABLISHED 16532/java
udp 0 0 ::ffff:192.168.12:53984 ::ffff:192.168.9.120:ndmp ESTABLISHED 20506/java
udp 0 0 ::ffff:192.168.12:57703 ::ffff:192.168.9.120:ndmp ESTABLISHED 31757/java
udp 0 0 ::ffff:192.168.12:53613 ::ffff:192.168.9.120:ndmp ESTABLISHED 3199/java
udp 0 0 :56309 :* 15429/java
udp 0 0 :54007 :* 16532/java
udp 0 0 :39544 :* 3199/java
udp 0 0 :43900 :* 19327/java
[root@andy ~]#
阐明:
实例 5:显示网卡列表
命令:
netstat -i
输入:
[root@andy ~]# netstat -i
Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 151818887 0 0 0 198928403 0 0 0 BMRU
lo 16436 0 107235 0 0 0 107235 0 0 0 LRU
[root@andy ~]#
阐明:
实例 6:显示组播组的关系
命令:
netstat -g
输入:
[root@andy ~]# netstat -g
IPv6/IPv4 Group Memberships
Interface RefCnt Group


lo 1 all-systems.mcast.net
eth0 1 all-systems.mcast.net
lo 1 ff02::1
eth0 1 ff02::1:ffff:9b0c
eth0 1 ff02::1
[root@andy ~]#
阐明:
实例 7:显示网络统计信息
命令:
netstat -s
输入:
[root@localhost ~]# netstat -s
Ip:
530999 total packets received
0 forwarded
0 incoming packets discarded
530999 incoming packets delivered
8258 requests sent out
1 dropped because of missing route
Icmp:
90 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:

destination unreachable: 17
echo requests: 1
echo replies: 72

106 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:

destination unreachable: 8
echo request: 97
echo replies: 1

IcmpMsg:

InType0: 72
InType3: 17
InType8: 1
OutType0: 1
OutType3: 8
OutType8: 97

Tcp:
8 active connections openings
15 passive connection openings
8 failed connection attempts
3 connection resets received
1 connections established
3132 segments received
2617 segments send out
53 segments retransmited
0 bad segments received.
252 resets sent
Udp:
0 packets received
0 packets to unknown port received.
0 packet receive errors
5482 packets sent
TcpExt:
1 invalid SYN cookies received
1 TCP sockets finished time wait in fast timer
57 delayed acks sent
Quick ack mode was activated 50 times
60 packets directly queued to recvmsg prequeue.
68 packets directly received from backlog
4399 packets directly received from prequeue
520 packets header predicted
51 packets header predicted and directly queued to user
1194 acknowledgments not containing data received
21 predicted acknowledgments
0 TCP data loss events
1 timeouts after reno fast retransmit
9 retransmits in slow start
42 other TCP timeouts
3 connections aborted due to timeout
IpExt:
InBcastPkts: 527777
阐明:
依照各个协定别离显示其统计数据。如果咱们的应用程序(如 Web 浏览器)运行速度比较慢,或者不能显示 Web 页之类的数据,那么咱们就能够用本选项来查看一下所显示的信息。咱们须要认真查看统计数据的各行,找到出错的关键字,进而确定问题所在。
实例 8:显示监听的套接口
命令:
netstat -l
输入:
[root@localhost ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:smux : LISTEN
tcp 0 0 :svn :* LISTEN
tcp 0 0 :ssh :* LISTEN
udp 0 0 localhost:syslog :
udp 0 0 :snmp :*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ACC] STREAM LISTENING 708833 /tmp/ssh-yKnDB15725/agent.15725
unix 2 [ACC] STREAM LISTENING 7296 /var/run/audispd_events
[root@localhost ~]#
阐明:
实例 9:显示所有已建设的无效连贯
命令:
netstat -n
输入:
[root@localhost ~]# netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 268 192.168.120.204:22 10.2.0.68:62420 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [] DGRAM 1491 @/org/kernel/udev/udevd
unix 4 [] DGRAM 7337 /dev/log
unix 2 [] DGRAM 708823
unix 2 [] DGRAM 7539
unix 3 [] STREAM CONNECTED 7287
unix 3 [] STREAM CONNECTED 7286
[root@localhost ~]#
阐明:
实例 10:显示对于以太网的统计数据
命令:
netstat -e
输入:
[root@localhost ~]# netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode
tcp 0 248 192.168.120.204:ssh 10.2.0.68:62420 ESTABLISHED root 708795
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [] DGRAM 1491 @/org/kernel/udev/udevd
unix 4 [] DGRAM 7337 /dev/log
unix 2 [] DGRAM 708823
unix 2 [] DGRAM 7539
unix 3 [] STREAM CONNECTED 7287
unix 3 [] STREAM CONNECTED 7286
[root@localhost ~]#
阐明:
用于显示对于以太网的统计数据。它列出的我的项目包含传送的数据报的总字节数、谬误数、删除数、数据报的数量和播送的数量。这些统计数据既有发送的数据报数量,也有接管的数据报数量。这个选项能够用来统计一些根本的网络流量)
实例 11:显示对于路由表的信息
命令:
netstat -r
输入:
[root@localhost ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]#
阐明:
实例 12:列出所有 tcp 端口
命令:
netstat -at
输入:
[root@localhost ~]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:smux : LISTEN
tcp 0 0 :svn :* LISTEN
tcp 0 0 :ssh :* LISTEN
tcp 0 284 192.168.120.204:ssh 10.2.0.68:62420 ESTABLISHED
[root@localhost ~]#
阐明:
实例 13:统计机器中网络连接各个状态个数
命令:
netstat -a | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
输入:
[root@localhost ~]# netstat -a | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
ESTABLISHED 1
LISTEN 3
[root@localhost ~]#
阐明:
实例 14:把状态全都取出来后应用 uniq -c 统计后再进行排序
命令:
netstat -nat |awk ‘{print $6}’|sort|uniq -c
输入:
[root@andy ~]# netstat -nat |awk ‘{print $6}’|sort|uniq -c
14 CLOSE_WAIT
1 established)
578 ESTABLISHED
1 Foreign
43 LISTEN
5 TIME_WAIT
[root@andy ~]# netstat -nat |awk ‘{print $6}’|sort|uniq -c|sort -rn
576 ESTABLISHED
43 LISTEN
14 CLOSE_WAIT
5 TIME_WAIT
1 Foreign
1 established)
[root@andy ~]#
阐明:
实例 15:查看连贯某服务端口最多的的 IP 地址
命令:
netstat -nat | grep “192.168.120.20:16067” |awk ‘{print $5}’|awk -F: ‘{print $4}’|sort|uniq -c|sort -nr|head -20

输入:
[root@andy ~]# netstat -nat | grep “192.168.120.20:16067” |awk ‘{print $5}’|awk -F: ‘{print $4}’|sort|uniq -c|sort -nr|head -20
8 10.2.1.68
7 192.168.119.13
6 192.168.119.201
6 192.168.119.20
6 192.168.119.10
4 10.2.1.199
3 10.2.1.207
2 192.168.120.20
2 192.168.120.15
2 192.168.119.197
2 192.168.119.11
2 10.2.1.206
2 10.2.1.203
2 10.2.1.189
2 10.2.1.173
1 192.168.120.18
1 192.168.119.19
1 10.2.2.227
1 10.2.2.138
1 10.2.1.208
[root@andy ~]#
阐明:
实例 16:找出程序运行的端口
命令:
netstat -ap | grep ssh
输入:
[root@andy ~]# netstat -ap | grep ssh
tcp 0 0 :ssh :* LISTEN 2570/sshd
tcp 0 0 ::ffff:192.168.120.206:ssh ::ffff:10.2.1.205:54508 ESTABLISHED 13883/14
tcp 0 0 ::ffff:192.168.120.206:ssh ::ffff:10.2.0.68:62886 ESTABLISHED 20900/6
tcp 0 0 ::ffff:192.168.120.206:ssh ::ffff:10.2.2.131:52730 ESTABLISHED 20285/sshd: root@no
unix 2 [ACC] STREAM LISTENING 194494461 20900/6 /tmp/ssh-cXIJj20900/agent.20900
unix 3 [] STREAM CONNECTED 194307443 20285/sshd: root@no
unix 3 [] STREAM CONNECTED 194307441 20285/sshd: root@no
[root@andy ~]#
阐明:
实例 17:在 netstat 输入中显示 PID 和过程名称
命令:
netstat -pt
输入:
[root@localhost ~]# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 248 192.168.120.204:ssh 10.2.0.68:62420 ESTABLISHED 15725/0
[root@localhost ~]#
阐明:
netstat -p 能够与其它开关一起应用,就能够增加“PID/ 过程名称”到 netstat 输入中,这样 debugging 的时候能够很不便的发现特定端口运行的程序。
实例 18:找出运行在指定端口的过程
命令:
netstat -anpt | grep ‘:16064’
输入:
[root@andy ~]# netstat -anpt | grep ‘:16064’
tcp 0 0 :::16064 :::* LISTEN 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.201:6462 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:26341 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:32208 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:32207 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:51303 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:51302 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50020 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50019 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56155 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50681 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50680 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:52136 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56989 ESTABLISHED 24594/java
tcp 0 0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56988 ESTABLISHED 24594/java
[root@andy ~]#

阐明:
运行在端口 16064 的过程 id 为 24596,再通过 ps 命令就能够找到具体的应用程序了。

正文完
 0