首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

Linux netstat命令详解 -转

2014-01-25 
Linux netstat命令详解--转Active Internet connections (w/o servers)Proto Recv-Q Send-Q Local Address

Linux netstat命令详解 --转

Active Internet connections (w/o servers)Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 2 210.34.6.89:telnet 210.34.6.96:2873 ESTABLISHEDtcp 296 0 210.34.6.89:1165 210.34.6.84:netbios-ssn ESTABLISHEDtcp 0 0 localhost.localdom:9001 localhost.localdom:1162 ESTABLISHEDtcp 0 0 localhost.localdom:1162 localhost.localdom:9001 ESTABLISHEDtcp 0 80 210.34.6.89:1161 210.34.6.10:netbios-ssn CLOSEActive UNIX domain sockets (w/o servers)Proto RefCnt Flags Type State I-Node Pathunix 1 [ ] STREAM CONNECTED 16178 @000000ddunix 1 [ ] STREAM CONNECTED 16176 @000000dcunix 9 [ ] DGRAM 5292 /dev/logunix 1 [ ] STREAM CONNECTED 16182 @000000df


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

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

另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。

常见参数

-a (all)显示所有选项,默认不显示LISTEN相关
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服務状态

-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令。

提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到

?

实用命令实例

?

1. 列出所有端口 (包括监听和未监听的)

? 列出所有端口 netstat -a

# netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address           Foreign Address         State tcp        0      0 localhost:30037         *:*                     LISTEN udp        0      0 *:bootpc                *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags       Type       State         I-Node   Path unix  2      [ ACC ]     STREAM     LISTENING     6135     /tmp/.X11-unix/X0 unix  2      [ ACC ]     STREAM     LISTENING     5140     /var/run/acpid.socket

? 列出所有 tcp 端口 netstat -at

# netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address           Foreign Address         State tcp        0      0 localhost:30037         *:*                     LISTEN tcp        0      0 localhost:ipp           *:*                     LISTEN tcp        0      0 *:smtp                  *:*                     LISTEN tcp6       0      0 localhost:ipp           [::]:*                  LISTEN

? 列出所有 udp 端口 netstat -au

# netstat -lx Active UNIX domain sockets (only servers) Proto RefCnt Flags       Type       State         I-Node   Path unix  2      [ ACC ]     STREAM     LISTENING     6294     private/maildrop unix  2      [ ACC ]     STREAM     LISTENING     6203     public/cleanup unix  2      [ ACC ]     STREAM     LISTENING     6302     private/ifmail unix  2      [ ACC ]     STREAM     LISTENING     6306     private/bsmtp
# netstat -s Ip: 11150 total packets received 1 with invalid addresses 0 forwarded 0 incoming packets discarded 11149 incoming packets delivered 11635 requests sent out Icmp: 0 ICMP messages received 0 input ICMP message failed. Tcp: 582 active connections openings 2 failed connection attempts 25 connection resets received Udp: 1183 packets received 4 packets to unknown port received. .....

? 显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su

# netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address           Foreign Address         State tcp        0      0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED tcp        1      1 ramesh-laptop.loc:52564 101.11.169.230:www      CLOSING tcp        0      0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED tcp        1      1 ramesh-laptop.loc:42367 101.101.34.101:www      CLOSING ^C

?

7. 显示系统不支持的地址族 (Address Families)
# netstat -ie Kernel Interface table eth0      Link encap:Ethernet  HWaddr 00:10:40:11:11:11 UP BROADCAST MULTICAST  MTU:1500  Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B) Memory:f6ae0000-f6b00000
wss8848@ubuntu:~$ netstat -nat | grep "192.168.1.15:22" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -2018 221.136.168.363 154.74.45.2422 78.173.31.2362 62.183.207.982 192.168.1.142 182.48.111.2152 124.193.219.342 119.145.41.22 114.255.41.301 75.102.11.99

? TCP各种状态列表

wss8848@ubuntu:~$ netstat -nat |awk '{print $6}'established)ForeignLISTENTIME_WAITESTABLISHEDTIME_WAITSYN_SENT
wss8848@ubuntu:~$ netstat -nat |awk '{print $6}'|sort|uniq -c143 ESTABLISHED1 FIN_WAIT11 Foreign1 LAST_ACK36 LISTEN6 SYN_SENT113 TIME_WAIT1 established)
awk '{print $1}' access.log |sort|uniq -c|sort -nr|head -10

热点排行