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:
destination unreachable: 148
echo replies: 528
717 icmp messages sent
0 icmp messages failed
icmp output histogram:
destination unreachable: 148
echo request: 569
[……]
(2)显示 tcp 端口的统计信息 netstat -st
[root@centos7 ~]# netstat -st
icmpmsg:
intype0: 528
intype3: 148
outtype3: 148
outtype8: 569
tcp:
0 active connections openings
2 passive connection openings
0 failed connection attempts
0 connection resets received
2 connections established
1637 segments received
1418 segments send out
0 segments retransmited
0 bad segments received.
0 resets sent
(3)显示 udp 端口的统计信息 netstat -su
[root@centos7 ~]# netstat -su
icmpmsg:
intype0: 528
intype3: 148
outtype3: 148
outtype8: 569
udp:
0 packets received
140 packets to unknown port received.
0 packet receive errors
169 packets sent
0 receive buffer errors
0 send buffer errors
udplite:
ipext:
inmcastpkts: 27
outmcastpkts: 33
inbcastpkts: 766
inoctets: 410294
outoctets: 432666
inmcastoctets: 4963
outmcastoctets: 5444
inbcastoctets: 179503
innoectpkts: 3848
8 显示 pid 和进程名称 netstat -p
[root@centos7 ~]# netstat -p
active internet connections (w/o servers)
proto recv-q send-q local address foreign address state pid/program name
tcp 0 52 centos7.3.local:ssh gateway:51029 established 1552/sshd: root@pts
tcp 0 0 centos7.3.local:ssh gateway:51180 established 1854/sshd: root@pts
active unix domain sockets (w/o servers)
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 [ ] stream connected 23993 1276/ibus-x11
以上就是netstat命令是什么?如何使用netstat命令?的详细内容。