How to audit open ports on a Linux server (ss, UFW and nmap from outside)
You cannot protect a port you do not know about. An audit has three parts: what listens, what the firewall allows, and what an outsider actually reaches. The three often disagree, and the difference is the risk.
List what listens and who owns it
The address tells you the exposure: 127.0.0.1 is local, 0.0.0.0 and :: are every interface.
sudo ss -ltnupName the program and its owner
For each unexpected port, find out what it is before deciding.
sudo ss -ltnp | grep ':PORT' ps -p PID -o pid,user,cmd systemctl status PID --no-pager | head -n 5Read the firewall rules
UFW does not show Docker's rules, so check iptables too.
sudo ufw status numbered sudo iptables -S DOCKER-USER sudo iptables -t nat -S DOCKER | headScan from outside, your own server only
This is the real picture. Run it from a different network.
nmap -Pn -p- --min-rate 1000 SERVER_IPDecide for each port: close, bind to localhost, or restrict
A service used only by the same server should listen on 127.0.0.1. A service for you or your team should be limited to those addresses.
sudo ufw deny PORT sudo ufw allow from YOUR_IP to any port PORT proto tcpRepeat after every change
Ports reappear after an update or a new container.
Common mistakes
- Trusting ufw status when containers publish ports.
- Scanning from the server itself: it only shows localhost behaviour.
- Closing a port an application needs without checking what uses it.
How to undo it
Delete the rule you added with sudo ufw delete NUMBER, or reopen the port limited to the addresses that need it.
Sources
Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.