netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等
1 常用选项:
-a :(all)显示所有选项,默认不显示LISTEN相关
-t :(tcp)仅显示tcp相关选项
-u :(udp)仅显示udp相关选项
-n :以数字显示IP和端口,能显示数字的全部转化成数字。
-l :仅列出有在 Listen (监听) 的服務状态
-p: 显示建立相关链接的程序名
-r :显示路由信息,路由表
-e :显示扩展信息,例如uid等
-s :按各个协议进行统计
-c :每隔一个固定时间,执行该netstat命令。
-w:raw socket相关
2 -tan 显示所有tcp端口
[root@centos7 ~]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 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 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 52 192.168.29.3:22 192.168.29.1:51029 ESTABLISHED
tcp 0 0 192.168.29.3:22 192.168.29.1:51180 ESTABLISHED
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::*
3 显示所有udp端口 -uan
[root@centos7 ~]# netstat -anu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:47414 0.0.0.0:*
udp 0 0 192.168.122.1:53 0.0.0.0:*
udp 0 0 0.0.0.0:67 0.0.0.0:*
4 显示所有处于监听的tcp端口 -tnl
[root@centos7 ~]# netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 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 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
5 显示所有处于监听状态的udp端口 -unl
[root@centos7 ~]# netstat -unl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:47414 0.0.0.0:*
udp 0 0 0.0.0.0:20815 0.0.0.0:*
udp 0 0 192.168.122.1:53 0.0.0.0:*
udp 0 0 0.0.0.0:67 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:9804 0.0.0.0:*
udp6 0 0 :::12799 :::*
udp6 0 0 :::37953 :::*
6 显示路由表 –rn
[root@centos7 ~]# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.29.1 0.0.0.0 UG 0 0 0 ens33
192.168.29.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@centos7 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.29.1 0.0.0.0 UG 100 0 0 ens33
192.168.29.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
7 显示每个协议的统计信息
(1)显示所有端口的统计信息 netstat -s
[root@centos7 ~]# netstat -s
Ip:
3808 total packets received
0 forwarded
0 incoming packets discarded
2444 incoming packets delivered
2254 requests sent out
32 outgoing packets dropped
Icmp:
676 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:
目標無法到達:148
回顯回覆:528
717 ICMP 訊息已發送
0 ICMP 訊息失敗
ICMP 輸出直方圖:
destination unreachable: 148
echo request: 569
[……]
(2)顯示TCP連接埠的統計資料netstat -st
[root@centos7 ~]# netstat -st
##IcmpMsg:InType0: 528InType3: 148OutType3 : 148OutType8: 569#Tcp:0 個主動連線開啟2 個被動連線開啟 0 個失敗的連線嘗試接收到的0 個連線重設建立了2 個連線接收到1637 個資料段發出1418 個資料段重傳0 個資料段0 收到壞段。的統計資訊netstat -su[root@centos7 ~]# netstat -su
IcmpMsg:InType0:528
InType3:148OutType3:148OutType8:569 Udp:接收到0 個封包接收到未知連接埠的140 個封包。緩衝區錯誤UdpLite:IpExt:#InMcastPkts: 27 OutMcastPkts:33InBcastPkts:766#InOctets:410294OutOctets:432666InMcastOctets:4963OutMcastOctets:5444#InBcastOctets: 179503 #S48 8 顯示PID 與程式名稱netstat -p#[root@centos7 ~]# netstat -p
##活動網際網路連線(無伺服器)Proto Recv-Q Send-Q 本機位址外部位址狀態PID/程式名稱tcp 0 52 centos7.3.local: ssh網關:51029 ESTABLISHED 1552/sshd: root@pts
tcp 0 0 centos7.3.local:ssh網關:51180 ESTABLISHED 1854 /sshd: root@ptss#UNIX字(無伺服器)Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ] DGRAM 8520 1/systemd /run/systemd/notify
unix 2 [] DGRAM 8522 1/systemd /run/systemd/cgroups-agent
unix 5 [] DGRAM 8542 1/systemd /run/systemd/journal/socket
unix 28 [ ] DGRAM 8544 1/systemd /dev/log
unix 2 [] DGRAM 11419 1/systemd /run/systemd/shutdownd
unix 3 [] 流已連接23993 1276/ibus-x111
以上是netstat指令是什麼?如何使用netstat指令?的詳細內容。更多資訊請關注PHP中文網其他相關文章!