How to check TCPIP open ports for Linux.
One of the tasks of support and management that we can perform continually is to check the TCP ports opened in the system because in this way we can have precise control over which service is making use of this port.
A port indicates the end point of any network communication and is associated with a particular program or service of the operating system.
Next, we will see how we can know the open ports in Linux.
Check Open Ports in Linux
We can make use of three practical applications for it, these are:
1. Command netstat
This netstat command is frequently used to display information associated with the network. It is available in Windows and Linux.
For its installation we can execute the following commands:
sudo yum install net-tools (RedHat/ CentOS ) sudo apt install net-tools (Debian/ Ubuntu) sudo dnf install net-tools Fedora
We must know the port number to be analyzed, and we will use the grep command to display the information using the following syntax:
netstat -ltnp | grep -w ':port_number'
The result will be the following:
- l: Indicates to netstat that it only deploys the listening ports (Listening).
- t: Display only the tcp connections.
- n: indicates that you display numeric addresses.
- p: Allows to visualize the ID of the process.
- grep: Display the exact match.
2. Command lsof
Lsof is used for the purpose of deploying all open ports in Linux. For its installation we will use the following options:
sudo yum install lsof (RedHat / CentOS) sudo apt install lsof (Debian/ Ubuntu) sudo dnf install lsof (Fedora)
For checking open ports with lsof use the following syntax:
lsof -i :port
We can see in detail the data of the process, user, etc.
3. Fuser Command
Thanks to this command we can display the PID associated with the ports opened in Linux. For its installation we will use any of the following lines as the case may be:
sudo yum install psmisc ( Redhat / CentOS ) sudo apt install psmisc ( Debian / Ubuntu ) sudo dnf install psmisc ( Fedora 22 & higher )
The syntax for its use is the following:
To find the name of the process we will use the following syntax:
ps -p PID -o comm=
In this way we can know the open ports in Linux.