加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

如何在Linux中查找服务的端口号

发布时间:2019-07-07 15:25:36 所属栏目:Windows 来源:Sk
导读:副标题#e# 由于某些原因,你可能经常需要查找端口名称和端口号。如果是这样,你很幸运。今天,在这个简短的教程中,我们将看到在 Linux 系统中最简单、最快捷的查找服务端口号的方法。可能有很多方法可以做到,但我目前只知道以下三种方法。请继续阅读。 在
副标题[/!--empirenews.page--]

/uploads/allimg/c190707/15624H4J45930-1b31.png

由于某些原因,你可能经常需要查找端口名称和端口号。如果是这样,你很幸运。今天,在这个简短的教程中,我们将看到在 Linux 系统中最简单、最快捷的查找服务端口号的方法。可能有很多方法可以做到,但我目前只知道以下三种方法。请继续阅读。

在 Linux 中查找服务的端口号

方法1:使用 grep 命令

要使用 grep 命令在 Linux 中查找指定服务的默认端口号,只需运行:

  1. $ grep <port> /etc/services

例如,要查找 SSH 服务的默认端口,只需运行:

  1. $ grep ssh /etc/services

就这么简单。此命令应该适用于大多数 Linux 发行版。以下是我的 Arch Linux 测试机中的示例输出:

  1. ssh 22/tcp
  2. ssh 22/udp
  3. ssh 22/sctp
  4. sshell 614/tcp
  5. sshell 614/udp
  6. netconf-ssh 830/tcp
  7. netconf-ssh 830/udp
  8. sdo-ssh 3897/tcp
  9. sdo-ssh 3897/udp
  10. netconf-ch-ssh 4334/tcp
  11. snmpssh 5161/tcp
  12. snmpssh-trap 5162/tcp
  13. tl1-ssh 6252/tcp
  14. tl1-ssh 6252/udp
  15. ssh-mgmt 17235/tcp
  16. ssh-mgmt 17235/udp

正如你在上面的输出中所看到的,SSH 服务的默认端口号是 22。

让我们找到 Apache Web 服务器的端口号。为此,命令是:

  1. $ grep http /etc/services
  2. # http://www.iana.org/assignments/port-numbers
  3. http 80/tcp www www-http # WorldWideWeb HTTP
  4. http 80/udp www www-http # HyperText Transfer Protocol
  5. http 80/sctp # HyperText Transfer Protocol
  6. https 443/tcp # http protocol over TLS/SSL
  7. https 443/udp # http protocol over TLS/SSL
  8. https 443/sctp # http protocol over TLS/SSL
  9. gss-http 488/tcp
  10. gss-http 488/udp
  11. webcache 8080/tcp http-alt # WWW caching service
  12. webcache 8080/udp http-alt # WWW caching service
  13. [...]

FTP 端口号是什么?这很简单!

  1. $ grep ftp /etc/services
  2. ftp-data 20/tcp
  3. ftp-data 20/udp
  4. # 21 is registered to ftp, but also used by fsp
  5. ftp 21/tcp
  6. ftp 21/udp fsp fspd
  7. tftp 69/tcp
  8. [...]

方法 2:使用 getent 命令

如你所见,上面的命令显示指定搜索词 “ssh”、“http” 和 “ftp” 的所有端口名称和数字。这意味着,你将获得与给定搜索词匹配的所有端口名称的相当长的输出。

但是,你可以使用 getent 命令精确输出结果,如下所示:

  1. $ getent services ssh
  2. ssh 22/tcp
  3.  
  4. $ getent services http
  5. http 80/tcp www www-http
  6.  
  7. $ getent services ftp
  8. ftp 21/tcp

如果你不知道端口名称,但是知道端口号,那么你只需将端口名称替换为数字:

  1. $ getent services 80
  2. http 80/tcp

要显示所有端口名称和端口号,只需运行:

  1. $ getent services

方法 3:使用 Whatportis 程序

Whatportis 是一个简单的 Python 脚本,来用于查找端口名称和端口号。与上述命令不同,此程序以漂亮的表格形式输出。

确保已安装 pip 包管理器。如果没有,请参考以下链接。

  • 如何使用 pip 管理 Python 包

安装 pip 后,运行以下命令安装 Whatportis 程序。

  1. $ pip install whatportis

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读