Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字。
netstat - 打印网络连接、路由表、连接的数据统计、伪装连接以及广播域成员。
-a (all)显示所有选项,默认不显示LISTEN相关
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服務状态
-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令。
root@linux_ever:~# netstat -at 激活Internet连接 (服务器和已建立连接的) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 yxk:domain *:* LISTEN tcp 0 0 *:ftp *:* LISTEN tcp 0 0 *:ssh *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:telnet *:* LISTEN tcp 0 0 *:echo *:* LISTEN tcp 0 0 localhost:mysql *:* LISTEN tcp 0 0 *:daytime *:* LISTEN tcp 0 0 192.168.56.101:ssh 192.168.56.1:46119 ESTABLISHED tcp6 0 0 [::]:ssh [::]:* LISTEN tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN tcp6 0 0 [::]:3128 [::]:* LISTEN tcp6 0 0 [::]:http [::]:* LISTEN tcp6 1 0 ip6-localhost:53757 ip6-localhost:ipp CLOSE_WAIT tcp6 1 0 ip6-localhost:53820 ip6-localhost:ipp CLOSE_WAIT
root@linux_ever:~# netstat -au 激活Internet连接 (服务器和已建立连接的) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:ipp *:* udp 0 0 *:mdns *:* udp 0 0 *:46678 *:* udp 0 0 *:51196 *:* udp 0 0 yxk:domain *:* udp 0 0 *:bootpc *:* udp 0 0 *:59784 *:* udp6 0 0 [::]:49703 [::]:* udp6 0 0 [::]:56256 [::]:* udp6 0 0 ip6-localhost:56355 ip6-localhost:42658 ESTABLISHED udp6 0 0 [::]:mdns [::]:* udp6 0 0 ip6-localhost:42658 ip6-localhost:56355 ESTABLISHED udp6 0 0 [::]:36927 [::]:*
root@linux_ever:~# netstat -ant 激活Internet连接 (服务器和已建立连接的) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:13 0.0.0.0:* LISTEN tcp 0 0 192.168.56.101:22 192.168.56.1:46119 ESTABLISHED tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:631 :::* LISTEN tcp6 0 0 :::3128 :::* LISTEN tcp6 0 0 :::80 :::* LISTEN tcp6 1 0 ::1:53757 ::1:631 CLOSE_WAIT tcp6 1 0 ::1:53820 ::1:631 CLOSE_WAIT
root@linux_ever:~# netstat -tnl 激活Internet连接 (仅服务器) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:7 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:13 0.0.0.0:* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:631 :::* LISTEN tcp6 0 0 :::3128 :::* LISTEN tcp6 0 0 :::80 :::* LISTEN
root@linux_ever:~# netstat -unl 激活Internet连接 (仅服务器) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:631 0.0.0.0:* udp 0 0 0.0.0.0:5353 0.0.0.0:* udp 0 0 0.0.0.0:46678 0.0.0.0:* udp 0 0 0.0.0.0:51196 0.0.0.0:* udp 0 0 127.0.1.1:53 0.0.0.0:* udp 0 0 0.0.0.0:68 0.0.0.0:* udp 0 0 0.0.0.0:59784 0.0.0.0:* udp6 0 0 :::49703 :::* udp6 0 0 :::56256 :::* udp6 0 0 :::5353 :::* udp6 0 0 :::36927 :::*
root@linux_ever:~# netstat -tnlp 激活Internet连接 (仅服务器) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1112/dnsmasq tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 826/vsftpd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 813/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 3517/cupsd tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN 815/xinetd tcp 0 0 0.0.0.0:7 0.0.0.0:* LISTEN 815/xinetd tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 933/mysqld tcp 0 0 0.0.0.0:13 0.0.0.0:* LISTEN 815/xinetd tcp6 0 0 :::22 :::* LISTEN 813/sshd tcp6 0 0 ::1:631 :::* LISTEN 3517/cupsd tcp6 0 0 :::3128 :::* LISTEN 945/squid3 tcp6 0 0 :::80 :::* LISTEN 1158/apache2使用 -p 选项时,netstat 必须运行在 root 权限之下,不然它就不能得到运行在 root 权限下的进程名,而很多服务包括 http 和 ftp 都运行在 root 权限之下。
root@linux_ever:~# netstat -ltep 激活Internet连接 (仅服务器) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 yxk:domain *:* LISTEN root 10511 1112/dnsmasq tcp 0 0 *:ftp *:* LISTEN root 8890 826/vsftpd tcp 0 0 *:ssh *:* LISTEN root 9398 813/sshd tcp 0 0 localhost:ipp *:* LISTEN root 19363 3517/cupsd tcp 0 0 *:telnet *:* LISTEN root 9559 815/xinetd tcp 0 0 *:echo *:* LISTEN root 9558 815/xinetd tcp 0 0 localhost:mysql *:* LISTEN mysql 10645 933/mysqld tcp 0 0 *:daytime *:* LISTEN root 9557 815/xinetd tcp6 0 0 [::]:ssh [::]:* LISTEN root 9400 813/sshd tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN root 19362 3517/cupsd tcp6 0 0 [::]:3128 [::]:* LISTEN root 200066 945/squid3 tcp6 0 0 [::]:http [::]:* LISTEN root 10582 1158/apache2
root@linux_ever:~# netstat -s Ip: 共计收到421162个数据包 0 已转发 0 incoming packets discarded 419945 incoming packets delivered 245868请求已发出 436 发出的报被抛弃 127 被抛弃,因为没有路由 Icmp: 6202 ICMP messages received 18 input ICMP message failed. InCsumErrors: 18 ICMP接收历史 目的地址不可达:6117 echo requests: 36 echo replies: 31 6296 ICMP messages sent 0 ICMP messages failed ICMP发出历史 目的地址不可达:6229 echo request: 31 echo replies: 36
root@linux_ever:~# netstat -su IcmpMsg: InType0: 31 InType3: 6117 InType8: 36 OutType0: 36 OutType3: 6229 OutType8: 31 Udp: 22928 packets received 6226 packets to unknown port received. 0 packet receive errors 24063 packets sent UdpLite: IpExt: InNoRoutes: 107 InMcastPkts: 17763 OutMcastPkts: 1007 InBcastPkts: 181400 InOctets: 87091549 OutOctets: 93256156 InMcastOctets: 1948386 OutMcastOctets: 129620 InBcastOctets: 15828501 InNoECTPkts: 443329
root@linux_ever:~# netstat -rn 内核 IP 路由表 Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
root@linux_ever:~# 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 434501 0 0 0 222915 0 0 0 BMRU lo 65536 0 36530 0 0 0 36530 0 0 0 LRU上面输出的信息比较原始。我们将 -e 选项和 -i 选项搭配使用,可以输出用户友好的信息。并且和ifconfig命令输出的结果是一样的。
root@linux_ever:~# netstat -ie Kernel Interface table eth0 Link encap:以太网 硬件地址 08:00:27:23:9c:b4 inet 地址:192.168.56.101 广播:192.168.56.255 掩码:255.255.255.0 inet6 地址: fe80::a00:27ff:fe23:9cb4/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:434595 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:222934 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:1000 接收字节:93754320 (93.7 MB) 发送字节:95995756 (95.9 MB) lo Link encap:本地环回 inet 地址:127.0.0.1 掩码:255.0.0.0 inet6 地址: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 跃点数:1 接收数据包:36536 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:36536 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:0 接收字节:2911517 (2.9 MB) 发送字节:2911517 (2.9 MB)
root@linux_ever:~# netstat -ct 激活Internet连接 (w/o 服务器) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.56.101:ssh 192.168.56.1:46119 ESTABLISHED tcp6 1 0 ip6-localhost:53757 ip6-localhost:ipp CLOSE_WAIT tcp6 1 0 ip6-localhost:53820 ip6-localhost:ipp CLOSE_WAIT 激活Internet连接 (w/o 服务器) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.56.101:ssh 192.168.56.1:46119 ESTABLISHED tcp6 1 0 ip6-localhost:53757 ip6-localhost:ipp CLOSE_WAIT tcp6 1 0 ip6-localhost:53820 ip6-localhost:ipp CLOSE_WAIT
root@linux_ever:~# netstat -g IPv6/IPv4 Group Memberships Interface RefCnt Group --------------- ------ --------------------- lo 1 224.0.0.1 eth0 1 224.0.0.251 eth0 1 224.0.0.1 lo 1 ip6-allnodes getnameinfo failed lo 1 [未知] getnameinfo failed eth0 1 [未知] getnameinfo failed eth0 1 [未知] eth0 1 ip6-allnodes getnameinfo failed eth0 1 [未知]
root@linux_ever:~# netstat -atnp | grep ESTA tcp 0 52 192.168.56.101:22 192.168.56.1:46119 ESTABLISHED 10964/1
root@linux_ever:~# netstat -aple | grep http tcp6 0 0 [::]:http [::]:* LISTEN root 10582 1158/apache2从这里可以看到 http 服务正在运行。