Linux初学者的netstat命令教程(8个示例)
如果您对Linux命令行不熟悉,并且对学习网络内容感兴趣,那么您应该了解许多命令行实用程序。 一个这样的工具是netstat 。 在本文中,我们将使用一些易于理解的示例来讨论此命令。
但在此之前,值得一提的是,所有这些示例都已在Ubuntu 16.04 LTS机器上进行了测试。
Linux netstat命令
netstat命令允许您打印网络连接,路由表,接口统计信息,伪装连接和多播成员身份。 以下是它的语法:
netstat [OPTIONS]
以下是手册页的解释:
Netstat prints information about the Linux networking subsystem. The
type of information printed is controlled by the first argument, as
follows:
(none)
By default, netstat displays a list of open sockets. If you don't
specify any address families, then the active sockets of all configured
address families will be printed.
--route , -r
Display the kernel routing tables. See the description in route(8) for
details. netstat -r and route -e produce the same output.
--groups , -g
Display multicast group membership information for IPv4 and IPv6.
--interfaces, -i
Display a table of all network interfaces.
--masquerade , -M
Display a list of masqueraded connections.
--statistics , -s
Display summary statistics for each protocol.
以下是一些Q&A样式的示例,应该给你一个关于netstat命令如何工作的好主意。
Q1。 如何使用netstat命令?
基本用法相当简单 - 只需运行netstat命令而不用任何选项。
netstat
例如,就我而言,产生了以下输出:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.1.7:59510 mails11.telegram.:https ESTABLISHED
tcp 0 0 192.168.1.7:57318 13.70.5.200:https ESTABLISHED
tcp 0 1 192.168.1.7:51238 52.114.32.7:https SYN_SENT
tcp 390 0 192.168.1.7:60392 del03s09-in-f10.1:https ESTABLISHED
tcp 0 1 192.168.1.7:51240 52.114.32.7:https SYN_SENT
tcp 0 0 192.168.1.7:50196 del03s10-in-f10.1:https TIME_WAIT
tcp 0 0 192.168.1.7:41996 maa05s05-in-f4.1e:https TIME_WAIT
tcp 0 0 192.168.1.7:56680 wa-in-f94.1e100.n:https ESTABLISHED
tcp 0 0 192.168.1.7:54578 del03s10-in-f14.1:https TIME_WAIT
tcp 0 0 192.168.1.7:51310 del03s10-in-f2.1e:https TIME_WAIT
因此,默认情况下,该命令将显示所有打开的套接字的列表。
Q2。 如何让netstat显示数字地址?
默认情况下,netstat命令尝试确定符号主机,端口或用户名。 这可能会减慢工具速度。 但是,如果数值不错,可以使用-n命令行选项使工具输出数值。
netstat -n
例如在我的情况下,产生了以下输出:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.1.7:59510 149.154.171.22:443 ESTABLISHED
tcp 0 0 192.168.1.7:57318 13.70.5.200:443 ESTABLISHED
tcp 0 0 192.168.1.7:51682 52.114.32.7:443 TIME_WAIT
tcp 0 0 192.168.1.7:54884 172.217.161.14:443 TIME_WAIT
tcp 0 0 192.168.1.7:35112 172.217.161.5:443 ESTABLISHED
tcp 0 0 192.168.2.97:45662 148.251.237.53:21 ESTABLISHED
tcp 0 0 192.168.1.7:50892 149.154.164.224:443 ESTABLISHED
tcp 0 0 192.168.1.7:58772 172.217.194.189:443 TIME_WAIT
tcp 0 1 192.168.1.7:47728 144.76.158.93:443 SYN_SENT
tcp 0 1 192.168.1.7:47730 144.76.158.93:443 SYN_SENT
tcp 0 0 192.168.1.7:59548 149.154.171.22:443 ESTABLISHED
tcp 0 0 192.168.1.7:35818 169.55.74.50:443 ESTABLISHED
tcp 0 0 192.168.1.7:50888 149.154.164.224:443 ESTABLISHED
tcp 0 0 192.168.1.7:44156 152.195.11.6:443 ESTABLISHED
tcp 0 0 192.168.1.7:58478 52.229.174.94:443 ESTABLISHED
tcp 52 0 127.0.0.1:60342 127.0.1.1:139 ESTABLISHED
tcp 0 0 192.168.1.7:58820 172.217.161.3:443 ESTABLISHED
tcp 0 0 192.168.1.7:45588 52.175.17.224:443 TIME_WAIT
tcp 0 0 192.168.1.7:50890 149.154.164.224:443 ESTABLISHED
tcp 0 0 192.168.1.7:58192 52.77.195.144:443 ESTABLISHED
tcp 0 0 192.168.1.7:54224 172.217.31.2:443 ESTABLISHED
tcp 0 0 127.0.1.1:139 127.0.0.1:60342 ESTABLISHED
tcp 0 0 192.168.1.7:34500 192.241.190.153:443 ESTABLISHED
tcp 0 0 192.168.1.7:54888 172.217.161.14:443 ESTABLISHED
所以你可以看到这次只有输出中产生的数字值(在Foreign Address列中)。
Q3。 如何让netstat只显示tcp连接?
这可以使用-t命令行选项来完成。
netstat -t
例如,就我而言,产生了以下输出:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.1.7:44236 152.195.11.6:https ESTABLISHED
tcp 0 0 192.168.1.7:59510 mails11.telegram.:https ESTABLISHED
tcp 0 0 192.168.1.7:57318 13.70.5.200:https ESTABLISHED
tcp 0 0 192.168.1.7:42706 maa05s05-in-f4.1e:https ESTABLISHED
tcp 0 0 192.168.1.7:51984 del03s10-in-f2.1e:https ESTABLISHED
tcp 0 0 192.168.1.7:51786 52.114.32.7:https ESTABLISHED
tcp 0 0 192.168.1.7:52294 xx-fbcdn-shv-01-m:https ESTABLISHED
tcp 0 0 192.168.1.7:54372 del03s01-in-f2.1e:https ESTABLISHED
tcp 0 1 192.168.1.7:47788 hyperserver.semse:https SYN_SENT
tcp 0 0 192.168.1.7:52298 xx-fbcdn-shv-01-m:https ESTABLISHED
tcp 0 0 192.168.1.7:51840 149.154.164.224:https TIME_WAIT
tcp 0 0 192.168.1.7:35258 del03s10-in-f1.1e:https ESTABLISHED
tcp 0 0 192.168.2.97:45662 blog.gsmarena.com:ftp ESTABLISHED
tcp 0 0 192.168.1.7:50728 del03s10-in-f10.1:https TIME_WAIT
tcp 0 0 192.168.1.7:56468 del03s09-in-f3.1e:https ESTABLISHED
tcp 0 0 192.168.1.7:50892 149.154.164.224:https ESTABLISHED
tcp 0 0 192.168.1.7:45670 52.175.17.224:https ESTABLISHED
tcp 0 0 192.168.1.7:55180 del03s10-in-f14.1:https TIME_WAIT
tcp 0 1 192.168.1.7:47790 hyperserver.semse:https SYN_SENT
tcp 0 0 192.168.1.7:59548 mails11.telegram.:https ESTABLISHED
tcp 0 0 192.168.1.7:35818 32.4a.37a9.ip4.st:https ESTABLISHED
tcp 0 0 192.168.1.7:50888 149.154.164.224:https ESTABLISHED
tcp 0 0 192.168.1.7:33360 del03s10-in-f13.1:https TIME_WAIT
tcp 0 0 192.168.1.7:52332 207.46.140.70:https ESTABLISHED
tcp 0 0 192.168.1.7:58478 52.229.174.94:https ESTABLISHED
tcp 0 0 192.168.1.7:54374 del03s01-in-f2.1e:https TIME_WAIT
tcp 0 0 192.168.1.7:50216 104.24.1.68:https TIME_WAIT
tcp 52 0 localhost:60342 ansh:netbios-ssn ESTABLISHED
tcp 0 0 192.168.1.7:50890 149.154.164.224:https ESTABLISHED
tcp 0 0 192.168.1.7:55200 del03s10-in-f14.1:https TIME_WAIT
tcp 0 0 192.168.1.7:54224 del03s01-in-f2.1e:https ESTABLISHED
tcp 0 0 ansh:netbios-ssn localhost:60342 ESTABLISHED
tcp 0 0 192.168.1.7:55212 del03s10-in-f14.1:https TIME_WAIT
tcp 0 1 192.168.1.7:47800 hyperserver.semse:https SYN_SENT
tcp 0 0 192.168.1.7:45668 52.175.17.224:https ESTABLISHED
tcp 0 0 192.168.1.7:58878 del03s10-in-f3.1e:https TIME_WAIT
tcp 0 0 192.168.1.7:34500 do-11.lastpass.co:https ESTABLISHED
tcp 0 0 192.168.1.7:41844 a23-35-40-198.dep:https ESTABLISHED
Q4。 如何让netstat只显示udp连接
这可以使用-u命令行选项。
netstat -u
Q5。 如何让netstat只显示监听套接字?
这可以通过使用-l命令行选项来实现。
netstat -l
例如,在我的情况下产生了以下输出:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ansh:domain *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 *:microsoft-ds *:* LISTEN
tcp 0 0 *:netbios-ssn *:* LISTEN
tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN
tcp6 0 0 [::]:microsoft-ds [::]:* LISTEN
tcp6 0 0 [::]:netbios-ssn [::]:* LISTEN
...
...
...
Q6。 如何使程序的netstat显示名称和PID?
如果您希望netstat显示每个套接字所属进程的进程ID的名称,请使用-p命令行选项。
netstat -p
例如:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.1.7:57318 13.70.5.200:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:36732 13.76.170.95:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:52050 149.154.164.224:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:55974 del03s10-in-f14.1:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:52074 149.154.164.224:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:60590 mails11.telegram.:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:52254 149.154.164.224:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.2.97:45662 blog.gsmarena.com:ftp ESTABLISHED 3160/filezilla
tcp 0 0 192.168.1.7:35818 32.4a.37a9.ip4.st:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:52528 52.114.32.7:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:55950 del03s10-in-f14.1:https ESTABLISHED 3247/firefox
tcp 68 0 localhost:60342 ansh:netbios-ssn ESTABLISHED 2625/gvfsd-smb-brow
tcp 0 0 192.168.1.7:44974 152.195.11.6:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:36730 13.76.170.95:https ESTABLISHED 3247/firefox
tcp 0 0 ansh:netbios-ssn localhost:60342 ESTABLISHED -
tcp 0 0 192.168.1.7:52732 del03s10-in-f2.1e:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:60314 52.229.174.94:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:53072 207.46.140.70:https ESTABLISHED 3247/firefox
tcp 0 0 192.168.1.7:34500 do-11.lastpass.co:https ESTABLISHED 3247/firefox
查看PID /程序名称信息的最后一列。
Q7。 如何使netstat打印网络统计信息?
这可以使用-s命令行选项完成。
netstat -s
在这种情况下,该工具会生成诸如接收,丢弃,交付以及更多信息的信息。 以下是使用-s命令行选项时的示例输出。
Ip:
592215 total packets received
41 with invalid addresses
0 forwarded
0 incoming packets discarded
580699 incoming packets delivered
539118 requests sent out
627 outgoing packets dropped
482 dropped because of missing route
2 fragments failed
Icmp:
2337 ICMP messages received
6 input ICMP message failed.
ICMP input histogram:
destination unreachable: 2337
3917 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 3917
IcmpMsg:
InType3: 2337
OutType3: 3917
Tcp:
10943 active connections openings
127 passive connection openings
38 failed connection attempts
467 connection resets received
34 connections established
443643 segments received
414589 segments send out
6929 segments retransmited
428 bad segments received.
11713 resets sent
...
...
...
Q8。 如何使netstat显示内核路由表?
这可以使用-r命令行选项完成。
netstat -r
以下是我的案例中输出的结果:
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default 192.168.1.1 0.0.0.0 UG 0 0 0 wlx18a6f713679b
link-local * 255.255.0.0 U 0 0 0 wlx18a6f713679b
192.168.1.0 * 255.255.255.0 U 0 0 0 wlx18a6f713679b
请注意, netstat -r和route -e会产生相同的输出。
结论
正如你现在所理解的那样,netstat命令是一个非常重要的工具。 它提供了很多功能 - 我们刚刚在这里描述了这个表面。 一旦完成了本教程中所讨论的内容,请访问该工具的手册页获取更多信息。