1.netstat详细说明
[wm@adx ~]$ netstat -help
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)
-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
-p, --programs display PID/Program name for sockets
-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)
-C, --cache display routing cache instead of FIB
-T, --notrim stop trimming long addresses
-Z, --context display SELinux security context for sockets
<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)
- 该命令的一般格式为 :
选项
- 命令中各选项的含义如下:
-c 每隔1秒就重新显示一遍,直到用户中断它。
-i 显示所有网络接口的信息,格式“netstat -i”。
-n 以网络IP地址代替名称,显示出网络连接情形。
-r显示核心路由表,格式同“route -e”。
-t 显示TCP协议的连接情况
-u 显示UDP协议的连接情况。
-v 显示正在进行的工作。
-p 显示指定协议信息。
-b 显示在创建每个连接或侦听端口时涉及的可执行程序。
-e 显示以太网统计。此选项可以与 -s 选项结合使用。
-f 显示外部地址的完全限定域名(FQDN)。
-o 显示拥有的与每个连接关联的进程 ID。
-s 显示每个协议的统计。
-x 显示 NetworkDirect 连接、侦听器和共享端点。
-y 显示所有连接的 TCP 连接模板。无法与其他选项结合使用。
interval 重新显示选定的统计,各个显示间暂停的 间隔秒数。按 CTRL+C 停止重新显示统计。如果省略,则 netstat 将打印当前的配置信息一次。
提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到
- Name接口的名字
Mtu 接口的最大传输单位
Net/Dest 接口所在的网络
Address 接口的IP地址
Ipkts 接收到的数据包数目
Ierrs 接收到时已损坏的数据包数目
Opkts 发送的数据包数目
Oeers 发送时已损坏的数据包数目
Collisions 由这个接口所记录的网络冲突数目
- 常用选项
netstat -s
——本选项能够按照各个协议分别显示其统计数据。如果你的应用程序(如Web浏览器)运行速度比较慢,或者不能显示Web页之类的数据,那么你就可以用本选项来查看一下所显示的信息。你需要仔细查看统计数据的各行,找到出错的关键字,进而确定问题所在。
netstat -e
——本选项用于显示关于以太网的统计数据,它列出的项目包括传送数据报的总字节数、错误数、删除数,包括发送和接收量(如发送和接收的字节数、数据包数[1] ),或有广播的数量。可以用来统计一些基本的网络流量。
netstat -r
——本选项可以显示关于路由表的信息,类似于后面所讲使用routeprint命令时看到的信息。除了显示有效路由外,还显示当前有效的连接。
netstat -a
——本选项显示一个所有的有效连接信息列表,包括已建立的连接(ESTABLISHED),也包括监听连接请求(LISTENING)的那些连接。
netstat -n
——显示所有已建立的有效连接。
netstat -p
——显示协议名查看某协议使用情况
- 常见状态
即连接状态。在原模式中没有状态,在用户数据报协议中也经常没有状态,于是状态列可以空出来。若有状态,通常取值为:
LISTEN 侦听来自远方的TCP端口的连接请求
SYN-SENT 在发送连接请求后等待匹配的连接请求
SYN-RECEIVED 在收到和发送一个连接请求后等待对方对连接请求的确认
ESTABLISHED 代表一个打开的连接
FIN-WAIT-1 等待远程TCP连接中断请求,或先前的连接中断请求的确认
FIN-WAIT-2 从远程TCP等待连接中断请求
CLOSE-WAIT 等待从本地用户发来的连接中断请求
CLOSING 等待远程TCP对连接中断的确认
LAST-ACK 等待原来的发向远程TCP的连接中断请求的确认
TIME-WAIT 等待足够的时间以确保远程TCP接收到连接中断请求的确认
CLOSED 没有任何连接状态
- 列出所有端口(包括未监听端口)
[wm@adx ~]$ netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:9312 *:* LISTEN
tcp 0 0 localhost.localdomain:2208 *:* LISTEN
tcp 0 0 *:nfs *:* LISTEN
tcp 0 0 *:31940 *:* LISTEN
tcp 0 0 *:27017 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
udp 0 0 *:mdns *:*
udp 0 0 *:sunrpc *:*
udp 0 0 *:ipp *:*
udp 0 0 *:26001 *:*
udp 0 0 *:45008 *:*
udp 0 0 *:mdns *:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 5026 @ISCSIADM_ABSTRACT_NAMESPACE
unix 2 [ ACC ] STREAM LISTENING 7275 /tmp/.font-unix/fs7100
unix 2 [ ACC ] STREAM LISTENING 7584 /tmp/mysql_cluster.sock
unix 2 [ ACC ] STREAM LISTENING 36185327 /tmp/mongodb-27017.sock
unix 2 [ ACC ] STREAM LISTENING 7408 /var/run/avahi-daemon/socket
unix 2 [ ACC ] STREAM LISTENING 5010 @ISCSID_UIP_ABSTRACT_NAMESPACE
从整体上看,netstat的输出结果可以分为两个部分:
一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指%0A的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。
- 列出所有tcp端口
[wm@adx ~]$ netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:9312 *:* LISTEN
tcp 0 0 localhost.localdomain:2208 *:* LISTEN
- 列出所有udp端口
[wm@adx ~]$ netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:53505 *:*
udp 0 0 *:nfs *:*
udp 0 0 *:tinc *:*
udp 0 0 *:tenfold *:*
udp 0 0 *:22090 *:*
udp 0 0 *:717 *:*
列出所有处于监听状态的 Sockets
- 只显示监听端口 netstat -l
[wm@adx ~]$ netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:9312 *:* LISTEN
tcp 0 0 localhost.localdomain:2208 *:* LISTEN
tcp 0 0 *:nfs *:* LISTEN
tcp 0 0 *:31940 *:* LISTEN
tcp 0 0 *:27017 *:* LISTEN
udp 0 0 *:732 *:*
udp 0 0 *:mdns *:*
udp 0 0 *:sunrpc *:*
udp 0 0 *:ipp *:*
udp 0 0 *:26001 *:*
udp 0 0 *:45008 *:*
udp 0 0 *:mdns *:*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 5026 @ISCSIADM_ABSTRACT_NAMESPACE
unix 2 [ ACC ] STREAM LISTENING 7275 /tmp/.font-unix/fs7100
unix 2 [ ACC ] STREAM LISTENING 7584 /tmp/mysql_cluster.sock
unix 2 [ ACC ] STREAM LISTENING 36185327 /tmp/mongodb-27017.sock
unix 2 [ ACC ] STREAM LISTENING 7408 /var/run/avahi-daemon/socket
unix 2 [ ACC ] STREAM LISTENING 5010 @ISCSID_UIP_ABSTRACT_NAMESPACE
unix 2 [ ACC ] STREAM LISTENING 6085 /var/run/dbus/system_bus_socket
- 只列出所有监听 tcp 端口 netstat -lt
[wm@adx ~]$ netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:9312 *:* LISTEN
tcp 0 0 localhost.localdomain:2208 *:* LISTEN
tcp 0 0 *:nfs *:* LISTEN
tcp 0 0 *:31940 *:* LISTEN
tcp 0 0 *:27017 *:* LISTEN
- 只列出所有监听 udp 端口 netstat -lu
[wm@adx ~]$ netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:53505 *:*
udp 0 0 *:nfs *:*
udp 0 0 *:tinc *:*
udp 0 0 *:tenfold *:*
- 只列出所有监听 UNIX 端口 netstat -lx
[wm@adx ~]$ netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 5026 @ISCSIADM_ABSTRACT_NAMESPACE
unix 2 [ ACC ] STREAM LISTENING 7275 /tmp/.font-unix/fs7100
unix 2 [ ACC ] STREAM LISTENING 7584 /tmp/mysql_cluster.sock
unix 2 [ ACC ] STREAM LISTENING 36185327 /tmp/mongodb-27017.sock
unix 2 [ ACC ] STREAM LISTENING 7408 /var/run/avahi-daemon/socket
unix 2 [ ACC ] STREAM LISTENING 5010 @ISCSID_UIP_ABSTRACT_NAMESPACE
显示每个协议的统计信息
显示所有端口的统计信息 netstat -s
[wm@adx ~]$ netstat -s
Ip:
166117795 total packets received
185973 with invalid addresses
0 forwarded
0 incoming packets discarded
164139417 incoming packets delivered
168224270 requests sent out
62 dropped because of missing route
5266 reassemblies required
2115 packets reassembled ok
5 fragments received ok
16 fragments created
Icmp:
290668 ICMP messages received
357 input ICMP message failed.
399276 connections reset due to unexpected data
1353140 connections reset due to early user close
1496632 connections aborted due to timeout
IpExt:
InMcastPkts: 3805
OutMcastPkts: 39
InBcastPkts: 1027863
OutBcastPkts: 17
显示 TCP 端口的统计信息 netstat -st
[wm@adx ~]$ netstat -st
IcmpMsg:
InType0: 57
InType3: 289139
InType8: 1451
InType11: 12
InType13: 4
InType15: 2
InType17: 3
OutType0: 1451
OutType3: 267912
OutType8: 770
OutType14: 4
Tcp:
4326140 active connections openings
3275617 passive connection openings
1575982 failed connection attempts
1357965 connection resets received
显示 UDP 端口的统计信息 netstat -su
[wm@adx ~]$ netstat -su
IcmpMsg:
InType0: 57
InType3: 289139
InType8: 1451
InType11: 12
InType13: 4
InType15: 2
InType17: 3
OutType0: 1451
OutType3: 267912
OutType8: 770
OutType14: 4
Udp:
10547358 packets received
268472 packets to unknown port received.
636095 packet receive errors
14253582 packets sent
IpExt:
InMcastPkts: 3805
OutMcastPkts: 39
InBcastPkts: 1027876
OutBcastPkts: 17
[wm@adx ~]$
- 在 netstat 输出中显示 PID 和进程名称 netstat -p
netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
[wm@adx ~]$ sudo netstat -p
[sudo] password for wm:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 10.1.172.10:ssh 192.168.8.90:59617 ESTABLISHED 10422/sshd
tcp 0 0 10.1.172.10:ssh 192.168.8.90:51381 ESTABLISHED 8856/sshd
tcp 0 0 10.1.172.10:6379 10.1.172.25:56109 ESTABLISHED 23546/./redis-serve
- 在 netstat 输出中不显示主机,端口和用户名 (host, port or user)
当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。同样可以加速输出,因为不用进行比对查询。
[wm@adx ~]$ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:9312 0.0.0.0:* LISTEN
- 持续输出 netstat 信息
[wm@adx ~]$ netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost.localdomain:smtp localhost.localdomain:45976 ESTABLISHED tcp 0 0 10.1.172.10:ssh 192.168.8.90:59617 ESTABLISHED tcp 0 0 10.1.172.10:ssh 192.168.8.90:51381 ESTABLISHED
- 显示系统不支持的地址族 (Address Families) 在输出的末尾,会有如下的信息
netstat: no support for `AF IPX' on this system. netstat: no support for `AF AX25' on this system. netstat: no support for `AF X25' on this system. netstat: no support for `AF NETROM' on this system.
- 显示核心路由信息 netstat -r
[wm@adx ~]$ netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 10.*.172.0 * 255.255.255.0 U 0 0 0 eth0 169.*.0.0 * 255.255.0.0 U 0 0 0 eth0 default 10.*.172.254 0.0.0.0 UG 0 0 0 eth0
- 找出程序运行的端口
[root@adx ~]# netstat -ap | grep ssh tcp 0 0 *:ssh *:* LISTEN 2460/sshd tcp 0 0 10.1.172.10:ssh 192.168.8.90:59617 ESTABLISHED 10422/sshd tcp 0 0 10.1.172.10:ssh 192.168.8.90:51381 ESTABLISHED 8856/sshd
找出运行在指定端口的进程[root@adx ~]# netstat -ap | grep ":59617" tcp 0 0 10.1.172.10:ssh 192.168.8.90:59617 ESTABLISHED 10422/sshd
- 显示网络接口列表
[root@adx ~]# 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 109950027 0 12 0 84347503 0 0 0 BMRU lo 16436 0 61454319 0 0 0 61454319 0 0 0 LRU
- 显示详细信息,像是 ifconfig 使用 netstat -ie:
[root@adx ~]# netstat -ie Kernel Interface table eth0 Link encap:Ethernet HWaddr 00:22:19:D4:CE:3B inet addr:10.1.172.10 Bcast:10.1.172.255 Mask:255.255.255.0 inet6 addr: fe80::222:19ff:fed4:ce3b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:109950279 errors:0 dropped:12 overruns:0 frame:0 TX packets:84347593 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:16318770580 (15.1 GiB) TX bytes:19733349393 (18.3 GiB) Interrupt:169 Memory:dfdf0000-dfe00000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:61454345 errors:0 dropped:0 overruns:0 frame:0 TX packets:61454345 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:5467632769 (5.0 GiB) TX bytes:5467632769 (5.0 GiB)
IP和TCP分析
- 查看连接某服务端口最多的的IP地址
[root@adx ~]# netstat -nat | grep "192.168.8.90:22" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20
TCP各种状态列表[root@adx ~]# netstat -nat |awk '{print $6}' established) Foreign LISTEN LISTEN LISTEN
- 查看服务器网络连接状态并汇总
[root@localhost workspace]# netstat -an | awk '/^tcp/ { ++S[$NF]} END {for(a in S) print a, S[a]}' LISTEN 12 ESTABLISHED 1
先把状态全都取出来,然后使用uniq -c统计,之后再进行排序。[root@adx ~]# netstat -nat |awk '{print $6}'|sort|uniq -c 1 established) 17 ESTABLISHED 1 Foreign 21 LISTEN 3 SYN_SENT 1 TIME_WAIT
- 再排序
[root@adx ~]# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn 21 LISTEN 17 ESTABLISHED 2 SYN_SENT 1 TIME_WAIT 1 Foreign 1 established)
- 分析access.log获得访问前10位的ip地址
awk '{print $1}' access.log |sort|uniq -c|sort -nr|head -10
参考文献:http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316661.html