Skip to content
STAGE33R

Guides · Server software · 7 steps · reviewed

MongoDB exposed to the internet: enable authentication, bind to localhost and close port 27017

A MongoDB with no authentication and a public address is one of the most common ways data is stolen and held for ransom. Securing it is three things: a user, authorization turned on, and a network address that is not public. Do them in this order so you do not lock yourself out.

  1. Check whether you are exposed

    See which address mongod listens on and whether anything outside can connect. 0.0.0.0 or a public IP on port 27017 is the problem.

    sudo ss -tlnp | grep 27017
    grep -nE 'bindIp|authorization' /etc/mongod.conf
  2. Back up first

    Take a dump so a mistake costs minutes, not data.

    mongodump --out /root/mongo-dump-$(date +%F)
  3. Create an administrator while authentication is still off

    Create the first user in the admin database with a strong generated password. Store it in a password manager, not in shell history.

    mongosh admin --eval 'db.createUser({user:"admin",pwd:passwordPrompt(),roles:[{role:"root",db:"admin"}]})'
  4. Turn on authorization and restrict the address

    In /etc/mongod.conf, bind to localhost (add a private IP only if another server of yours needs it) and enable authorization.

    net:
      port: 27017
      bindIp: 127.0.0.1
    security:
      authorization: enabled
  5. Restart and test the login

    After a restart, an unauthenticated command must fail and the admin login must work.

    sudo systemctl restart mongod
    mongosh --eval 'db.adminCommand({listDatabases:1})'   # must be refused
    mongosh -u admin --authenticationDatabase admin
  6. Firewall the port and verify from outside

    Even bound to a private address, allow the port only from the servers that need it. Check from another machine that it is closed.

    sudo ufw deny 27017/tcp
    nmap -Pn -p 27017 YOUR_SERVER_IP   # from another machine: closed or filtered
  7. Give applications their own limited users

    Create one user per application with readWrite on its own database only, and update the connection strings.

    mongosh -u admin --authenticationDatabase admin --eval 'db.getSiblingDB("appdb").createUser({user:"app",pwd:passwordPrompt(),roles:[{role:"readWrite",db:"appdb"}]})'

Common mistakes

  • Enabling authorization before any user exists: you lock yourself out (the localhost exception is limited).
  • Binding to 0.0.0.0 and relying only on a cloud security group.
  • Applications that connect without credentials break at the restart: update their connection strings in the same window.
  • Assuming it was never accessed: if it was open, check the logs and treat the data as exposed.

How to undo it

Set authorization to disabled and bindIp back to the previous value in /etc/mongod.conf, restart mongod, and restore from the dump if data changed. Do this only while the port stays firewalled.

Sources

Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.