Services listen on public addresses
- Check
net.public_listeners- Severity
- info
- Runs
- on the server, in the daily run of the agent
- Looks at
- TCP and UDP sockets on 0.0.0.0, :: or a public address, with the process name
What the check looks at
The TCP and UDP sockets that listen on 0.0.0.0 or :: (every address of the server) or on a public address,
with the port and the name of the process; loopback and private addresses are left out. The check warns with
severity info, so that you review the list.
Risk
A socket on a public address is reachable from the internet unless a firewall drops the traffic. SSH, HTTP and HTTPS are meant to be. Databases, caches, message queues, metrics endpoints and admin panels usually are not, and scanners such as Shodan and Censys index open database and Redis ports.
How to fix it
List the sockets (-p shows the process and needs root) and read the column Local Address:Port:
sudo ss -tulpn
| 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 |
0.0.0.0:5432, *:5432, [::]:5432 |
every network the server is on, including the internet |
For each service that should not be public, pick one fix:
- Bind it to loopback in its configuration and restart it (the settings of common databases are in the table below).
- Bind it to the private address when other servers in the same private network need it, and allow only their addresses in the firewall.
- For a Docker container, publish the port on loopback with
-p 127.0.0.1:5432:5432(or"127.0.0.1:5432:5432"in Compose). ufw rules do not filter a port that Docker publishes on0.0.0.0; see Docker bypasses ufw. - Stop the service if nothing uses it:
sudo systemctl disable --now <service>(Debian, Ubuntu, RHEL, Rocky, Alma) orsudo rc-update del <service> && sudo rc-service <service> stop(Alpine).
| Service | Setting for loopback only |
|---|---|
| PostgreSQL | listen_addresses = 'localhost' in postgresql.conf |
| MySQL and MariaDB | bind-address = 127.0.0.1 |
| Redis | bind 127.0.0.1 -::1 |
A firewall (see the firewall guide) still drops the traffic when an
update or a configuration change puts a service back on 0.0.0.0.
How to check the fix
sudo ss -tlpn 'sport = :5432'
The local address must be 127.0.0.1 or the private address. From a machine outside the network,
nc -vz <server-ip> 5432 must time out or be refused.
When a warning is fine
For services that are meant to be public: sshd on port 22, the web server on 80 and 443, a VPN such as
WireGuard (often UDP 51820), a mail server, and a DHCP client on UDP port 68 on a server that gets its address
by DHCP.
Sources
- ss(8) (options and filters)
- PostgreSQL: listen_addresses
- Redis: security, network binding
- Docker: packet filtering and firewalls
- Censys: Databases. EXPOSED! (Redis) (2022-09-19: 350,675 Redis services reachable from the internet, 39,405 of them without authentication)
- Shodan blog: It's the Data, Stupid! (2015-07-18: Shodan crawls the internet for database software; nearly 30,000 MongoDB instances without authentication)