Posted by

Audit Open Ports on a Linux Server with ss: Find Listening Services, Bind to Localhost and Close What You Don't Need

Every service that listens on a network port is a door into your server. A firewall helps, but the safest door is the one that does not exist. Before you write firewall rules, you should know exactly which programs are listening, on which addresses, and why. This guide shows how to do that audit with ss, the standard socket tool on modern Linux, and how to close or lock down anything you do not need.

The commands below were written for Ubuntu and Debian servers, but ss and systemctl work the same way on most systemd-based distributions.

Step 1: List every listening port

ss is part of the iproute2 package, which is installed by default on Ubuntu and Debian. It replaces the older netstat command from net-tools, which is no longer installed by default on many systems.

sudo ss -tulpn

Each letter has a job:

OptionMeaning
-tShow TCP sockets
-uShow UDP sockets
-lShow only listening sockets (services waiting for connections)
-pShow the process that owns each socket
-nShow numeric addresses and ports instead of resolving names

Run it with sudo. Without root, ss still lists the sockets, but the process column is empty for programs owned by other users, which is exactly the information you need.

A typical result on a small web server looks like this:

Netid State  Recv-Q Send-Q Local Address:Port  Peer Address:Port Process
udp   UNCONN 0      0      127.0.0.53%lo:53         0.0.0.0:*     users:(("systemd-resolve",pid=601,fd=13))
tcp   LISTEN 0      4096   127.0.0.53%lo:53         0.0.0.0:*     users:(("systemd-resolve",pid=601,fd=14))
tcp   LISTEN 0      128          0.0.0.0:22         0.0.0.0:*     users:(("sshd",pid=812,fd=3))
tcp   LISTEN 0      511          0.0.0.0:443        0.0.0.0:*     users:(("nginx",pid=903,fd=7))
tcp   LISTEN 0      151        127.0.0.1:3306       0.0.0.0:*     users:(("mysqld",pid=1044,fd=23))
tcp   LISTEN 0      511          0.0.0.0:6379       0.0.0.0:*     users:(("redis-server",pid=1120,fd=6))
tcp   LISTEN 0      128             [::]:22            [::]:*     users:(("sshd",pid=812,fd=4))

This is example output. Your process IDs, queue sizes and services will be different.

Step 2: Read the Local Address column

The most important column is Local Address:Port. The address tells you who can reach the service:

Local addressWhat it meansReachable from the network?
0.0.0.0All IPv4 addresses on the serverYes, unless a firewall blocks it
[::] or *All IPv6 addresses, and often IPv4 tooYes, unless a firewall blocks it
127.0.0.1 or [::1]Loopback onlyNo, only programs on the same server
127.0.0.53%loThe systemd-resolved local DNS stub on UbuntuNo, loopback only
A specific IP, for example 10.0.0.5Only that one interface addressOnly from networks that can reach that address

In the example above, SSH and Nginx listen on all addresses, which is expected for a public web server. MySQL listens on 127.0.0.1, so only local programs can use it. Redis listens on 0.0.0.0:6379, which means it accepts connections from any network that can reach the server. That is the line to fix.

A quick rule: a database, cache, queue or admin panel should almost never listen on 0.0.0.0 or [::] unless another server really needs to connect to it.

Step 3: Find out what each process is

Sometimes the process name in ss is not enough. Use the PID from the users:(...) part to learn more.

Show the full command line and owner:

ps -o pid,user,cmd -p 1120

Find the systemd unit that started it. systemctl status accepts a PID and shows the unit that contains that process:

systemctl status 1120

Find which package installed the program (Debian and Ubuntu):

dpkg -S "$(readlink -f /proc/1120/exe)"

To check a single port, add a filter instead of reading the full list:

sudo ss -tlnp 'sport = :6379'
sudo ss -tlnp '( sport = :80 or sport = :443 )'

Now sort every listening service into one of three groups:

  1. Needed and public, such as SSH, HTTP and HTTPS. Keep them, and protect them with your firewall and hardening.
  2. Needed but local only, such as a database used by an app on the same server. Bind it to localhost (Step 4).
  3. Not needed, such as a leftover test service or a package you no longer use. Stop and disable it (Step 5).

Step 4: Bind internal services to localhost

If a service is only used by programs on the same server, tell it to listen on the loopback address. Then it cannot be reached from the network even if a firewall rule is wrong. Always restart the service and run ss again after a change.

MySQL and MariaDB

The setting is bind-address in the [mysqld] section. On Ubuntu, the MySQL package keeps it in /etc/mysql/mysql.conf.d/mysqld.cnf; on MariaDB, look in /etc/mysql/mariadb.conf.d/50-server.cnf. Packaged configs usually already use localhost, so if you see 0.0.0.0:3306, someone changed it.

[mysqld]
bind-address = 127.0.0.1
sudo systemctl restart mysql      # or: sudo systemctl restart mariadb
sudo ss -tlnp 'sport = :3306'

PostgreSQL

The setting is listen_addresses in postgresql.conf (on Debian and Ubuntu, under /etc/postgresql/<version>/main/). The default is localhost. A value of '*' means all addresses.

listen_addresses = 'localhost'
sudo systemctl restart postgresql
sudo ss -tlnp 'sport = :5432'

Redis

The setting is bind in /etc/redis/redis.conf. The default config binds to loopback only. Keep protected-mode yes as well.

bind 127.0.0.1 -::1
protected-mode yes
sudo systemctl restart redis-server
sudo ss -tlnp 'sport = :6379'

If an app on another server really needs the database, bind it to a private network address (for example a VPC or VPN address) instead of 0.0.0.0, and allow only that client's IP in the firewall.

Your own apps

Node.js, Python and other app servers behind Nginx should usually listen on 127.0.0.1, not on all addresses. For example, app.listen(3000, '127.0.0.1') in Node.js, or gunicorn --bind 127.0.0.1:8000 for Gunicorn. Nginx then proxies public traffic to that local port.

Step 5: Stop and disable services you do not need

When you find a service you do not use, stop it now and keep it from starting at boot with one command:

sudo systemctl disable --now example.service

Some services are socket-activated. systemd itself holds the port open and starts the program only when a connection arrives. In that case ss may show systemd as the process, and disabling only the .service unit is not enough. List socket units and disable the matching one too:

systemctl list-units --type=socket
sudo systemctl disable --now example.socket

If another package keeps starting a service you never want, you can block it completely with sudo systemctl mask example.service. Undo that later with unmask.

If you are sure you no longer need the software at all, removing the package is cleaner than leaving it disabled:

sudo apt remove example-package

Run sudo ss -tulpn again and confirm the port is gone.

A special case: Docker published ports

When you publish a container port with -p 8080:80, Docker listens on all host addresses by default. On many hosts you will see this in ss as a docker-proxy process. Docker also adds its own iptables rules, so traffic to published ports can bypass UFW rules, as explained in our UFW guide.

Two points matter for your audit:

  • If the container should only be reached through Nginx on the same host, publish it on loopback: -p 127.0.0.1:8080:80, or in Compose, "127.0.0.1:8080:80".
  • If the Docker daemon runs with "userland-proxy": false, there is no docker-proxy process, so a published port may not appear in ss at all even though it is reachable. On Docker hosts, also check docker ps for the PORTS column.

Step 6: Confirm from outside the server

ss shows what the server is listening on. It does not show what the internet can actually reach after firewalls, cloud security groups and NAT. Check that from a different machine.

Only scan servers you own or have written permission to test. From another computer:

nmap -Pn -p- your.server.ip

-p- scans all 65,535 TCP ports and -Pn skips the ping check, which many servers block. The result should list only the ports you decided to keep public, for example 22, 80 and 443. A UDP scan (sudo nmap -sU) is much slower and less precise, so use it only when you need it.

For a quick check of a single port, nc -zv your.server.ip 6379 from another machine should now fail or time out.

Step 7: Make the audit a habit

New packages, containers and app updates can open new ports without warning. Save a known-good list and compare against it later:

sudo ss -Htulpn | awk '{print $1, $5}' | sort -u > ~/ports-baseline.txt

# Later:
sudo ss -Htulpn | awk '{print $1, $5}' | sort -u | diff ~/ports-baseline.txt -

-H removes the header line. The awk part keeps only the protocol and the local address and port, so changing process IDs do not create noise. Any line that diff prints is a port that was added or removed since your baseline. You can run this check from a systemd timer and review its output in the journal.

Common mistakes

  • Running ss without sudo. You lose the process names for system services.
  • Trusting the firewall alone. One wrong rule, a disabled firewall or a Docker bypass can expose a database that should have been bound to localhost.
  • Forgetting IPv6. A service on [::] may be reachable over IPv6 even if your IPv4 firewall rules look correct. Check both.
  • Disabling SSH by accident. Never stop or rebind sshd on a remote server unless you have console access or a second session open.
  • Disabling a service but not its socket. Socket-activated services come back on the next connection.

Quick checklist

  1. Run sudo ss -tulpn.
  2. Mark every port as public, local-only or not needed.
  3. Bind databases, caches and app servers to 127.0.0.1.
  4. Disable unused services and their sockets with systemctl disable --now.
  5. Check Docker published ports separately.
  6. Confirm from another machine with nmap.
  7. Save a baseline and compare it regularly.

Sources