netstat 可以用来查 Linx 网络系统接口的状态信息,统计信息,打开的 socket 连接,以及路由表等等。
在 CentOS 7 操作系统中,此命令默认是未安装的,所以我们可以使用如下命令进行网络工具包的安装。
yum install -y net-tools
注意,在 Windows 操作系统也是有这个命令的,用法一样。
我们可以使用netstat --help
命令来查看帮助文档。
[root@chenpihost ~]# netstat --help
usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [<Socket> ...]
netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]
-r, --route display routing table
-I, --interfaces=<Iface> display interface table for <Iface>
-i, --interfaces display interface table
-g, --groups display multicast group memberships
-s, --statistics display networking statistics (like SNMP)
-M, --masquerade display masqueraded connections
-v, --verbose be verbose
-W, --wide don't truncate IP addresses
-n, --numeric don't resolve names
--numeric-hosts don't resolve host names
--numeric-ports don't resolve port names
--numeric-users don't resolve user names
-N, --symbolic resolve hardware names
-e, --extend display other/more information
-p, --programs display PID/Program name for sockets
-o, --timers display timers
-c, --continuous continuous listing
-l, --listening display listening server sockets
-a, --all display all sockets (default: connected)
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
-Z, --context display SELinux security context for sockets
<Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-S|--sctp} {-w|--raw}
{-x|--unix} --ax25 --ipx --netrom
<AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet
List of possible address families (which support routing):
inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
x25 (CCITT X.25)
下面介绍几个常用的选项:
下面演示几个比较经常用的命令选项搭配使用。
显示所有 socket 连接,包括监听和未监听的,一般很少使用这个选项来查看,因为无用信息比较多。
[root@chenpihost ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 96 chenpihost:ssh 192.168.1.6:52061 ESTABLISHED
tcp6 0 0 [::]:6379 [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
udp 0 0 0.0.0.0:bootpc 0.0.0.0:*
udp 0 0 localhost:323 0.0.0.0:*
udp6 0 0 localhost:323 [::]:*
raw6 0 0 [::]:ipv6-icmp [::]:* 7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 20432 private/verify
unix 2 [ ACC ] STREAM LISTENING 21315 private/proxymap
unix 2 [ ACC ] STREAM LISTENING 21318 private/proxywrite
unix 2 [ ACC ] STREAM LISTENING 21321 private/smtp
unix 2 [ ACC ] STREAM LISTENING 21324 private/relay
unix 2 [ ACC ] STREAM LISTENING 21330 private/error
unix 2 [ ACC ] STREAM LISTENING 21333 private/retry
查看路由表信息。
[root@chenpihost ~]# 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 ens32
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens32
查看所有 TCP 相关。
[root@chenpihost ~]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 96 chenpihost:ssh desktop-ca0ir74:52061 ESTABLISHED
tcp6 0 0 [::]:6379 [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
查看所有 UDP 相关,使用得也少。
[root@chenpihost ~]# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:bootpc 0.0.0.0:*
udp 0 0 localhost:323 0.0.0.0:*
udp6 0 0 localhost:323 [::]:*
查看所有监听状态的 TCP 相关,并打印相关的程序名。我们一般使用此命令来查找主机上哪个程序占用哪个端口;或者是否服务正常启动,正常监听端口。这个选项搭配是平时最常用到的
。
Local Address 列中,如果 IP 是 0.0.0.0 则表示监听所有的 IP 地址,如果是监听 127.0.0.1 则表示此端口只能在本地访问。
[root@chenpihost ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1449/redis-server *
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1171/master
tcp6 0 0 :::6379 :::* LISTEN 1449/redis-server *
tcp6 0 0 :::22 :::* LISTEN 1070/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1171/master
如果要实时监听端口情况,则可以使用watch netstat -ntpl
命令,它会每2秒刷新一次,如下所示:
Every 2.0s: netstat -ntpl Wed Dec 29 23:59:20 2021
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1449/redis-server *
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1171/master
tcp6 0 0 :::6379 :::* LISTEN 1449/redis-server *
tcp6 0 0 :::22 :::* LISTEN 1070/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1171/master
当然,也可以使用-c
选项来实时监听端口情况,命令如下:
[root@chenpihost ~]# netstat -ntplc
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1449/redis-server *
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1171/master
tcp6 0 0 :::6379 :::* LISTEN 1449/redis-server *
tcp6 0 0 :::22 :::* LISTEN 1070/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1171/master
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1449/redis-server *
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1171/master
tcp6 0 0 :::6379 :::* LISTEN 1449/redis-server *
tcp6 0 0 :::22 :::* LISTEN 1070/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1171/master
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1449/redis-server *
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1171/master
tcp6 0 0 :::6379 :::* LISTEN 1449/redis-server *
tcp6 0 0 :::22 :::* LISTEN 1070/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1171/master
查看连接某服务端口最多的的 IP 地址。例如下面查看6379端口的。
[root@chenpihost ~]# netstat -ntu | grep :6379 | awk '{print $5}' | cut -d: -f1 | awk '{++ip[$1]} END {for(i in ip) print ip[i],"\t",i}' | sort -nr
2 127.0.0.1
TCP 各种状态列表。
[root@chenpihost ~]# netstat -nt | grep -e 127.0.0.1 -e 0.0.0.0 -e ::: -v | awk '/^tcp/ {++state[$NF]} END {for(i in state) print i,"\t",state[i]}'
ESTABLISHED 1
查看 redis 进程数,如果接近预设值,说明不够用,需要增加。
[root@chenpihost ~]# netstat -anpo | grep "redis" | wc -l
4
本次分享到此结束啦~~
如果觉得文章对你有帮助,点赞、收藏、关注、评论,您的支持就是我创作最大的动力!