Find open ports on a Linux server with ss -tulpn
A service that listens on a public address can be reached from the internet unless a firewall drops the traffic. Software installed for a test, such as a database or a metrics exporter, may listen on every address of the server.
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 |
Output in a Debian 13 test container:
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))
For UDP, the state UNCONN 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 (as in 0.0.0.0%eth0:68) binds the socket to one interface.
A filter hides the loopback sockets (-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, a helper process that Docker starts for
each published port. The traffic itself goes through 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. The fix is in
Docker bypasses ufw.
Check from outside
ss shows what listens; the firewall decides what is reachable. Test from another machine, and scan only your
own servers:
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 the IPv6 address too. A firewall configured for IPv4 only leaves services open on IPv6.
Close what should not be public
Sort each service on the list into one of four groups:
- Public services (SSH, the web server, a VPN): keep them, and keep them updated.
- Used only by this server: 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. - Used by other servers in a private network: bind it to the private address and allow only those servers in the firewall.
- Not used: stop and disable it with
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 -
Vulnpocalypse 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 Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.
Vulnpocalypse