Find open ports on a Linux server with ss -tulpn
Every service that listens on a public address can be reached from the internet unless a firewall stops the traffic. Most surprises come from software that opened a port by default: a database installed for a quick test, a metrics exporter, an admin panel. One command lists them, and a few more tell you where each one comes from.
List the listening sockets
sudo ss -tulpn
| Option | Meaning |
|---|---|
-t, -u |
TCP and UDP sockets |
-l |
listening sockets only |
-p |
the process that owns the socket (needs root to see processes of other users) |
-n |
numeric addresses and ports, no name lookups |
Example output from a Debian 13 test container with a few listeners:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
udp UNCONN 0 0 0.0.0.0:5353 0.0.0.0:* users:(("nc",pid=2720,fd=3))
tcp LISTEN 0 1 0.0.0.0:8081 0.0.0.0:* users:(("nc",pid=2714,fd=3))
tcp LISTEN 0 1 127.0.0.1:5432 0.0.0.0:* users:(("nc",pid=2655,fd=3))
tcp LISTEN 0 1 [::1]:6380 [::]:* users:(("nc",pid=2718,fd=3))
UDP sockets show the state UNCONN; for UDP that is the listening state.
Read the local address
The column Local Address:Port tells who can connect:
| Local address | Reachable from |
|---|---|
127.0.0.1:5432, [::1]:5432 |
this server only |
10.0.0.5:5432 (a private address) |
the private network the address belongs to |
0.0.0.0:5432, *:5432, [::]:5432 |
every network the server is on, including the internet |
A suffix such as %eth0 (for example 0.0.0.0%eth0:68) means the socket is bound to one interface.
To hide the loopback sockets and keep the rest, give ss a filter (-H drops the header line):
sudo ss -Htulpn 'not ( src 127.0.0.0/8 or src [::1] )'
In the example above, this leaves the two sockets on 0.0.0.0.
Find out where a socket comes from
The users:(("name",pid=1234,fd=6)) part names the process. From the PID to the service and the package:
ps -o pid,user,cmd -p 1234
systemctl status 1234 # the systemd unit of the process
readlink /proc/1234/exe # the program file
dpkg -S /usr/sbin/nginx # Debian, Ubuntu: the package
rpm -qf /usr/sbin/nginx # RHEL, Rocky, Alma
apk info --who-owns /usr/sbin/nginx # Alpine
Docker containers
A port published by Docker (-p 8080:80) shows up as docker-proxy, the helper process that Docker starts for
each published port. The actual traffic goes through the NAT rules that Docker writes, and those rules bypass
ufw. List the published ports from Docker:
docker ps --format 'table {{.Names}}\t{{.Ports}}'
0.0.0.0:8080->80/tcp is public; 127.0.0.1:8080->80/tcp is local only. When the Docker daemon runs with
"userland-proxy": false, there is no docker-proxy process, and published ports do not appear in ss at
all. docker ps is then the only place to see them. See Docker bypasses ufw
for the fix.
Check from outside
ss shows what listens. A firewall decides what is reachable. Test from another machine, for your own servers
only:
nc -vz 203.0.113.10 5432
nmap -Pn -p 1-65535 203.0.113.10
nmap -6 -Pn -p 1-65535 2001:db8::10
Test IPv6 too. A firewall configured for IPv4 only leaves services open on the IPv6 address.
Close what should not be public
For each service on the list, one of these applies:
- It should be public (SSH, the web server, a VPN). Keep it, and keep it updated.
- Only this server uses it. Bind it to
127.0.0.1in its configuration (listen_addresses = 'localhost'in PostgreSQL,bind-address = 127.0.0.1in MySQL and MariaDB,bind 127.0.0.1 -::1in Redis) and restart it. For containers:-p 127.0.0.1:5432:5432. - Other servers in a private network use it. Bind it to the private address and allow only those servers in the firewall.
- Nothing uses it. Stop and disable it:
sudo systemctl disable --now <unit>, or on Alpinesudo rc-service <service> stop && sudo rc-update del <service>. Remove the package if you installed it for a test.
Then add a firewall with a default-deny policy, so that the next forgotten service stays closed: ufw, firewalld or nftables.
Keep a baseline
Save the list once and compare it later:
sudo ss -Htuln | awk '{print $1, $5}' | sort -u > ~/ports-baseline.txt
# later
sudo ss -Htuln | awk '{print $1, $5}' | sort -u | diff ~/ports-baseline.txt -
Patchbrief sends the list of public listeners with each daily report and shows what changed since the day before.
Sources
- ss(8) — options, filters,
-H - Docker: packet filtering and firewalls — published ports, NAT rules, ufw
- dockerd: daemon configuration —
userland-proxy - PostgreSQL: connection settings —
listen_addresses
Check your servers every day
The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.