PortGate

Guides · Databases · 4 min

Is Your Database Open to the Internet? Checking Redis, Postgres and MongoDB

Scanners find open database ports within hours. How to test yours from the outside, and the settings that keep Redis, PostgreSQL and MongoDB private.

Automated scanners sweep the whole IPv4 internet for open database ports all day. A Redis or MongoDB instance that answers without a password can be found, emptied, ransomed or used to run code within hours of going online. The fix is usually one line. The hard part is knowing that the port is open, because everything works fine from inside the server.

Test from the outside, not from the server

ss -tulpn on the server tells you what is listening:

sudo ss -tulpn | grep -E ':(6379|5432|3306|27017|9200)\b'

A line showing 0.0.0.0:6379, *:6379 or [::]:6379 means it listens on every interface. Whether that is reachable depends on firewalls, so check from another machine:

nc -vz -w 3 your.server.ip 6379
nc -vz -w 3 your.server.ip 5432
nc -vz -w 3 your.server.ip 27017

"Connection refused" or a timeout is what you want. "Succeeded" or "open" means anyone can reach it.

Why Docker setups are the usual culprit

Two defaults combine badly:

  1. A port published as 6379:6379 in Docker Compose listens on all interfaces, and Docker's own iptables rules let it through even if UFW says deny. See how to stop Docker from bypassing UFW.
  2. The official Redis image turns protected mode off by default, so containers can reach each other. Redis then accepts connections from anywhere without a password.

The fix in docker-compose.yml is to publish to localhost only, or not publish at all if only other containers need the database:

services:
  redis:
    image: redis:7
    ports:
      - "127.0.0.1:6379:6379"   # host-only
  postgres:
    image: postgres:17
    # no ports: other services reach it as postgres:5432 on the Compose network

Apply it with docker compose up -d, then test again from outside.

Redis

Outside Docker, the package defaults are safe: bind 127.0.0.1 -::1 and protected mode on. Problems start when someone changes bind to 0.0.0.0 to "make it work" from another server. If it must be reachable over the network:

bind 127.0.0.1 10.0.0.5        # loopback plus the private network address
protected-mode yes
requirepass use-a-long-random-password
rename-command CONFIG ""

An open Redis is dangerous beyond data loss. Its CONFIG command can make Redis write files to disk, which attackers have used to plant SSH keys and cron jobs. Disabling CONFIG (or using Redis ACLs to deny it) removes that path.

PostgreSQL

Two files control access. In postgresql.conf:

listen_addresses = 'localhost'

In pg_hba.conf, allow only the addresses that need it, with password authentication:

host  all  all  127.0.0.1/32  scram-sha-256
host  all  all  10.0.0.0/24   scram-sha-256

Never use trust for anything but local sockets. The official Postgres Docker image listens on all interfaces inside the container, and setting POSTGRES_HOST_AUTH_METHOD=trust there allows connections with no password at all. Combined with a published port, that is an open database.

MongoDB

MongoDB has bound to localhost by default since version 3.6. If you changed bindIp, turn authorization on before anything else, in /etc/mongod.conf:

net:
  bindIp: 127.0.0.1,10.0.0.5
security:
  authorization: enabled

When you really need remote access

For a developer reaching the database from a laptop, use an SSH tunnel instead of opening the port:

ssh -N -L 5432:127.0.0.1:5432 [email protected]

Your local tools then connect to localhost:5432. For app servers talking to a database server, use the provider's private network and allow only those addresses in the firewall.

If it was already open

Assume the data was read. Check for unknown databases or keys (ransom notes are often stored as a key or collection called something like README or RECOVER), check for new SSH keys and cron jobs on the host, rotate the database password and anything stored in the database, and work out whether personal data was exposed. See finding cron and SSH key persistence.

How PortGate helps

The Server & Infra Guard lists every listening port on each report and flags database and cache ports open to the internet, including ports published by Docker that UFW does not see. The fix it gives matches the cause: a 127.0.0.1: port mapping or a DOCKER-USER rule for containers, and a firewall rule for services on the host.