netstat 的10个基本用法

转自:https://linux.cn/article-2434-1.html

netstat 的10个基本用法

Netstat 简介

Netstat 是一款命令行工具,可用于列出系统上全部的网络套接字链接状况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字。若是你想确认系统上的 Web 服务有没有起来,你能够查看80端口有没有打开。以上功能使 netstat 成为网管和系统管理员的必备利器。在这篇教程中,我会列出几个例子,教你们如何使用 netstat 去查找网络链接信息和系统开启的端口号。html

如下的简单介绍来自 netstat 的 man 手册:node

netstat - 打印网络链接、路由表、链接的数据统计、假装链接以及广播域成员。linux

1. 列出全部链接

第一个要介绍的,是最简单的命令:列出全部当前的链接。使用 -a 选项便可。nginx

 

  1. $ netstat -a
  2.  
  3. Active Internet connections (servers and established)
  4. Proto Recv-Q Send-Q Local Address Foreign Address State
  5. tcp 0 0 enlightened:domain *:* LISTEN
  6. tcp 0 0 localhost:ipp *:* LISTEN
  7. tcp 0 0 enlightened.local:54750 li240-5.members.li:http ESTABLISHED
  8. tcp 0 0 enlightened.local:49980 del01s07-in-f14.1:https ESTABLISHED
  9. tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN
  10. udp 0 0 enlightened:domain *:*
  11. udp 0 0 *:bootpc *:*
  12. udp 0 0 enlightened.local:ntp *:*
  13. udp 0 0 localhost:ntp *:*
  14. udp 0 0 *:ntp *:*
  15. udp 0 0 *:58570 *:*
  16. udp 0 0 *:mdns *:*
  17. udp 0 0 *:49459 *:*
  18. udp6 0 0 fe80::216:36ff:fef8:ntp [::]:*
  19. udp6 0 0 ip6-localhost:ntp [::]:*
  20. udp6 0 0 [::]:ntp [::]:*
  21. udp6 0 0 [::]:mdns [::]:*
  22. udp6 0 0 [::]:63811 [::]:*
  23. udp6 0 0 [::]:54952 [::]:*
  24. Active UNIX domain sockets (servers and established)
  25. Proto RefCnt Flags Type State I-Node Path
  26. unix 2 [ ACC ] STREAM LISTENING 12403 @/tmp/dbus-IDgfj3UGXX
  27. unix 2 [ ACC ] STREAM LISTENING 40202 @/dbus-vfs-daemon/socket-6nUC6CCx

上述命令列出 tcp, udp 和 unix 协议下全部套接字的全部链接。然而这些信息还不够详细,管理员每每须要查看某个协议或端口的具体链接状况。chrome

2. 只列出 TCP 或 UDP 协议的链接

使用 -t 选项列出 TCP 协议的链接:apache

  1. $ netstat -at
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 enlightened:domain *:* LISTEN
  5. tcp 0 0 localhost:ipp *:* LISTEN
  6. tcp 0 0 enlightened.local:36310 del01s07-in-f24.1:https ESTABLISHED
  7. tcp 0 0 enlightened.local:45038 a96-17-181-10.depl:http ESTABLISHED
  8. tcp 0 0 enlightened.local:37892 ABTS-North-Static-:http ESTABLISHED
  9. .....

使用 -u 选项列出 UDP 协议的链接:网络

  1. $ netstat -au
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. udp 0 0 *:34660 *:*
  5. udp 0 0 enlightened:domain *:*
  6. udp 0 0 *:bootpc *:*
  7. udp 0 0 enlightened.local:ntp *:*
  8. udp 0 0 localhost:ntp *:*
  9. udp 0 0 *:ntp *:*
  10. udp6 0 0 fe80::216:36ff:fef8:ntp [::]:*
  11. udp6 0 0 ip6-localhost:ntp [::]:*
  12. udp6 0 0 [::]:ntp [::]:*

上面同时显示了 IPv4 和 IPv6 的链接。dom

3. 禁用反向域名解析,加快查询速度

默认状况下 netstat 会经过反向域名解析技术查找每一个 IP 地址对应的主机名。这会下降查找速度。若是你以为 IP 地址已经足够,而没有必要知道主机名,就使用 -n 选项禁用域名解析功能。socket

  1. $ netstat -ant
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN
  5. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
  6. tcp 0 0 192.168.1.2:49058 173.255.230.5:80 ESTABLISHED
  7. tcp 0 0 192.168.1.2:33324 173.194.36.117:443 ESTABLISHED
  8. tcp6 0 0 ::1:631 :::* LISTEN

上述命令列出全部 TCP 协议的链接,没有使用域名解析技术。So easy ? 很是好。tcp

4. 只列出监听中的链接

任何网络服务的后台进程都会打开一个端口,用于监听接入的请求。这些正在监听的套接字也和链接的套接字同样,也能被 netstat 列出来。使用 -l 选项列出正在监听的套接字。

  1. $ netstat -tnl
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN
  5. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
  6. tcp6 0 0 ::1:631 :::* LISTEN

如今咱们能够看处处于监听状态的 TCP 端口和链接。若是你查看全部监听端口,去掉 -t 选项。若是你只想查看 UDP 端口,使用 -u 选项,代替 -t 选项。

注意:不要使用 -a 选项,不然 netstat 会列出全部链接,而不单单是监听端口。

5. 获取进程名、进程号以及用户 ID

查看端口和链接的信息时,能查看到它们对应的进程名和进程号对系统管理员来讲是很是有帮助的。举个栗子,Apache 的 httpd 服务开启80端口,若是你要查看 http 服务是否已经启动,或者 http 服务是由 apache 仍是 nginx 启动的,这时候你能够看看进程名。

使用 -p 选项查看进程信息。

  1. ~$ sudo netstat -nlpt
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1144/dnsmasq
  5. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 661/cupsd
  6. tcp6 0 0 ::1:631 :::* LISTEN 661/cupsd

使用 -p 选项时,netstat 必须运行在 root 权限之下,否则它就不能获得运行在 root 权限下的进程名,而不少服务包括 http 和 ftp 都运行在 root 权限之下。

相比进程名和进程号而言,查看进程的拥有者会更有用。使用 -ep 选项能够同时查看进程名和用户名。

  1. $ sudo netstat -ltpe
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
  4. tcp 0 0 enlightened:domain *:* LISTEN root 11090 1144/dnsmasq
  5. tcp 0 0 localhost:ipp *:* LISTEN root 9755 661/cupsd
  6. tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN root 9754 661/cupsd

上面列出 TCP 协议下的监听套接字,同时显示进程信息和一些额外信息。

这些额外的信息包括用户名和进程的索引节点号。这个命令对网管来讲颇有用。

注意 - 假如你将 -n-e 选项一块儿使用,User 列的属性就是用户的 ID 号,而不是用户名。

6. 打印统计数据

netstat 能够打印出网络统计数据,包括某个协议下的收发包数量。

下面列出全部网络包的统计状况:

  1. $ netstat -s
  2. Ip:
  3. 32797 total packets received
  4. 0 forwarded
  5. 0 incoming packets discarded
  6. 32795 incoming packets delivered
  7. 29115 requests sent out
  8. 60 outgoing packets dropped
  9. Icmp:
  10. 125 ICMP messages received
  11. 0 input ICMP message failed.
  12. ICMP input histogram:
  13. destination unreachable: 125
  14. 125 ICMP messages sent
  15. 0 ICMP messages failed
  16. ICMP output histogram:
  17. destination unreachable: 125
  18. ... OUTPUT TRUNCATED ...

若是想只打印出 TCP 或 UDP 协议的统计数据,只要加上对应的选项(-t-u)便可,so easy。

7. 显示内核路由信息

使用 -r 选项打印内核路由信息。打印出来的信息与 route 命令输出的信息同样。咱们也可使用 -n 选项禁止域名解析。

  1. $ netstat -rn
  2. Kernel IP routing table
  3. Destination Gateway Genmask Flags MSS Window irtt Iface
  4. 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
  5. 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

8. 打印网络接口

netstat 也能打印网络接口信息,-i 选项就是为这个功能而生。

  1. $ netstat -i
  2. Kernel Interface table
  3. Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
  4. eth0 1500 0 31611 0 0 0 27503 0 0 0 BMRU
  5. lo 65536 0 2913 0 0 0 2913 0 0 0 LRU

上面输出的信息比较原始。咱们将 -e 选项和 -i 选项搭配使用,能够输出用户友好的信息。

  1. $ netstat -ie
  2. Kernel Interface table
  3. eth0 Link encap:Ethernet HWaddr 00:16:36:f8:b2:64
  4. inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
  5. inet6 addr: fe80::216:36ff:fef8:b264/64 Scope:Link
  6. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  7. RX packets:31682 errors:0 dropped:0 overruns:0 frame:0
  8. TX packets:27573 errors:0 dropped:0 overruns:0 carrier:0
  9. collisions:0 txqueuelen:1000
  10. RX bytes:29637117 (29.6 MB) TX bytes:4590583 (4.5 MB)
  11. Interrupt:18 Memory:da000000-da020000
  12.  
  13. lo Link encap:Local Loopback
  14. inet addr:127.0.0.1 Mask:255.0.0.0
  15. inet6 addr: ::1/128 Scope:Host
  16. UP LOOPBACK RUNNING MTU:65536 Metric:1
  17. RX packets:2921 errors:0 dropped:0 overruns:0 frame:0
  18. TX packets:2921 errors:0 dropped:0 overruns:0 carrier:0
  19. collisions:0 txqueuelen:0
  20. RX bytes:305297 (305.2 KB) TX bytes:305297 (305.2 KB)

上面的输出信息与 ifconfig 输出的信息同样。

9. netstat 持续输出

咱们可使用 netstat 的 -c 选项持续输出信息。

  1. $ netstat -ct

这个命令可持续输出 TCP 协议信息。

10. 显示多播组信息

选项 -g 会输出 IPv4 和 IPv6 的多播组信息。

  1. $ netstat -g
  2. IPv6/IPv4 Group Memberships
  3. Interface RefCnt Group
  4. --------------- ------ ---------------------
  5. lo 1 all-systems.mcast.net
  6. eth0 1 224.0.0.251
  7. eth0 1 all-systems.mcast.net
  8. lo 1 ip6-allnodes
  9. lo 1 ff01::1
  10. eth0 1 ff02::fb
  11. eth0 1 ff02::1:fff8:b264
  12. eth0 1 ip6-allnodes
  13. eth0 1 ff01::1
  14. wlan0 1 ip6-allnodes
  15. wlan0 1 ff01::1

更多用法

目前为止咱们列出了 netstat 的基本用法,如今让咱们一块儿来 geek 吧~

打印 active 状态的链接

active 状态的套接字链接用 "ESTABLISHED" 字段表示,因此咱们可使用 grep 命令得到 active 状态的链接:

  1. $ netstat -atnp | grep ESTA
  2. (Not all processes could be identified, non-owned process info
  3. will not be shown, you would have to be root to see it all.)
  4. tcp 0 0 192.168.1.2:49156 173.255.230.5:80 ESTABLISHED 1691/chrome
  5. tcp 0 0 192.168.1.2:33324 173.194.36.117:443 ESTABLISHED 1691/chrome

配合 watch 命令监视 active 状态的链接:

  1. $ watch -d -n0 "netstat -atnp | grep ESTA"

查看服务是否在运行

若是你想看看 http,smtp 或 ntp 服务是否在运行,使用 grep。

  1. $ sudo netstat -aple | grep ntp
  2. udp 0 0 enlightened.local:ntp *:* root 17430 1789/ntpd
  3. udp 0 0 localhost:ntp *:* root 17429 1789/ntpd
  4. udp 0 0 *:ntp *:* root 17422 1789/ntpd
  5. udp6 0 0 fe80::216:36ff:fef8:ntp [::]:* root 17432 1789/ntpd
  6. udp6 0 0 ip6-localhost:ntp [::]:* root 17431 1789/ntpd
  7. udp6 0 0 [::]:ntp [::]:* root 17423 1789/ntpd
  8. unix 2 [ ] DGRAM 17418 1789/ntpd

从这里能够看到 ntp 服务正在运行。使用 grep 命令你能够查看 http 或 smtp 或其它任何你想查看的服务。

好了,netstat 的大部分功能都介绍过了,若是你想知道 netstat 更高级的功能,阅读它的手册吧(man netstat)。

欢迎在下面留下你的反馈和建议。

相关文章
相关标签/搜索