How to Find Out all the Open Ports in Linux?

How To Find Out All The Open Ports In Linux

In this tutorial, we will learn how to find out all the open ports in Linux.

There are different commands to know which ports are opened such as nmap, netstat, and lsof. But do we know what ports are? Some people might think that it is the gateway to enter the system. There are a lot of myth concepts regarding port numbers. Let’s discuss this in detail.

What is Port number?

There are many services running in the system such as httpd, Hadoop service and so on. How will the network client identify the particular service from the bundle of services running on the system? Whenever we start the web server, instead of creating unique names for each service, a port number is assigned where the service listens for packets. Let’s look at some common ports.

  • Port number of HTTP: 80
  • Port number of SMTP: 25
  • Port number of FTP: 21
  • Port number of DNS: 53

The user can use his own personal port also. The port is internally stored in 2 bytes i.e. 2^16 = 65536. The range of the port number is from 0 to 65535. This is divided into different categories:

  • 0 – 1023: The ports in this range are called well-known ports or system ports.
  • 1024 – 49151: The ports in this range are called registered ports or user ports.
  • 49152-65535: The ports in this range are called the Dynamic ports and Private ports.

I hope the concept is clear. Let’s understand how to find out list of the open ports in Linux.

List all the services and their ports

If you want to know the list of all the services with their ports, it can be done using cat command. The file named services in /etc/ directory consists of the name and ports of all the services. It will also display the protocols related to the service. Let’s have a look at command below:

cat /etc/services
Services 1
List of services

You can see that it has displayed the list of services with their port and protocols.

Find out all the open ports in Linux using netstat command

Earlier, we described how to list all the services. If the user wants to know the find out all the open ports in Linux, we can use the netstat command. It will list all the network connections in the system. Apart from this, it also prints the routing tables, interfaces statistics, and multicast memberships.

For more detail information, check the Linux Official Documentation.

The user will use -lntu option with the netstat command where,

  • -l: It will print only listening sockets
  • -n: It will display the port number
  • -t: It enables the listing of TCP ports.
  • -u: It enables the listing of UDP ports.

Let’s have a look at the command below:

netstat -lntu
Netstat 1
List of currently running services

Display the running ports using ss command

Earlier we used netstat command to display the running ports. Here, we are using the ss command to do so. You might be thinking that what is the difference between both of them.

The ss command is the substitute of the netstat command. Although it is similar to the netstat command, it displays more information. It is used to track TCP connections and sockets. The ss is included in the iproute2 package while netstat uses /proc/net/tcp to gather the information about the system network.

For more detail information, check the Linux Official Documentation of ss command.

Let’s have a look at the following command:

ss -lntu
Ss
Ss

The significance of -lntu is described above in the netstat command.

Find out all the open ports in Linux using lsof command

Earlier, we discussed how to list the ports using netstat command and ss command. Both were used to display the running services. Here, we will discuss about lsof command. The LSOF stands for List open files. This command is used to list all the open files and processes in the system. It is used in Unix-Linux operating system.

For more details, check the Linux Official Documentation of lsof command.

Let’s have a look at the command below:

sudo lsof -i -P -n | grep LISTEN
Lsof
Lsof

The options associated with this command are:

  • -i: It will list all the running process of specific ports
  • -P: It will convert the port numbers to port names for network files.
  • -n: It will convert the network names to hostnames for network files.

Grep command is used to search for the particular word. As here it will print the ports which are in LISTEN state.

Find out all the open ports using the ps Command

Earlier, we used lsof command to print the ports. Here we will discuss the ps command. The ps command is used to display the information related to the running process in the system. Here, we will use ps -aux command which will print all those processes that are owned by the user. Remember, using “ps aux” will print the warning.

For more details, check the Linux Official Documentation of ps command.

Let’s have a look at the following command:

ps -aux
Ps Command
Ps Command

Conclusion

We have discussed how to find out all the open ports in Linux using ps, netstat, lsof and ss command. Since most of the commands have usage apart from what’s described here, go ahead use the man pages by using the man command in Linux. We hope the concept is cleared. If you face any issues, do let us know in the comments.