Linux users often find it challenging to list all the open ports in their system and the services responsible for managing those ports.
In this article, I'll guide you through the easiest way to find the list of all open ports in your Linux system, identify the services behind them, and stop those services.
List All the Open Ports in Linux
The easiest and simplest method that I find to list all the open ports in Linux is using the ss command, which comes built-in, making it easily accessible on a wide range of Linux systems.
Alternatively, you can use another popular command-line utility program called netstat
, which shares the same options as the ss command
but requires the net-tools
package to be installed on some Linux distributions as it is missing.
For that reason, the ss command
is the perfect choice for listing all the open ports on your Linux system by simply adding the -tulpn
option to it.
- ss -tulpn
Output:
The above list comprises all open ports in your Linux system; you only need to concentrate on ports whose state is set to LISTEN
.
Identify the Services Behind the Open Ports
Identifying the services or programs responsible for opening or managing the open port shown in the previous example requires root or sudo privileges.
For instance, executing the previously mentioned ss -tulpn
command with root privileges or by adding a sudo
before it will list all the ports along with their respective services.
- sudo ss -tulpn
Output:
Identify the Services Responsible for Managing the Particular Port
In the earlier example, you were provided with a comprehensive list of port numbers and the corresponding services responsible for their management.
However, you can use a grep command with the previous command to sort out the result by a particular port number, as shown.
- sudo ss -tulpn | grep :80
The above command will only list the services responsible for managing port 80.
Alternatively, use the lsof command
with the -i
option and the port number you are looking for to list all the background services handling it.
- sudo lsof -i :80
Output:
Kill the Services or Programs That Handle the Open Port
Once you have identified the services or programs managing the specific open port, you can gracefully halt them using their respective methods, which vary from program to program.
For instance, in our demonstration, we sought port 80 services, which were subsequently identified as being managed by NGINX, allowing you to gracefully stop them using the systemctl command.
- Stop the NGINX service handling port number 80
- sudo systemctl stop nginx
- Check the status of the NGINX service
- sudo systemctl status nginx
Output:
Alternatively, you can directly terminate all services or programs responsible for managing that specific port using the process ID or process name using the pkill command.
- sudo pkill nginx
Output: