Linux 疑难解答 – 在 CentOS 7/8 和 RHEL 7/8 中找不到 netstat 命令

你可能知道, 网络统计 (工作 统计istics) 是一个命令行工具,可用于检查网络配置和活动。 netcat 命令在 RHEL 8、7 和它的克隆(如 CentOS 8 和 7)中不可用。这个简短的教程描述了如何让 netstat 命令在 CentOS 7、RHEL 7 系统中工作。

重要说明:

正如我们的一位读者在下面的评论部分指出的那样,许多旧的 Linux 网络命令已被弃用,取而代之的是它们的 iproute2 对应命令。 尤其是 Netstat 已被 “咝咝” 默认情况下可用的命令。 如果您出于任何原因仍使用 netstat,此提示可能会有所帮助。

在 CentOS 7/8 和 RHEL 7/8 中找不到 netstat 命令

首先,我们将看到如何在 CentOS 8 和 RHEL 8 服务器版本中修复此错误。

修复 CentOS 8 和 RHEL 8 中的“netstat command not found”错误

首先,我们需要找到哪个包提供 ‘网络统计’ 命令使用以下命令:

# dnf provides */netstat

或者,

# dnf whatprovides */netstat

我的 CentOS 8 服务器的示例输出:

net-tools-2.0-0.51.20160912git.el8.x86_64 : Basic networking tools
Repo        : BaseOS
Matched from:
Filename    : /usr/bin/netstat

如您所见,“net-tools”包提供了“netstat”命令。

因此,让我们使用以下命令以 root 用户身份安装 net-tools 包 用户:

# dnf install net-tools

样本输出:

CentOS-8 - AppStream                                                                                                                      1.5 kB/s | 4.3 kB     00:02    
CentOS-8 - Base                                                                                                                           4.5 kB/s | 3.8 kB     00:00    
CentOS-8 - Extras                                                                                                                         1.7 kB/s | 1.5 kB     00:00    
Dependencies resolved.
==========================================================================================================================================================================
 Package                               Architecture                       Version                                                Repository                          Size
==========================================================================================================================================================================
Installing:
 net-tools                             x86_64                             2.0-0.51.20160912git.el8                               BaseOS                             323 k

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

Total download size: 323 k
Installed size: 1.0 M
Is this ok [y/N]: y
Downloading Packages:
net-tools-2.0-0.51.20160912git.el8.x86_64.rpm                                                                                             253 kB/s | 323 kB     00:01    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                     170 kB/s | 323 kB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                  1/1 
  Installing       : net-tools-2.0-0.51.20160912git.el8.x86_64                                                                                                        1/1 
  Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64                                                                                                        1/1 
  Verifying        : net-tools-2.0-0.51.20160912git.el8.x86_64                                                                                                        1/1 

Installed:
  net-tools-2.0-0.51.20160912git.el8.x86_64                                                                                                                               

Complete!

现在你可以在你的 CentOS 8 系统上使用 netstat 命令了。

修复 CentOS 7 和 RHEL 7 中的“netstat command not found”错误

首先,我们应该找出哪个包提供 ‘网络统计’ 命令。

要找出答案,请运行:

$ yum provides */netstat

或者,

$ yum whatprovides */netstat

样本输出:

yum provides */netstat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.nbrc.ac.in
 * extras: mirror.nbrc.ac.in
 * updates: mirror.nbrc.ac.in
net-tools-2.0-0.17.20131004git.el7.x86_64 : Basic networking tools
Repo : base
Matched from:
Filename : /bin/netstat

正如你在上面的输出中看到的,我们需要安装 网络工具 包以获取 netstat 命令。

因此,让我们使用终端中的以下命令安装 net-tools 命令:

$ sudo yum install net-tools

样本输出:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.nbrc.ac.in
 * extras: mirror.nbrc.ac.in
 * updates: mirror.nbrc.ac.in
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

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

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

Total download size: 304 k
Installed size: 917 k
Is this ok [y/d/N]: y
Downloading packages:
net-tools-2.0-0.17.20131004git.el7.x86_64.rpm | 304 kB 00:00:05 
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 Installing : net-tools-2.0-0.17.20131004git.el7.x86_64 1/1 
 Verifying : net-tools-2.0-0.17.20131004git.el7.x86_64 1/1

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

Complete!

就是这样。 现在您可以使用“netstat”命令。

$ netstat

样本输出:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State 
tcp 0 0 server.ostechnix.com:ssh sk:54534 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 10304 /run/systemd/shutdownd
unix 2 [ ] DGRAM 10242 @/org/freedesktop/systemd1/notify
unix 5 [ ] DGRAM 6104 /run/systemd/journal/socket
unix 13 [ ] DGRAM 6106 /dev/log
unix 3 [ ] STREAM CONNECTED 14013 /var/run/dbus/system_bus_socket
unix 2 [ ] DGRAM 24798 
unix 3 [ ] STREAM CONNECTED 17856 
unix 3 [ ] STREAM CONNECTED 17874 
unix 3 [ ] STREAM CONNECTED 15064

[...]

unix 3 [ ] STREAM CONNECTED 17878 
unix 3 [ ] STREAM CONNECTED 17832 
unix 3 [ ] STREAM CONNECTED 17841 
unix 3 [ ] STREAM CONNECTED 14010 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 14710 /var/run/dbus/system_bus_socket
unix 2 [ ] DGRAM 11594 
unix 3 [ ] STREAM CONNECTED 16500 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 17809 
unix 3 [ ] STREAM CONNECTED 17836 
unix 2 [ ] DGRAM 13906 
unix 3 [ ] STREAM CONNECTED 17845

希望这可以帮助。

建议阅读:

  • 如何查找正在侦听特定端口的服务
  • 如何在 Linux 上查找可用的网络接口

感谢您的光临!

帮助我们帮助您:

祝你有美好的一天!!