how to list open ports in linux

How to Check if Port is Open or Closed in Linux

Sometimes you may need to check if a port is open or in use on your Linux system, before running a software on it. There are many easy ways to do this. Here are some commonly used commands to find out if a port is open or closed in Linux. You can use them on almost every Linux distribution since they come pre-installed.


How to Check if Port is Open or Closed in Linux

Here are some of the commands to find if a port is in use or not.


1. Using lsof command

You can use lsof command to view list of open files in Linux. Since ports are treated as files in Linux, you can also use lsof command to view open ports.

Here are some of the commands using lsof to view open ports.

$ sudo lsof -i -P -n
$ sudo lsof -i -P -n | grep LISTEN

It will show output as follows

sshd    8379     root    3u  IPv4 0xffff70000029e000      0t0  TCP 102.186.128.38:22 (LISTEN)
  • sshd – name of application
  • 8379 – PID of application
  • 102.186.128.38 – ip address to which the application is bound
  • 22 – port number in use


2. Using netstat

netstat is another command used to list open ports and running processes.

$ netstat -tulpn | grep LISTEN

In case netstat has been deprecated on your system, you can use ss command instead.

$ sudo ss -tulw
$ sudo ss -tulwn

Here is what the different options stand for:

-t : Display only TCP sockets on Linux
-u : Display only UDP sockets on Linux
-l : Display open sockets
-p : Display process name that opened sockets
-n : Don’t use DNS


3. nmap command

You may also use nmap command to list open ports.

$ sudo nmap -sTU -O localhost

In this article, we have covered 3 commands to easily view open ports in Linux.

Also read:

How to Determine Kernel Architecture in Linux
How to List All Cron Jobs for All Users
How to Check Crontab logs in Linux
How to Add Repository in Ubuntu/Debian Linux
How to Add Directory to PATH in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *