简介
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
输出信息含义
执行netstat后,其输出结果为
[vagrant@centos6 ~]$ netstat|more Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 10.0.2.15:ssh 10.0.2.2:52091 ESTABLISHED Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 11 [ ] DGRAM 11021 /dev/log unix 2 [ ] DGRAM 11556 @/org/freedesktop/hal/udev_event unix 2 [ ] DGRAM 8986 @/org/kernel/udev/udevd unix 3 [ ] STREAM CONNECTED 17777 unix 3 [ ] STREAM CONNECTED 17776
从整体上看,netstat的输出结果可以分为两个部分:
OUTPUT Active Internet connections (TCP, UDP, raw) Proto The protocol (tcp, udp, raw) used by the socket. Recv-Q The count of bytes not copied by the user program connected to this socket. Send-Q The count of bytes not acknowledged by the remote host.
一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指的是接收队列和发送队列,这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积,这种情况非常少见。
Active UNIX domain Sockets Proto The protocol (usually unix) used by the socket. RefCnt The reference count (i.e. attached processes via this socket). Flags The flags displayed is SO_ACCEPTON (displayed as ACC), SO_WAITDATA (W) or SO_NOSPACE (N). SO_ACCECPTON is used on unconnected sockets if their corresponding processes are waiting for a connect request. The other flags are not of nor- mal interest. Type There are several types of socket access: SOCK_DGRAM The socket is used in Datagram (connectionless) mode. SOCK_STREAM This is a stream (connection) socket. SOCK_RAW The socket is used as a raw socket.
另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议, RefCnt表示连接到本套接口上的进程数量, Types显示套接口的类型, State显示套接口当前的状态, Path表示连接到套接口的其它进程使用的路径名。
常见参数
-a (all)显示所有选项,netstat默认不显示LISTEN相关 -t (tcp)仅显示tcp相关选项 -u (udp)仅显示udp相关选项 -n 拒绝显示别名,能显示数字的全部转化成数字。(重要) -l 仅列出有在 Listen (监听) 的服務状态 -p 显示建立相关链接的程序名(macOS中表示协议 -p protocol) -r 显示路由信息,路由表 -e 显示扩展信息,例如uid等 -s 按各个协议进行统计 (重要) -c 每隔一个固定时间,执行该netstat命令。
提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到
实用命令实例
1. 列出所有端口 (包括监听和未监听的)
列出所有端口: netstat -a 列出所有tcp端口: netstat -at 列出所有udp端口: netstat -au
2. 列出所有处于监听状态的 Sockets
只显示监听端口: netstat -l 只列出所有监听tcp端口: netstat -lt 只列出所有监听udp端口: netstat -lu 只列出所有监听UNIX端口: netstat -lx
3. 显示每个协议的统计信息
显示所有端口的统计信息 netstat -s
[vagrant@centos6 ~]$ netstat -s Ip: 2081 total packets received 2 with invalid addresses 0 forwarded 0 incoming packets discarded 2079 incoming packets delivered 1319 requests sent out Icmp: 0 ICMP messages received 0 input ICMP message failed. ICMP input histogram: 0 ICMP messages sent 0 ICMP messages failed ICMP output histogram: Tcp: 0 active connections openings 3 passive connection openings 0 failed connection attempts 0 connection resets received 1 connections established 1957 segments received 1103 segments send out 0 segments retransmited 0 bad segments received. 1 resets sent Udp: 122 packets received 0 packets to unknown port received. 0 packet receive errors 216 packets sent UdpLite: TcpExt: 9 delayed acks sent 1 packets directly queued to recvmsg prequeue. 730 packets header predicted 5 acknowledgments not containing data received 1036 predicted acknowledgments 0 TCP data loss events IpExt: InOctets: 146500 OutOctets: 132340
显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
# netstat -st # netstat -su
4. 显示 PID 和进程名称
netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
[root@centos6 ~]# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 10.0.2.15:ssh 10.0.2.2:52964 ESTABLISHED 3008/sshd tcp 0 0 10.0.2.15:ssh 10.0.2.2:52091 ESTABLISHED 2813/sshd
5. 不显示主机,端口和用户名 (host, port or user)
当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。该参数可以加速output的输出,因为不用进行比对查询。
# netstat -an
# 如果只是不想让这三个名称中的一个被显示,使用以下命令 # netsat -a --numeric-ports # netsat -a --numeric-hosts # netsat -a --numeric-users
6. 持续输出 netstat 信息
netstat 将每隔一秒输出网络信息。
# netstat -t -c 2
7. 显示系统不支持的地址族 (Address Families)
netstat --verbose
8. 显示核心路由信息
[root@centos6 ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 eth0 # 注意: 使用 netstat -rn 显示数字格式,不查询主机名称,效果等价于route -n
9. 找出程序运行的端口
并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
[root@centos6 ~]# netstat -apn | grep ssh tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1438/sshd tcp 0 0 10.0.2.15:22 10.0.2.2:52964 ESTABLISHED 3008/sshd tcp 0 0 10.0.2.15:22 10.0.2.2:52091 ESTABLISHED 2813/sshd tcp 0 0 :::22 :::* LISTEN 1438/sshd unix 3 [ ] STREAM CONNECTED 18443 3008/sshd unix 3 [ ] STREAM CONNECTED 18442 3011/sshd unix 2 [ ] DGRAM 18439 3008/sshd unix 3 [ ] STREAM CONNECTED 17777 2813/sshd unix 3 [ ] STREAM CONNECTED 17776 2816/sshd unix 2 [ ] DGRAM 17773 2813/sshd
找出运行在指定端口的进程
[root@centos6 ~]# netstat -an | grep ':22' tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 10.0.2.15:22 10.0.2.2:52964 ESTABLISHED tcp 0 0 10.0.2.15:22 10.0.2.2:52091 ESTABLISHED tcp 0 0 :::22 :::* LISTEN
10. 显示网络接口列表
[root@centos6 ~]# 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 12125 0 0 0 5474 0 0 0 BMRU lo 65536 0 0 0 0 0 0 0 0 0 LRU [root@centos6 ~]# netstat -ie Kernel Interface table eth0 Link encap:Ethernet HWaddr 52:54:00:5D:A4:AF inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fe5d:a4af/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:12131 errors:0 dropped:0 overruns:0 frame:0 TX packets:5477 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7474852 (7.1 MiB) TX bytes:580343 (566.7 KiB) 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:65536 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:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)