Sub topic 15
Listing and testing ports from the command line
This chapter assumes you know what a port number identifies, the difference between TCP and UDP, and how to run commands in a Linux terminal. You will learn to answer two questions: which ports is this machine listening on, and can I reach a port on another machine?
Listing listening ports with ss
The ss tool reads socket information from the kernel. The flags below combine into one useful command.
# List listening TCP and UDP sockets with numeric ports and owning processes
ss -tulnp| Flag | Meaning |
|---|---|
| `-t` | Show TCP sockets |
| `-u` | Show UDP sockets |
| `-l` | Show only listening sockets |
| `-n` | Show numbers instead of service names |
| `-p` | Show the owning process (needs root to see other users' processes) |
A typical output line looks like this:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=812,fd=3))
tcp LISTEN 0 4096 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=990,fd=5))Read the Local Address column carefully. 0.0.0.0 (or *, or [::] for IPv6) means the service accepts connections on every interface. 127.0.0.1 means only the machine itself can connect. In the example, SSH is open to the network, while PostgreSQL is reachable only locally.
Testing a remote port with nc
The nc (netcat) tool tries to open a connection and reports the result.
# Test whether a TCP port on a host you own is reachable
nc -vz 192.0.2.10 22Here -v prints the result and -z closes the connection right after it opens, without sending data. Only scan hosts you own or are authorized to test.
The result tells you something specific:
- succeeded or open: the connection completed, so a service is listening and no firewall blocked the path.
- Connection refused: the host answered, but nothing is listening on that port. A reset came back quickly.
- Timed out: no answer arrived. A firewall is probably dropping packets, or the host is unreachable.
The difference between "refused" and "timed out" is the most useful clue when debugging.
Testing from Python
If nc is not installed, Python can do the same job. This script also shows the ephemeral client port the operating system chose.
import socket
with socket.create_connection(("example.com", 80), timeout=5) as s:
print("local :", s.getsockname())
print("remote:", s.getpeername())A failed attempt raises ConnectionRefusedError or TimeoutError, matching the two failure cases above.
Common mistakes
- Running
sswithout-n, which hides port numbers behind names and makes the output harder to compare with documentation. - Assuming a listening port is reachable. A service can listen on
0.0.0.0and still be blocked by a firewall. - Testing from the same machine and concluding the port is open to everyone. Test from a second host.
Exercise
From another machine, nc -vz 192.0.2.10 5432 prints "Connection refused", but ss -tlnp on 192.0.2.10 shows PostgreSQL listening on 127.0.0.1:5432. What explains this, and what would you check or change?
Show answer
PostgreSQL is bound only to the loopback address, so the kernel rejects connections that arrive on the network interface with a reset. Change the database's listen address setting (in PostgreSQL, listen_addresses) to the server's network address, restart it, and confirm ss shows the new local address. Then check that the firewall allows port 5432 only from the clients that need it.