Is your database open to the internet? A 5-minute port check for any Linux VPS
Most servers that get broken into aren't hit with anything clever. Someone ran a scanner across the internet, found a database that answered, and walked in. Redis, MongoDB, Elasticsearch and Postgres instances get found
Most servers that get broken into aren't hit with anything clever. Someone ran a scanner across the internet, found a database that answered, and walked in. Redis, MongoDB, Elasticsearch and Postgres instances get found this way every day, usually within hours of going online.
The good news is that checking your own server takes about five minutes. Here's how I do it.
1. List everything that is listening
On the server, run:
sudo ss -tlnp
You'll get one line per listening TCP socket, with the address it is bound to and the process that owns it. The column to look at is Local Address:Port:
| You see | It means |
|---|---|
127.0.0.1:6379 or [::1]:6379
|
Only this server can connect. Good. |
10.x.x.x, 172.16-31.x.x, 192.168.x.x
|
A private network. Usually fine. |
0.0.0.0:6379, [::]:6379 or *:6379
|
Every interface, including the public one. |
Anything on 0.0.0.0 or [::] that isn't meant for the public (in other words, anything other than SSH, 80 and 443 on most web servers) is worth a second look.
If you also run UDP services, add -u: sudo ss -tulnp.
2. Watch out for Docker
If you use Docker, you'll often see docker-proxy in that list. A Compose file like this:
services:
redis:
image: redis:7
ports:
- "6379:6379"
publishes Redis on every interface of the host. And on most Linux setups Docker writes its own iptables rules, which are evaluated before UFW's, so ufw deny 6379 does nothing to stop it. This catches a lot of people who did everything else right.
The fix is to bind the published port to localhost:
ports:
- "127.0.0.1:6379:6379"
Better still, if only your other containers need Redis, remove ports: entirely and let them reach it by service name on the Compose network.
3. Check from the outside
What the server thinks is listening and what the internet can actually reach aren't always the same thing (cloud firewalls, security groups, NAT). So test from a different machine:
nmap -Pn -p 22,80,443,3306,5432,6379,9200,11434,27017 YOUR.SERVER.IP
Or, for a single port without nmap:
nc -zv YOUR.SERVER.IP 6379
open from outside means anyone can try it. filtered or closed is what you want for anything that isn't a public service.
4. Fix the common ones
Most databases already default to localhost these days. They end up public because a tutorial or a Docker run command changed that. Here's where to look:
-
Redis (6379): in
redis.conf, keepbind 127.0.0.1 -::1andprotected-mode yes, and set a password withrequirepass. An open Redis can be used to write files to disk, which is a well-known route from "database" to "shell on your server". Full walkthrough: Port 6379 Redis guide. -
PostgreSQL (5432):
listen_addresses = 'localhost'inpostgresql.conf, and no0.0.0.0/0lines inpg_hba.conf. Port 5432 guide. -
MySQL / MariaDB (3306):
bind-address = 127.0.0.1in the server config. Port 3306 guide. -
MongoDB (27017):
net.bindIp: 127.0.0.1inmongod.conf, and turn on authorization. Open MongoDB servers are routinely copied and wiped by ransom bots. Port 27017 guide. -
Elasticsearch (9200):
network.host: 127.0.0.1, with security enabled. Port 9200 guide.
After any change, restart the service and run sudo ss -tlnp again to confirm the address moved to 127.0.0.1.
5. Need remote access? Use a tunnel, not a public port
If you need to reach a database from your laptop, you don't need to open it to the world. An SSH tunnel does the job:
ssh -L 5432:127.0.0.1:5432 [email protected]
Then connect your client to localhost:5432. When you close the SSH session, the tunnel closes too. For a team, a private network such as WireGuard or Tailscale is the next step up.
6. Make it a habit, not a one-off
The trouble with ports is that they change. Someone adds a container, a package update rewrites a config, a teammate copies a docker run line from a blog post. The check you did today says nothing about next month.
A few low-effort ways to keep on top of it:
- Run
sudo ss -tlnpafter every deploy that touches Docker or system packages. - Lint Compose files before they ship. I made a free Docker Compose security linter that flags ports published on all interfaces, privileged containers and mounted Docker sockets.
- Keep a short list of the ports each server is supposed to have open, and compare against it.
Full disclosure: I build PortGate, which automates that last step. A small agent reports the server's public ports every five minutes, compares them with a baseline you approve and alerts you, with the fix command, when something new appears. It's free for one server. But everything above works fine by hand, and it's worth doing today either way.
If you've been caught out by one of these, I'd like to hear which one in the comments.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.