How to Configure Iptables to Act as a Firewall for your MongoDB Server

Learn how to use the GNU/Linux bundled firewall — iptables — to protect your MongoDB server and only allow connections from your application servers.

Stampery Inc.
Mongoaudit — the mongoaudit guides
2 min readJan 26, 2017

--

First of all, you need to identify the public IP address of all your application servers.

You will also need to know which port your mongod listens at. You can log into the server where mongod is located and figure it out using the following command:

Then, create the/etc/mongo.firewall.rules file and make sure it contains the following lines:

Then save to disk and run the following command to apply the rules immediately as well as for persisting them after reboot:

If you usually run your app in your own computer for testing or development purposes, you may also want to whitelist your home or office IP.

--

--