Show used ports in Linux
Websites communicate on specific ports. For instance, behind a web address with HTTP, there’s always a request on port 80. Once HTTPS is used, the browser requests a webpage from the webserver on port 443. For this, a webserver is required. Widely used are [NGINX](https://www.nginx.com/) and [Apache](https://www.apache.org/).
Personally, I prefer NGINX because it can be used as both a webserver and a reverse proxy. Reverse proxy means that internally, the request can be forwarded to another website or to an internally running Apache or Docker container. Also, configuring an encrypted connection with NGINX is very straightforward, and it handles many requests without issue.
To check for occupied ports under Linux, there are several methods available. Here are three of the most common:
Checking Open Ports with lsof:
lsof can be installed on Debian/Ubuntu with the command:
sudo apt install lsof -y
One way to display occupied ports under Linux is to use lsof
(List Open Files). This tool can display not only open files but also network connections. Here’s how you can use it:
sudo lsof -i -P -n | grep LISTEN
This command displays a list of all open network connections that are listening (LISTEN
) on your system. Here’s a brief explanation of the options:
- -i
: Displays Internet network connections.
- -P
: Prevents the conversion of port numbers to service names.
- -n
: Displays numerical addresses (no name resolution).
- grep LISTEN
: Filters the output to display only connections waiting for connections (LISTEN).
Netstat Command:
The following command is required for the installation of Netstat under Debian/Ubuntu:
sudo apt install lsof -y
The netstat
command is a versatile tool for displaying various network information, including the list of currently occupied ports. Open the terminal and enter the following command:
netstat -tuln
This command displays a list of TCP (-t) and UDP (-u) ports along with their status (-l for listening ports, -n for numeric output instead of name resolution).
SS Command (Socket Statistics):
The following command is required for the installation of Socket Statistics (ss) under Debian/Ubuntu:
sudo apt install iproute2 -y
The Socket Statistics (ss
)command is a more modern alternative to netstat
and offers similar functionality but is more efficient. Enter the following command in the terminal:
ss -tuln
This command also displays a list of TCP (-t) and UDP (-u) ports along with their status (-l for listening ports, -n for numeric output instead of name resolution).
These methods provide additional ways to view occupied ports on your Linux system and give you insight into active network connections.