Method and command for finding the port number of a service in Linux

For some reason, you may often need to find the port name and port number. If so, you're lucky. Today, in this short tutorial, we will see the simplest and fastest way to find the service port number in the Linux system. There may be many ways to do this, but I only know the following three ways. Please read on.

Find the port number of the service in Linux

Method 1: use the grep command

To find the default port number of a specified service in Linux using the grep command, simply run:

$ grep <port> /etc/services

For example, to find the default port of the SSH service, simply run:

$ grep ssh /etc/services

It's that simple. This command should apply to most Linux distributions. The following is a sample output from my Arch Linux tester:

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 from the above output, the default port number of SSH service is 22.

Let's find the port number of the Apache Web server. To do this, the command is:

$ 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
[...]

What is the FTP port number? It's simple!

$ 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: use the getent command

As you can see, the above command displays all port names and numbers for the specified search terms "ssh", "http" and "ftp". This means that you will get fairly long output of all port names that match the given search term.

However, you can use the getent command to accurately output the results, as shown 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 you know the port number, you just need to replace the port name with a number:

$ getent services 80
http 80/tcp

To display all port names and port numbers, simply run:

$ getent services

Method 3: use Whatportis program

Whaportis is a simple Python script to find port names and port numbers. Unlike the above commands, this program outputs in a beautiful tabular form.

Ensure that the pip package manager is installed. If not, please refer to the following link.

  • How to manage Python packages using pip

After installing pip, run the following command to install the Whatportis program.

$ pip install whatportis

Now you can find the port associated with the service, as shown below.

$ whatportis ssh

$ whatportis ftp

$ whatportis http

Sample output from my CentOS 7 server:

Find the port number of the service in Linux

If you don't know the exact name of the service, use the – like flag to display the relevant results.

$ whatportis mysql --like

The above command helps you find the port associated with the service. You can also find the service associated with the port number, as shown below.

$ whatportis 993

You can even display the results in JSON format.

$ whatportis 993 --json

Posted by d3ad1ysp0rk on Sun, 21 Nov 2021 20:02:59 -0800