本文转载自(http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316661.html)
简介
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
输出信息含义
执行netstat后,其输出结果为
[root@test1 ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 52 10.2.3.110:ssh 10.2.3.31:63608 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 7151 @/org/kernel/udev/udevd
unix 2 [ ] DGRAM 10759 @/org/freedesktop/hal/udev_event
unix 22 [ ] DGRAM 9895 /dev/log
unix 2 [ ] DGRAM 65590
unix 2 [ ] DGRAM 61488
unix 3 [ ] STREAM CONNECTED 24214 @/tmp/gdm-session-IThxCJbO
unix 3 [ ] STREAM CONNECTED 24213
unix 3 [ ] STREAM CONNECTED 24210 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 24209
unix 3 [ ] STREAM CONNECTED 24189 @/tmp/dbus-3GSkb2QbPQ
unix 3 [ ] STREAM CONNECTED 24188
unix 2 [ ] DGRAM 24187
unix 3 [ ] STREAM CONNECTED 24147 /var/run/dbus/system_bus_socket
从整体上看,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
[root@test1 ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:57579 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
tcp 0 0 10.2.3.110:ssh 10.2.3.31:63608 ESTABLISHED
tcp 0 0 *:53961 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
udp 0 0 *:858 *:*
udp 0 0 *:49245 *:*
udp 0 0 *:mdns *:*
udp 0 0 *:sunrpc *:*
udp 0 0 *:ipp *:*
udp 0 0 10.2.3.110:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp 0 0 *:41611 *:*
udp 0 0 *:782 *:*
udp 0 0 *:32866 *:*
udp 0 0 *:sunrpc *:*
udp 0 0 fe80::20c:29ff:fe51:8bd6:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp 0 0 *:782 *:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 11622 /var/run/abrt/abrt.socket
unix 2 [ ACC ] STREAM LISTENING 6982 @/com/ubuntu/upstart
unix 2 [ ACC ] STREAM LISTENING 10732 @/var/run/hald/dbus-XPXahQaTzc
unix 2 [ ACC ] STREAM LISTENING 23274 @/tmp/.X11-unix/X0
unix 2 [ ] DGRAM 7151 @/org/kernel/udev/udevd
unix 2 [ ACC ] STREAM LISTENING 10737 @/var/run/hald/dbus-YeQAH6W2CK
unix 2 [ ACC ] STREAM LISTENING 9554 /var/run/vmware/guestServicePipe
unix 2 [ ACC ] STREAM LISTENING 23446 @/tmp/gdm-session-IThxCJbO
unix 2 [ ] DGRAM 10759 @/org/freedesktop/hal/udev_event
unix 2 [ ACC ] STREAM LISTENING 10673 /var/run/cups/cups.sock
unix 2 [ ACC ] STREAM LISTENING 23467 @/tmp/.ICE-unix/3213
列出所有 tcp 端口 netstat -at
[root@test1 ~]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:57579 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
tcp 0 52 10.2.3.110:ssh 10.2.3.31:63608 ESTABLISHED
tcp 0 0 *:53961 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
2. 列出所有处于监听状态的 Sockets
只显示监听端口 netstat -l
[root@test1 ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:57579 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
tcp 0 0 *:53961 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
udp 0 0 *:858 *:*
udp 0 0 *:49245 *:*
udp 0 0 *:mdns *:*
udp 0 0 *:sunrpc *:*
udp 0 0 *:ipp *:*
udp 0 0 10.2.3.110:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp 0 0 *:41611 *:*
udp 0 0 *:782 *:*
udp 0 0 *:32866 *:*
udp 0 0 *:sunrpc *:*
udp 0 0 fe80::20c:29ff:fe51:8bd6:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp 0 0 *:782 *:*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 11622 /var/run/abrt/abrt.socket
unix 2 [ ACC ] STREAM LISTENING 6982 @/com/ubuntu/upstart
unix 2 [ ACC ] STREAM LISTENING 10732 @/var/run/hald/dbus-XPXahQaTzc
unix 2 [ ACC ] STREAM LISTENING 23274 @/tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 10737 @/var/run/hald/dbus-YeQAH6W2CK
unix 2 [ ACC ] STREAM LISTENING 9554 /var/run/vmware/guestServicePipe
unix 2 [ ACC ] STREAM LISTENING 23446 @/tmp/gdm-session-IThxCJbO
unix 2 [ ACC ] STREAM LISTENING 10673 /var/run/cups/cups.sock
unix 2 [ ACC ] STREAM LISTENING 23467 @/tmp/.ICE-unix/3213
只列出所有监听 tcp 端口 netstat -lt
[root@test1 ~]# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:57579 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
tcp 0 0 *:53961 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
只列出所有监听 udp 端口 netstat -lu
[root@test1 ~]# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:858 *:*
udp 0 0 *:49245 *:*
udp 0 0 *:mdns *:*
udp 0 0 *:sunrpc *:*
udp 0 0 *:ipp *:*
udp 0 0 10.2.3.110:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp 0 0 *:41611 *:*
udp 0 0 *:782 *:*
udp 0 0 *:32866 *:*
udp 0 0 *:sunrpc *:*
udp 0 0 fe80::20c:29ff:fe51:8bd6:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp 0 0 *:782 *:*
只列出所有监听 UNIX 端口 netstat -lx
[root@test1 ~]# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 11622 /var/run/abrt/abrt.socket
unix 2 [ ACC ] STREAM LISTENING 6982 @/com/ubuntu/upstart
unix 2 [ ACC ] STREAM LISTENING 10732 @/var/run/hald/dbus-XPXahQaTzc
unix 2 [ ACC ] STREAM LISTENING 23274 @/tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 10737 @/var/run/hald/dbus-YeQAH6W2CK
unix 2 [ ACC ] STREAM LISTENING 9554 /var/run/vmware/guestServicePipe
unix 2 [ ACC ] STREAM LISTENING 23446 @/tmp/gdm-session-IThxCJbO
unix 2 [ ACC ] STREAM LISTENING 10673 /var/run/cups/cups.sock
unix 2 [ ACC ] STREAM LISTENING 23467 @/tmp/.ICE-unix/3213
unix 2 [ ACC ] STREAM LISTENING 23424 @/tmp/dbus-3GSkb2QbPQ
unix 2 [ ACC ] STREAM LISTENING 10049 /var/run/rpcbind.sock
unix 2 [ ACC ] STREAM LISTENING 10123 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 23275 /tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 23468 /tmp/.ICE-unix/3213
unix 2 [ ACC ] STREAM LISTENING 23489 /tmp/orbit-gdm/linc-c90-0-2584ca1f3ad76
3. 显示每个协议的统计信息
显示所有端口的统计信息 netstat -s
[root@test1 ~]# netstat -s
Ip:
93998 total packets received
158 with invalid addresses
0 forwarded
0 incoming packets discarded
93840 incoming packets delivered
42872 requests sent out
4 dropped because of missing route
Icmp:
4 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:
destination unreachable: 1
echo replies: 3
8 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 1
echo request: 7
IcmpMsg:
InType0: 3
InType3: 1
OutType3: 1
OutType8: 7
Tcp:
33 active connections openings
8 passive connection openings
10 failed connection attempts
2 connection resets received
1 connections established
79482 segments received
42260 segments send out
11 segments retransmited
0 bad segments received.
12 resets sent
Udp:
5935 packets received
1 packets to unknown port received.
0 packet receive errors
597 packets sent
UdpLite:
TcpExt:
19 TCP sockets finished time wait in fast timer
151 delayed acks sent
2 delayed acks further delayed because of locked socket
Quick ack mode was activated 7 times
15 packets directly queued to recvmsg prequeue.
14 packets directly received from prequeue
72222 packets header predicted
2480 acknowledgments not containing data received
119 predicted acknowledgments
TCPDSACKUndo: 6
5 congestion windows recovered after partial ack
0 TCP data loss events
11 other TCP timeouts
7 DSACKs sent for old packets
11 DSACKs received
2 connections aborted due to timeout
TCPDSACKIgnoredNoUndo: 1
TCPSackShiftFallback: 4
IpExt:
InMcastPkts: 5409
OutMcastPkts: 43
InBcastPkts: 8428
InOctets: 111384855
OutOctets: 3083247
InMcastOctets: 841260
OutMcastOctets: 9367
InBcastOctets: 789602
显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
netstat -st
netstat -su
4. 在 netstat 输出中显示 PID 和进程名称 netstat -p
netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
[root@test1 ~]# netstat -p
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 52 10.2.3.110:ssh 10.2.3.31:63608 ESTABLISHED 11494/sshd
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ] DGRAM 7151 465/udevd @/org/kernel/udev/udevd
unix 2 [ ] DGRAM 10759 2034/hald @/org/freedesktop/hal/udev_event
unix 22 [ ] DGRAM 9895 1837/rsyslogd /dev/log
unix 2 [ ] DGRAM 70129 11674/pickup
unix 2 [ ] DGRAM 65590 11494/sshd
unix 3 [ ] STREAM CONNECTED 24214 3189/gdm-simple-sla @/tmp/gdm-session-IThxCJbO
unix 3 [ ] STREAM CONNECTED 24213 3249/pam
unix 3 [ ] STREAM CONNECTED 24210 1894/dbus-daemon /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 24209 3249/pam
unix 3 [ ] STREAM CONNECTED 24189 3212/dbus-daemon @/tmp/dbus-3GSkb2QbPQ
unix 3 [ ] STREAM CONNECTED 24188 3231/metacity
unix 2 [ ] DGRAM 24187 3235/gdm-simple-gre
unix 3 [ ] STREAM CONNECTED 24147 1894/dbus-daemon /var/run/dbus/system_bus_socket
5. 持续输出 netstat 信息
netstat 将每隔一秒输出网络信息。
[root@test1 ~]# netstat -lnpt -c 1
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:57579 0.0.0.0:* LISTEN 1954/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1879/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3561/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2014/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2301/master
tcp 0 0 :::53961 :::* LISTEN 1954/rpc.statd
tcp 0 0 :::111 :::* LISTEN 1879/rpcbind
tcp 0 0 :::22 :::* LISTEN 3561/sshd
tcp 0 0 ::1:631 :::* LISTEN 2014/cupsd
tcp 0 0 ::1:25 :::* LISTEN 2301/master
6. 找出程序运行的端口
并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
[root@test1 ~]# netstat -a | grep ssh
tcp 0 0 *:ssh *:* LISTEN
tcp 0 52 10.2.3.110:ssh 10.2.3.31:63608 ESTABLISHED
tcp 0 0 *:ssh *:* LISTEN
找出运行在指定端口的进程
[root@test1 ~]# netstat -an | grep ":22"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 52 10.2.3.110:22 10.2.3.31:63608 ESTABLISHED
tcp 0 0 :::22 :::* LISTEN
7. 显示网络接口列表
[root@test1 ~]# 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 115516 0 0 0 44279 0 0 0 BMRU
lo 16436 0 52 0 0 0 52 0 0 0 LRU
显示详细信息,像是 ifconfig 使用 netstat -ie:
[root@test1 ~]# netstat -ie
Kernel Interface table
eth0 Link encap:Ethernet HWaddr 00:0C:29:51:8B:D6
inet addr:10.2.3.110 Bcast:10.2.3.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe51:8bd6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:115832 errors:0 dropped:0 overruns:0 frame:0
TX packets:44295 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:114221208 (108.9 MiB) TX bytes:4016156 (3.8 MiB)
Interrupt:19 Base address:0x2024
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:52 errors:0 dropped:0 overruns:0 frame:0
TX packets:52 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3476 (3.3 KiB) TX bytes:3476 (3.3 KiB)