查看远程Linux系统中某个端口是否开启的3种方法

查看远程Linux系统中某个端口是否开启的3种方法

这是一个很重要的话题,不仅对 Linux 管理员而言,对于我们大家而言也非常有帮助。我的意思是说对于工作在 IT 基础设施行业的用户来说,了解这个话题也是非常有用的。他们需要在执行下一步操作前,检查 Linux 服务器上某个端口是否开启。

假如这个端口没有被开启,则他们会直接找 Linux 管理员去开启它。如果这个端口已经开启了,则我们需要和应用团队来商量下一步要做的事。

在本篇文章中,我们将向你展示如何检查某个端口是否开启的 3 种方法。

这个目标可以使用下面的 Linux 命令来达成:

  • nc:netcat 是一个简单的 Unix 工具,它使用 TCP 或 UDP 协议去读写网络连接间的数据。
  • nmap:(“Network Mapper”)是一个用于网络探索和安全审计的开源工具,被设计用来快速地扫描大规模网络。
  • telnet:被用来交互地通过 TELNET 协议与另一台主机通信。

如何使用 nc(netcat)命令来查看远程 Linux 系统中某个端口是否开启?

ncnetcatnetcat 是一个简单的 Unix 工具,它使用 TCP 或 UDP 协议去读写网络连接间的数据。

它被设计成为一个可信赖的后端工具,可被直接使用或者简单地被其他程序或脚本调用。

与此同时,它也是一个富含功能的网络调试和探索工具,因为它可以创建你所需的几乎所有类型的连接,并且还拥有几个内置的有趣功能。

netcat 有三类功能模式,它们分别为连接模式、监听模式和隧道模式。

ncnetcat)命令的一般语法:

  1. <span class="pln">$ nc </span><span class="pun">[-</span><span class="pln">options</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="typ">HostName</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> IP</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="typ">PortNumber</span><span class="pun">]</span>

在下面的例子中,我们将检查远程 Linux 系统中的 22 端口是否开启。

假如端口是开启的,你将获得类似下面的输出。

  1. <span class="com">#</span><span class="pln"> nc </span><span class="pun">-</span><span class="pln">zvw3 </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.8</span><span class="pln"> </span><span class="lit">22</span>
  2. <span class="typ">Connection</span><span class="pln"> to </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.8</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> port </span><span class="pun">[</span><span class="pln">tcp</span><span class="pun">/</span><span class="kwd">ssh</span><span class="pun">]</span><span class="pln"> succeeded</span><span class="pun">!</span>

命令详解:

  • nc:即执行的命令主体;
  • z:零 I/O 模式(被用来扫描);
  • v:显式地输出;
  • w3:设置超时时间为 3 秒;
  • 192.168.1.8:目标系统的 IP 地址;
  • 22:需要验证的端口。

当检测到端口没有开启,你将获得如下输出:

  1. <span class="com">#</span><span class="pln"> nc </span><span class="pun">-</span><span class="pln">zvw3 </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.95</span><span class="pln"> </span><span class="lit">22</span>
  2. <span class="pln">nc</span><span class="pun">:</span><span class="pln"> connect to </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.95</span><span class="pln"> port </span><span class="lit">22</span><span class="pln"> </span><span class="pun">(</span><span class="pln">tcp</span><span class="pun">)</span><span class="pln"> failed</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Connection</span><span class="pln"> refused</span>

如何使用 nmap 命令来查看远程 Linux 系统中某个端口是否开启?

nmap(“Network Mapper”)是一个用于网络探索和安全审计的开源工具,被设计用来快速地扫描大规模网络,尽管对于单个主机它也同样能够正常工作。

nmap 以一种新颖的方式,使用裸 IP 包来决定网络中的主机是否可达,这些主机正提供什么服务(应用名和版本号),它们运行的操作系统(系统的版本),它们正在使用的是什么***滤软件或者防火墙,以及其他额外的特性。

尽管 nmap 通常被用于安全审计,许多系统和网络管理员发现在一些日常任务(例如罗列网络资产、管理服务升级的计划、监视主机或者服务是否正常运行)中,它也同样十分有用。

nmap 的一般语法:

  1. <span class="pln">$ </span><span class="kwd">nmap</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">options</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="typ">HostName</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> IP</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="typ">PortNumber</span><span class="pun">]</span>

假如端口是开启的,你将获得如下的输出:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">nmap</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.8</span><span class="pln"> </span><span class="pun">-</span><span class="pln">p </span><span class="lit">22</span>
  2.  
  3. <span class="typ">Starting</span><span class="pln"> </span><span class="typ">Nmap</span><span class="pln"> </span><span class="lit">7.70</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//nmap.org ) at 2019-03-16 03:37 IST Nmap scan report for 192.168.1.8 Host is up (0.00031s latency).</span>
  4.  
  5. <span class="pln">PORT STATE SERVICE</span>
  6.  
  7. <span class="lit">22</span><span class="pun">/</span><span class="pln">tcp open </span><span class="kwd">ssh</span><span class="pln"> </span>
  8.  
  9. <span class="typ">Nmap</span><span class="pln"> </span><span class="kwd">done</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> IP address </span><span class="pun">(</span><span class="lit">1</span><span class="pln"> host up</span><span class="pun">)</span><span class="pln"> scanned </span><span class="kwd">in</span><span class="pln"> </span><span class="lit">13.06</span><span class="pln"> seconds</span>

假如端口没有开启,你将得到类似下面的结果:

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">nmap</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.8</span><span class="pln"> </span><span class="pun">-</span><span class="pln">p </span><span class="lit">80</span>
  2. <span class="typ">Starting</span><span class="pln"> </span><span class="typ">Nmap</span><span class="pln"> </span><span class="lit">7.70</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//nmap.org ) at 2019-03-16 04:30 IST</span>
  3. <span class="typ">Nmap</span><span class="pln"> scan report </span><span class="kwd">for</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.8</span>
  4. <span class="typ">Host</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> up </span><span class="pun">(</span><span class="lit">0.00036s</span><span class="pln"> latency</span><span class="pun">).</span>
  5.  
  6. <span class="pln">PORT STATE SERVICE</span>
  7. <span class="lit">80</span><span class="pun">/</span><span class="pln">tcp closed http</span>
  8.  
  9. <span class="typ">Nmap</span><span class="pln"> </span><span class="kwd">done</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> IP address </span><span class="pun">(</span><span class="lit">1</span><span class="pln"> host up</span><span class="pun">)</span><span class="pln"> scanned </span><span class="kwd">in</span><span class="pln"> </span><span class="lit">13.07</span><span class="pln"> seconds</span>

如何使用 telnet 命令来查看远程 Linux 系统中某个端口是否开启?

telnet 命令被用来交互地通过 TELNET 协议与另一台主机通信。

telnet 命令的一般语法:

  1. <span class="pln">$ telnet </span><span class="pun">[</span><span class="typ">HostName</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> IP</span><span class="pun">]</span><span class="pln"> </span><span class="pun">[</span><span class="typ">PortNumber</span><span class="pun">]</span>

假如探测成功,你将看到类似下面的输出:

  1. <span class="pln">$ telnet </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.9</span><span class="pln"> </span><span class="lit">22</span>
  2. <span class="typ">Trying</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.9</span><span class="pun">...</span>
  3. <span class="typ">Connected</span><span class="pln"> to </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.9</span><span class="pun">.</span>
  4. <span class="typ">Escape</span><span class="pln"> character </span><span class="kwd">is</span><span class="pln"> </span><span class="str">'^]'</span><span class="pun">.</span>
  5. <span class="pln">SSH</span><span class="pun">-</span><span class="lit">2.0</span><span class="pun">-</span><span class="typ">OpenSSH_5</span><span class="pun">.</span><span class="lit">3</span>
  6. <span class="pun">^]</span>
  7. <span class="typ">Connection</span><span class="pln"> closed by foreign host</span><span class="pun">.</span>

假如探测失败,你将看到类似下面的输出:

相关推荐