Skip to content
STAGE33R

Guides · Hardening · 6 steps · reviewed

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.

  1. 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 -ltnup
  2. Name 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 5
  3. Read 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 | head
  4. Scan from outside, your own server only

    This is the real picture. Run it from a different network.

    nmap -Pn -p- --min-rate 1000 SERVER_IP
  5. Decide 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 tcp
  6. Repeat 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.