A step-by-step guide to check the port status of your Raspberry Pi device.
Ever wondered if a specific port on your Raspberry Pi is in use or not? In this article, we’ll show you how to check for open ports and their statuses using different methods. …
Updated October 11, 2023
Ever wondered if a specific port on your Raspberry Pi is in use or not? In this article, we’ll show you how to check for open ports and their statuses using different methods.
- Using the
netstat
command:
The netstat (network statistics) command is a powerful tool for checking the status of open ports on your Raspberry Pi. To use this method, follow these steps:
- Open a terminal window on your Raspberry Pi device.
- Type the following command and press Enter:
sudo netstat -tuln
- This will display a list of all active TCP and UDP ports along with their statuses. The output will 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:8080 0.0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0.0:* LISTEN -
tcp6 0 0 :::80 :::* LISTEN -
In the output above, you can see that ports 631 (for CUPS), 22 (for SSH), and 8080 are open on the device. You can also see which program is using a specific port by looking at the PID/Program name column.
- Using the
lsof
command:
The lsof (list open files) command allows you to check which processes have opened a file or a network port. To use this method, follow these steps:
- Open a terminal window on your Raspberry Pi device.
- Type the following command and press Enter:
sudo lsof -i
- This will display a list of all open files including the ones that correspond to network connections. The output will look something like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 1234 user 8u IPv4 29071 TCP *:631 (LISTEN)
apache2 50 root 4u IPv6 118969 TCP *:http (LISTEN)
In the output above, you can see that port 631 is open and being used by the python3 process with PID 1234.
- Using Python’s
socket
module:
You can also check if a specific port is available using Python’s built-in socket module. Here’s an example script that checks if port 8080 is open or not:
import socket
def check_port(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', port))
if result == 0:
print("Port {port} is open".format(port=port))
else:
print("Port {port} is closed".format(port=port))
sock.close()
check_port(8080)
Save the script as check_port.py
and run it using Python 3 on your Raspberry Pi: python3 check_port.py
. If port 8080 is open, you’ll see a message saying “Port 8080 is open”. Otherwise, you’ll get a message saying “Port 8080 is closed”.
In conclusion, there are several ways to check the port status of your Raspberry Pi device. Choose the method that best suits your needs and use it to manage and monitor your network connections effectively. Remember to always keep track of open ports and close them when they’re not in use to improve security.