Discover Open Ports on Your Raspberry Pi and Monitor Their Status

Learn how to check ports in Raspberry Pi with a step-by-step guide. Use built-in commands to scan for open ports and monitor their status, using netstat, lsof or ss. …


Updated September 1, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to check ports in Raspberry Pi with a step-by-step guide. Use built-in commands to scan for open ports and monitor their status, using netstat, lsof or ss.

Netstat Command

Netstat is a powerful command that displays information about network connections, routing tables, interface statistics, and more. To check open ports on your Raspberry Pi using netstat, follow these steps:

  1. Open the terminal on your Raspberry Pi.
  2. Type netstat -tuln and press Enter. This command will display all open TCP and UDP ports along with the associated programs. The output may look something like this:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:631           0.0.0.0.0:*               LISTEN      -                  
tcp        0      0 0.0.0.0:80             0.0.0.0:*               LISTEN      -                  
tcp        0      0 0.0.0.0:22             0.0.0.0:*               LISTEN      -                  
tcp6       0      0 :::22                 :::*                    LISTEN      -                  
udp        0      0 192.168.1.154.5353      0.0.0.0:*                           -                  
udp        0      0 0.0.0.0:67            0.0.0.0:*                            -                   
udp        0      0 0.0.0.0:5353          0.0.0.0:*                            -                   
udp6       0      0 :::49827              :::*                                -                    

In this example, we can see that ports 22 (SSH), 80 (HTTP) and 5353 (DNS) are open on the Raspberry Pi. The Local Address column shows which IP address and port number is being listened to, while the State column tells us whether it’s a listening (LISTEN) or established (ESTABLISHED) connection.

Lsof Command

lsof (short for “list open files”) is another command that can be used to check ports in Raspberry Pi. It displays information about files opened by processes, including network sockets. Here’s how:

  1. Open the terminal on your Raspberry Pi.
  2. Type lsof -i and press Enter. This command will list all open Internet connections and their associated processes. The output may look something like this:
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1035  root   3u  IPv4  20869       TCP *:ssh (LISTEN)
sshd    1035  root   4u  IPv6  20871       TCP *:ssh (LISTEN)
python  1234  user   4u  IPv4  337461      TCP localhost.localdomain:4567->localhost.localdomain:http (ESTABLISHED)

In this example, we can see that the SSH service is listening on ports 22 for both IPv4 and IPv6, and there’s an established connection from port 4567 to the HTTP service on localhost.

Ss Command

ss (short for “socket statistics”) is a newer command that provides a more flexible and efficient way to check ports in Raspberry Pi compared to netstat or lsof. It can display various types of sockets, including TCP, UDP, UNIX, and others. Here’s how:

  1. Open the terminal on your Raspberry Pi.
  2. Type ss -tuln and press Enter. This command will display all open TCP and UDP ports along with the associated programs in a similar format to netstat. The output may look something like this:
State      Recv-Q Send-Q Local Address:Port Peer Address:Port    Process
LISTEN     0      128        *:ssh                  *:*      users:(("sshd",pid=1035,fd=3)
LISTEN     0      128          *:http               *:*      users:(("apache2",pid=1467,fd=4)
ESTAB      0      0       192.168.1.10:4567  192.168.1.5:http  users:(("python",pid=1234,fd=4)

In this example, we can see that the SSH service is listening on ports 22 for both IPv4 and IPv6, and there’s an established connection from port 4567 to the HTTP service on localhost.

Conclusion

We have seen how to check open ports in Raspberry Pi using netstat, lsof, and ss commands. These commands can be useful for monitoring network activity and identifying potential security risks or performance issues. Remember that it’s always a good idea to keep your Raspberry Pi up-to-date with the latest software updates and close unused ports to improve security.