Linux系统运行netstat命令时的过三关斩一将

2021-05-17 15:40:32 浏览数 (1)

1.简介

  这篇文章主要是记录在日常工作中遇到的一些问题,将其都总结整合到一起,方便查看,提高工作效率。小伙伴们看到标题可能觉得奇怪,不是过五关斩六将而是过三关斩一将。慢慢地往后看发现其中奥秘。

2.过三关斩一将

2.1第一关(没有这个命令)

宏哥在Linux上运行netstat时,提示没有这个命令,如下图所示:

代码语言:javascript复制
[admin@jumpclient1 ~]$ netstat -nap | grep 10493
-bash: netstat: command not found
[admin@jumpclient1 ~]$
2.2斩杀第一将(没有这个命令)

宏哥向来简单粗暴,直接yum -y install netstat。斩杀第一将,结果宏哥大意了,出现了第二关:不能正常安装。

2.3第二关(不能正常安装)

宏哥在运行yum -y install netstat ,不能正常安装。如下图所示:

代码语言:javascript复制
[admin@oftp202 ~]$ sudo yum install netstat
[sudo] password for admin: 
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: ftp.sjtu.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
base                                                                                                                                    | 3.6 kB  00:00:00     
extras                                                                                                                                  | 2.9 kB  00:00:00     
updates                                                                                                                                 | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                                                           | 153 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                                                                       | 236 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                                                                                                         | 6.1 MB  00:00:12     
(4/4): updates/7/x86_64/primary_db                                                                                                      | 8.0 MB  00:00:16     
No package netstat available.
Error: Nothing to do
2.3.斩杀第二将(不能正常安装)

其实,第二关就是因为没有可以用的netstat的安装包,所以不能正常安装。此时可以使用“yum search”来查询这个命令在哪个包中,就可以轻轻松松的斩杀第二将。如下图所示:

代码语言:javascript复制
[admin@oftp202 ~]$ yum search netstat
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.neusoft.edu.cn
 * extras: mirrors.neusoft.edu.cn
 * updates: mirrors.aliyun.com
====================================================================== Matched: netstat =======================================================================
dstat.noarch : Versatile resource statistics tool
net-snmp.x86_64 : A collection of SNMP protocol tools and libraries
net-tools.x86_64 : Basic networking tools  #这里已经给出了netstat命令所在包

接着直接安装这个包即可:yum -y install net-tools,就遇到了第三关:需要root执行权限执行。如下图所示:

代码语言:javascript复制
[admin@oftp202 ~]$ yum -y install net-tools
Loaded plugins: fastestmirror
You need to be root to perform this command.
2.4斩杀第三将(需要root执行)

需要root权限执行,那我们就用root的权限执行就可以了,如下图所示:

代码语言:javascript复制
[admin@oftp202 ~]$ sudo yum -y install net-tools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================
 Package                             Arch                             Version                                             Repository                      Size
===============================================================================================================================================================
Installing:
 net-tools                           x86_64                           2.0-0.25.20131004git.el7                            base                           306 k

Transaction Summary
===============================================================================================================================================================
Install  1 Package

Total download size: 306 k
Installed size: 917 k
Downloading packages:
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm                                                                                           | 306 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                   1/1 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                   1/1 

Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                                                                                                  

Complete!

到此,就完成了过三关斩一将(前边的两个直接被干死)。重试命令。

3.重试命令

重试命令:netstat -nap | grep 10493 ,如下图所示:

代码语言:javascript复制
[admin@oftp202 ~]$ netstat -nap | grep 10493
(No info could be read for "-p": geteuid()=1000 but you should be root.)

提示还是需要root的权限执行,使用root权限执行,如下图所示:

代码语言:javascript复制
[admin@oftp201 ~]$ sudo netstat -nap | grep 30542
tcp        0      0 127.0.0.1:56368         127.0.0.1:9090          ESTABLISHED 30542/./cea_webFron 
tcp        0      0 127.0.0.1:56366         127.0.0.1:9090          ESTABLISHED 30542/./cea_webFron 
tcp6       0      0 :::8080                 :::*                    LISTEN      30542/./cea_webFron 
tcp6       0      0 10.11.55.173:8080       10.11.55.121:40902      ESTABLISHED 30542/./cea_webFron 
tcp6       0      0 10.11.55.173:8080       10.11.55.121:40900      ESTABLISHED 30542/./cea_webFron 
tcp6       0      0 10.11.55.173:8080       10.11.55.121:38702      ESTABLISHED 30542/./cea_webFron 
tcp6       0      0 10.11.55.173:8080       10.11.55.121:38708      ESTABLISHED 30542/./cea_webFron 
tcp6       0      0 10.11.55.173:8080       10.11.55.121:40904      ESTABLISHED 30542/./cea_webFron 
tcp6       0      0 10.11.55.173:8080       10.11.55.121:40906      ESTABLISHED 30542/./cea_webFron 

4.小结

4.1斩杀第二将的另一种方法(使用yum whatprovides)

查找安装包,还可以使用yum whatprovides,如下图所示:

代码语言:javascript复制
[admin@oftp202 ~]$ yum  whatprovides *netstat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.neusoft.edu.cn
 * extras: mirrors.neusoft.edu.cn
 * updates: mirrors.aliyun.com
base/7/x86_64/filelists_db                                                                                                              | 7.2 MB  00:00:10     
extras/7/x86_64/filelists_db                                                                                                            | 231 kB  00:00:00     
updates/7/x86_64/filelists_db                                                                                                           | 4.7 MB  00:00:05     
1:net-snmp-utils-5.7.2-49.el7.x86_64 : Network management utilities using SNMP, from the NET-SNMP project
Repo        : base
Matched from:
Filename    : /usr/bin/snmpnetstat



1:net-snmp-utils-5.7.2-49.el7_9.1.x86_64 : Network management utilities using SNMP, from the NET-SNMP project
Repo        : updates
Matched from:
Filename    : /usr/bin/snmpnetstat



net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools
Repo        : base
Matched from:
Filename    : /bin/netstat



zsh-5.0.2-34.el7_8.2.x86_64 : Powerful interactive shell
Repo        : base
Matched from:
Filename    : /usr/share/zsh/5.0.2/functions/_netstat



net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools
Repo        : @base
Matched from:
Filename    : /bin/netstat

好了今天主要是介绍一下,如何使用netstat命令根据进程号,来查看这个进程占用那些端口,以避免你在使用端口时出现端口占用的情况。

yum

0 人点赞