mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated
This commit is contained in:
parent
7742b8482e
commit
99d87b057b
@ -1,210 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How To Find The Port Number Of A Service In Linux)
|
||||
[#]: via: (https://www.ostechnix.com/how-to-find-the-port-number-of-a-service-in-linux/)
|
||||
[#]: author: (sk https://www.ostechnix.com/author/sk/)
|
||||
|
||||
How To Find The Port Number Of A Service In Linux
|
||||
======
|
||||
|
||||
![Find The Port Number Of A Service In Linux OS][1]
|
||||
|
||||
You might often need to find the port names and numbers for some reasons. If so, you’re in luck. Today, in this brief tutorial, we are going to see the easiest and quickest ways to find the port number of a service in Linux operating system. There could be many methods to do it, but I am aware of the following three methods only at present. Read on.
|
||||
|
||||
### Find The Port Number Of A Service In Linux
|
||||
|
||||
**Method 1 – Using[Grep][2] command:**
|
||||
|
||||
To find the default port number of a given service in Linux using grep command, just run:
|
||||
|
||||
```
|
||||
$ grep <port> /etc/services
|
||||
```
|
||||
|
||||
For example, to find the default port of a SSH service, simply run:
|
||||
|
||||
```
|
||||
$ grep ssh /etc/services
|
||||
```
|
||||
|
||||
It’s that simple. This command should work on most Linux distributions. Here is the sample output from my Arch Linux test box:
|
||||
|
||||
```
|
||||
ssh 22/tcp
|
||||
ssh 22/udp
|
||||
ssh 22/sctp
|
||||
sshell 614/tcp
|
||||
sshell 614/udp
|
||||
netconf-ssh 830/tcp
|
||||
netconf-ssh 830/udp
|
||||
sdo-ssh 3897/tcp
|
||||
sdo-ssh 3897/udp
|
||||
netconf-ch-ssh 4334/tcp
|
||||
snmpssh 5161/tcp
|
||||
snmpssh-trap 5162/tcp
|
||||
tl1-ssh 6252/tcp
|
||||
tl1-ssh 6252/udp
|
||||
ssh-mgmt 17235/tcp
|
||||
ssh-mgmt 17235/udp
|
||||
```
|
||||
|
||||
As you can see in the above output, the default port number of SSH service is 22.
|
||||
|
||||
Let us find the port number of Apache web server. To do so, the command would be:
|
||||
|
||||
```
|
||||
$ grep http /etc/services
|
||||
# http://www.iana.org/assignments/port-numbers
|
||||
http 80/tcp www www-http # WorldWideWeb HTTP
|
||||
http 80/udp www www-http # HyperText Transfer Protocol
|
||||
http 80/sctp # HyperText Transfer Protocol
|
||||
https 443/tcp # http protocol over TLS/SSL
|
||||
https 443/udp # http protocol over TLS/SSL
|
||||
https 443/sctp # http protocol over TLS/SSL
|
||||
gss-http 488/tcp
|
||||
gss-http 488/udp
|
||||
webcache 8080/tcp http-alt # WWW caching service
|
||||
webcache 8080/udp http-alt # WWW caching service
|
||||
[...]
|
||||
```
|
||||
|
||||
How about FTP port number? That’s easy!
|
||||
|
||||
```
|
||||
$ grep ftp /etc/services
|
||||
ftp-data 20/tcp
|
||||
ftp-data 20/udp
|
||||
# 21 is registered to ftp, but also used by fsp
|
||||
ftp 21/tcp
|
||||
ftp 21/udp fsp fspd
|
||||
tftp 69/tcp
|
||||
[...]
|
||||
```
|
||||
|
||||
**Method 2 – Using getent command**
|
||||
|
||||
As you can see, the above commands shows all port names and numbers for the given search term “ssh”, “http” and “ftp”. That means, you will get a quite long output of all port names that matches with the given search term.
|
||||
|
||||
You can, however, narrow down the result to exact output using “getent” command like below:
|
||||
|
||||
```
|
||||
$ getent services ssh
|
||||
ssh 22/tcp
|
||||
|
||||
$ getent services http
|
||||
http 80/tcp www www-http
|
||||
|
||||
$ getent services ftp
|
||||
ftp 21/tcp
|
||||
```
|
||||
|
||||
If you don’t know the port name but the port number, simply replace the port name with number like below:
|
||||
|
||||
```
|
||||
$ getent services 80
|
||||
http 80/tcp
|
||||
```
|
||||
|
||||
To display all port names and numbers, simply run:
|
||||
|
||||
```
|
||||
$ getent services
|
||||
```
|
||||
|
||||
* * *
|
||||
|
||||
**Suggested read:**
|
||||
|
||||
* [**How To Change Apache Default Port To A Custom Port**][3]
|
||||
* [**How To Change FTP Default Port To A Custom Port**][4]
|
||||
* [**How To Change SSH Default Port To A Custom Port**][5]
|
||||
|
||||
|
||||
|
||||
* * *
|
||||
|
||||
**Method 3 – Using Whatportis Utility**
|
||||
|
||||
The **Whatportis** is a simple python script used to find port names and numbers. Unlike the above commands, this utility displays the output in a nice tabular column format.
|
||||
|
||||
Make sure you have installed PIP package manager. If not, refer the following link.
|
||||
|
||||
* [**How To Manage Python Packages Using Pip**][6]
|
||||
|
||||
|
||||
|
||||
Once installed PIP, run the following command to install Whatportis utility.
|
||||
|
||||
```
|
||||
$ pip install whatportis
|
||||
```
|
||||
|
||||
Now, you can find what port is associated with a service as shown below.
|
||||
|
||||
```
|
||||
$ whatportis ssh
|
||||
|
||||
$ whatportis ftp
|
||||
|
||||
$ whatportis http
|
||||
```
|
||||
|
||||
Sample output from my CentOS 7 server:
|
||||
|
||||
![][7]
|
||||
|
||||
Find The Port Number Of A Service In Linux
|
||||
|
||||
If you don’t know the exact name of a service, use **–like** flag to display the relevant results.
|
||||
|
||||
```
|
||||
$ whatportis mysql --like
|
||||
```
|
||||
|
||||
The above commands helped you to find what port is associated with a service. You can also find what service is associated with a port number like below.
|
||||
|
||||
```
|
||||
$ whatportis 993
|
||||
```
|
||||
|
||||
You can even display the results in **JSON** format.
|
||||
|
||||
```
|
||||
$ whatportis 993 --json
|
||||
```
|
||||
|
||||
![][8]
|
||||
|
||||
For more details, refer the GitHub repository.
|
||||
|
||||
* [**Whatportis GitHub Repository**][9]
|
||||
|
||||
|
||||
|
||||
And, that’s all for now. You know now how to find the port names and numbers in Linux using three simple methods. If you know any other methods/commands, let me know in the comment section below. I will check and update this guide accordingly.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/how-to-find-the-port-number-of-a-service-in-linux/
|
||||
|
||||
作者:[sk][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.ostechnix.com/author/sk/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.ostechnix.com/wp-content/uploads/2018/06/Find-The-Port-Number-720x340.png
|
||||
[2]: https://www.ostechnix.com/the-grep-command-tutorial-with-examples-for-beginners/
|
||||
[3]: https://www.ostechnix.com/how-to-change-apache-ftp-and-ssh-default-port-to-a-custom-port-part-1/
|
||||
[4]: https://www.ostechnix.com/how-to-change-apache-ftp-and-ssh-default-port-to-a-custom-port-part-2/
|
||||
[5]: https://www.ostechnix.com/how-to-change-apache-ftp-and-ssh-default-port-to-a-custom-port-part-3/
|
||||
[6]: https://www.ostechnix.com/manage-python-packages-using-pip/
|
||||
[7]: https://www.ostechnix.com/wp-content/uploads/2018/06/whatportis.png
|
||||
[8]: https://www.ostechnix.com/wp-content/uploads/2018/06/whatportis-1.png
|
||||
[9]: https://github.com/ncrocfer/whatportis
|
@ -0,0 +1,195 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How To Find The Port Number Of A Service In Linux)
|
||||
[#]: via: (https://www.ostechnix.com/how-to-find-the-port-number-of-a-service-in-linux/)
|
||||
[#]: author: (sk https://www.ostechnix.com/author/sk/)
|
||||
|
||||
如何在 Linux 中查找服务的端口号
|
||||
======
|
||||
|
||||
![Find The Port Number Of A Service In Linux OS][1]
|
||||
|
||||
由于某些原因,你可能经常需要查找端口名称和端口号。如果是这样,你很幸运。今天,在这个简短的教程中,我们将看到在 Linux 系统中最简单、最快捷的查找服务端口号的方法。可能有很多方法可以做到,但我目前只知道以下三种方法。请继续阅读。
|
||||
|
||||
### 在 Linux 中查找服务的端口号
|
||||
|
||||
**方法1:使用 [grep][2] 命令**
|
||||
|
||||
要使用 grep 命令在 Linux 中查找指定服务的默认端口号,只需运行:
|
||||
|
||||
```
|
||||
$ grep <port> /etc/services
|
||||
```
|
||||
|
||||
例如,要查找 SSH 服务的默认端口,只需运行:
|
||||
|
||||
```
|
||||
$ grep ssh /etc/services
|
||||
```
|
||||
|
||||
就这么简单。此命令应该适用于大多数 Linux 发行版。以下是我的 Arch Linux 测试机中的示例输出:
|
||||
|
||||
```
|
||||
ssh 22/tcp
|
||||
ssh 22/udp
|
||||
ssh 22/sctp
|
||||
sshell 614/tcp
|
||||
sshell 614/udp
|
||||
netconf-ssh 830/tcp
|
||||
netconf-ssh 830/udp
|
||||
sdo-ssh 3897/tcp
|
||||
sdo-ssh 3897/udp
|
||||
netconf-ch-ssh 4334/tcp
|
||||
snmpssh 5161/tcp
|
||||
snmpssh-trap 5162/tcp
|
||||
tl1-ssh 6252/tcp
|
||||
tl1-ssh 6252/udp
|
||||
ssh-mgmt 17235/tcp
|
||||
ssh-mgmt 17235/udp
|
||||
```
|
||||
|
||||
正如你在上面的输出中所看到的,SSH 服务的默认端口号是 22。
|
||||
|
||||
让我们找到 Apache Web 服务器的端口号。为此,命令是:
|
||||
|
||||
```
|
||||
$ grep http /etc/services
|
||||
# http://www.iana.org/assignments/port-numbers
|
||||
http 80/tcp www www-http # WorldWideWeb HTTP
|
||||
http 80/udp www www-http # HyperText Transfer Protocol
|
||||
http 80/sctp # HyperText Transfer Protocol
|
||||
https 443/tcp # http protocol over TLS/SSL
|
||||
https 443/udp # http protocol over TLS/SSL
|
||||
https 443/sctp # http protocol over TLS/SSL
|
||||
gss-http 488/tcp
|
||||
gss-http 488/udp
|
||||
webcache 8080/tcp http-alt # WWW caching service
|
||||
webcache 8080/udp http-alt # WWW caching service
|
||||
[...]
|
||||
```
|
||||
|
||||
FTP 端口号是什么?这很简单!
|
||||
|
||||
```
|
||||
$ grep ftp /etc/services
|
||||
ftp-data 20/tcp
|
||||
ftp-data 20/udp
|
||||
# 21 is registered to ftp, but also used by fsp
|
||||
ftp 21/tcp
|
||||
ftp 21/udp fsp fspd
|
||||
tftp 69/tcp
|
||||
[...]
|
||||
```
|
||||
|
||||
**方法 2:使用 getent 命令**
|
||||
|
||||
如你所见,上面的命令显示指定搜索词 “ssh”、“http” 和 “ftp” 的所有端口名称和数字。这意味着,你将获得与给定搜索词匹配的所有端口名称的相当长的输出。
|
||||
|
||||
但是,你可以使用 “getent” 命令精确输出结果,如下所示:
|
||||
|
||||
```
|
||||
$ getent services ssh
|
||||
ssh 22/tcp
|
||||
|
||||
$ getent services http
|
||||
http 80/tcp www www-http
|
||||
|
||||
$ getent services ftp
|
||||
ftp 21/tcp
|
||||
```
|
||||
|
||||
如果你不知道端口名称,但是知道端口号,那么你只需将端口名称替换为数字:
|
||||
|
||||
```
|
||||
$ getent services 80
|
||||
http 80/tcp
|
||||
```
|
||||
|
||||
要显示所有端口名称和端口号,只需运行:
|
||||
|
||||
```
|
||||
$ getent services
|
||||
```
|
||||
|
||||
**方法 3:使用 Whatportis 程序**
|
||||
|
||||
**Whatportis** 是一个简单的 python 脚本,来用于查找端口名称和端口号。与上述命令不同,此程序以漂亮的表格形式输出。
|
||||
|
||||
确保已安装 PIP 包管理器。如果没有,请参考以下链接。
|
||||
|
||||
* [**如何使用 Pip 管理 Python 包**][6]
|
||||
|
||||
|
||||
|
||||
安装 PIP 后,运行以下命令安装 Whatportis 程序。
|
||||
|
||||
```
|
||||
$ pip install whatportis
|
||||
```
|
||||
|
||||
现在,你可以找到与服务关联的端口,如下所示。
|
||||
|
||||
```
|
||||
$ whatportis ssh
|
||||
|
||||
$ whatportis ftp
|
||||
|
||||
$ whatportis http
|
||||
```
|
||||
|
||||
我的 CentOS 7 服务器的示例输出:
|
||||
|
||||
![][7]
|
||||
|
||||
在 Linux 中查找服务的端口号
|
||||
|
||||
如果你不知道服务的确切名称,请使用 **–like** 标志来显示相关结果。
|
||||
|
||||
```
|
||||
$ whatportis mysql --like
|
||||
```
|
||||
|
||||
上述命令帮助你查找与服务关联的端口。你还可以找到与端口号相关联的服务,如下所示。
|
||||
|
||||
```
|
||||
$ whatportis 993
|
||||
```
|
||||
|
||||
你甚至可以以 **JSON** 格式显示结果。
|
||||
|
||||
```
|
||||
$ whatportis 993 --json
|
||||
```
|
||||
|
||||
![][8]
|
||||
|
||||
有关更多详细信息,请参阅 GitHub 仓库。
|
||||
|
||||
* [**Whatportis GitHub 仓库**][9]
|
||||
|
||||
|
||||
|
||||
就是这些了。你现在知道了如何使用三种简单方法在 Linux 中查找端口名称和端口号。如果你知道任何其他方法/命令,请在下面的评论栏告诉我。我会查看并更相应地更新本指南。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/how-to-find-the-port-number-of-a-service-in-linux/
|
||||
|
||||
作者:[sk][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.ostechnix.com/author/sk/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.ostechnix.com/wp-content/uploads/2018/06/Find-The-Port-Number-720x340.png
|
||||
[2]: https://www.ostechnix.com/the-grep-command-tutorial-with-examples-for-beginners/
|
||||
[6]: https://www.ostechnix.com/manage-python-packages-using-pip/
|
||||
[7]: https://www.ostechnix.com/wp-content/uploads/2018/06/whatportis.png
|
||||
[8]: https://www.ostechnix.com/wp-content/uploads/2018/06/whatportis-1.png
|
||||
[9]: https://github.com/ncrocfer/whatportis
|
Loading…
Reference in New Issue
Block a user