Regularly checking port availability can help identify any unexpected open ports, which could indicate unauthorized access or a security vulnerability. If a service or application isn’t working as expected, you might check if the required port is available to identify any conflicts or issues.
When troubleshooting network or application issues, checking port availability and associated processes can provide valuable insights into the root cause of the problem.
Port and process identification
To identify the process that is using the port use the lsof
command to find the process ID (PID) associated with the port.
Show all ports
Open the terminal and type:
sudo lsof -i -P -n
List open files (lsof
) that are currently being used by active processes, network connections, associated with internet connections (-i
), displaying ports (-P
) and avoiding hostname resolution (-n
). The -n
parameter speeds up the command by avoiding DNS lookups.
or
sudo lsof -i -P -n | grep LISTEN
grep LISTEN
searches for lines containing the word “LISTEN
” in the input it receives from the previous command. It filters the output to only show lines indicating processes that are listening for incoming connections.
Show a specific port
Syntax
sudo lsof -i:<port_number>
This will show you information about the process using the specified port.
Example
sudo lsof -i:9000