Resources · Port reference · port 27017
Port 27017 (MongoDB): what it is and how to secure it
MongoDB on port 27017 must never be public. Open MongoDB servers are copied and wiped by automated ransom bots.
Check command
sudo ss -tlnp | grep 27017
Copied.
Port 27017 is MongoDB. Keep it on 127.0.0.1 or a private network, and turn authorization on. Public MongoDB servers without a password are found by bots, copied, wiped and left with a ransom note.
What port 27017 is for
MongoDB listens on 27017 for clients such as your app, mongosh and Compass. Ports 27018 and 27019 are used by shard and config servers in larger setups and need the same protection.
Why an open MongoDB is dangerous
Since version 3.6, MongoDB binds to localhost by default, but access control is still off until you enable it. When someone changes the bind address to reach the database remotely, or runs it in Docker, the result is often a database anyone can read and delete. Ransom bots connect, dump the data, drop the databases and create one called something like READ_ME_TO_RECOVER_YOUR_DATA.
Check yours
sudo ss -tlnp | grep 27017
grep -A3 -E "^net:|^security:" /etc/mongod.conf
docker ps --format '{{.Names}} {{.Ports}}' | grep 27017
bindIp: 127.0.0.1 and authorization: enabled is what you want. From another machine:
mongosh "mongodb://YOUR_SERVER_IP:27017" --eval "db.adminCommand({ listDatabases: 1 })"
A list of databases means it is public and open.
Fix it
Create an admin user first, while you can still connect locally:
use admin
db.createUser({ user: "admin", pwd: passwordPrompt(), roles: ["root"] })
Then set the bind address and enable authorization in /etc/mongod.conf:
net:
port: 27017
bindIp: 127.0.0.1
security:
authorization: enabled
sudo systemctl restart mongod
Give your app its own user with access to its own database only, not root.
For Docker, publish on loopback and set the root user:
services:
mongo:
image: mongo:7
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
ports:
- "127.0.0.1:27017:27017"
If you need remote access
Use an SSH tunnel for Compass or mongosh:
ssh -L 27017:127.0.0.1:27017 you@YOUR_SERVER_IP
For app servers elsewhere, bind to the private network address and allow only those servers.
Common mistakes
bindIp: 0.0.0.0to "make Compass work". Use a tunnel instead.- Enabling a user but not
authorization. Users exist, but nothing asks for them. - Docker publishing 27017. UFW does not filter Docker-published ports.
FAQ
My database was replaced with a ransom note. Should I pay? No. There is often no copy to return. Restore from backup, then fix the exposure before you bring it back.
Is MongoDB Atlas affected? Atlas requires users and an IP access list. Do not add 0.0.0.0/0 to that list.
How do I know if it opens again? PortGate's Server Guard alerts when a database port such as 27017 listens on a public address.