How to check ports in linux

In order to check ports in linux, we can use the next tools

LSOF

Installation
 
1. Execute the next command at the console
​[abernal@sandbox ~]$ sudo dnf install lsof

Basic usage

1. To list the open ports and the process that owns them, execute the next command

[abernal@sandbox ~]$ sudo lsof -i

2. To show which process is bound to a given port (sample port : 22)

[abernal@sandbox ~]$ sudo lsof -i :22

3. To check which binary is being executed by a given PID (Sample PID: 308)

[abernal@sandbox ~]$ sudo ls -l /proc/308/exe

 

NETSTAT

Installation

1. By default netstat is within the operative system however if it is not, execute the next command

​[abernal@sandbox ~]$ sudo dnf install netstat

Usage

1. To list the open ports and the process that owns them, execute the next command

​[abernal@sandbox ~]$ sudo netstat -tulpn | less

2. To check more information about a given "process name" or user executing a given PID from the netstat command (in this case, we can use : mysql)

[abernal@sandbox ~]$ whatis mysql

​3. To check the working directory of a process (sample PID: 308)

​[abernal@sandbox ~]$ pwdx 308