How to Check Listening / Open Ports with Netstat on Linux

By | May 1, 2023

Open Ports

Ports are opened on a system by various background services like http server, database server, smtp server etc.

If you are running a webserver and launch any such service then it will open a port so that other systems on the internet or local network can connect to it.

After you start a service on your system, then you need to check if the desired port is opened by the service or not.

If the port is not open, then the service probably failed to start or has stopped working due to some error.

Check open ports with Netstat

A very simple way to check for open ports on a linux server or system is to use the netstat commands. Though the netstat command is meant for checking network connections on a system, it can check and report open ports easily.

The syntax is very simple. You need to use the following options:

  • "-l" (for listening connections)
  • "-t" (for tcp connections)
  • "-p" (process name and id that opened the port)
  • "-n" (show port numbers instead of names)

Here is a quick example:

$ sudo netstat -ltpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:33060           0.0.0.0:*               LISTEN      1294/mysqld         
tcp        0      0 0.0.0.0:6600            0.0.0.0:*               LISTEN      1/init              
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1294/mysqld         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1283/apache2        
tcp        0      0 0.0.0.0:1716            0.0.0.0:*               LISTEN      1748/kdeconnectd    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      735/systemd-resolve 
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1471/cupsd

The above example clearly shows that apache has opened the http (80) port and mysql has opened 3306 port.

If you omit the "-n" option then the port names will be displayed instead.

$ sudo netstat -ltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:33060           0.0.0.0:*               LISTEN      1294/mysqld         
tcp        0      0 0.0.0.0:6600            0.0.0.0:*               LISTEN      1/init              
tcp        0      0 localhost:mysql         0.0.0.0:*               LISTEN      1294/mysqld         
tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN      1283/apache2        
tcp        0      0 0.0.0.0:1716            0.0.0.0:*               LISTEN      1748/kdeconnectd    
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN      735/systemd-resolve 
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN      1471/cupsd

Check specific open port

If you want to list out a specific open port then filter the output with grep. The following command filters out the port number containing 3306

$ sudo netstat -ltpn | grep "3306"
tcp        0      0 0.0.0.0:33060           0.0.0.0:*               LISTEN      1294/mysqld         
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1294/mysqld

Conclusion

To learn more about the netstat command, check out our tutorial here:

10 basic examples of Linux Netstat command

Hope that was helpful. If you have questions or feedback, let us know in the comments below.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

2 Comments

How to Check Listening / Open Ports with Netstat on Linux

Leave a Reply

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