Open In App

Firewall Configuration - MongoDB

Last Updated : 27 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MongoDB is a widely used NoSQL database that offers flexibility and scalability. However, securing a MongoDB instance is crucial to prevent unauthorized access and potential cyber threats. One of the most effective ways to secure MongoDB is by configuring a firewall. This guide will explore the best practices and steps to configure a firewall for MongoDB to enhance security.

Why Firewall Configuration is Important for MongoDB

By default, MongoDB listens on port 27017, making it susceptible to unauthorized access if not secured properly. A firewall acts as a barrier between MongoDB and unauthorized users, ensuring that only trusted connections are allowed. Some key reasons for configuring a firewall include:

  • Preventing Unauthorized Access: Blocks unwanted connections from malicious users.
  • Enhancing Data Security: Protects sensitive information stored in the database.
  • Reducing Attack Surface: Limits exposure to the internet, minimizing the risk of cyberattacks.
  • Complying with Security Standards: Many regulatory standards mandate network-level security measures, including firewall protection.

Steps to Configure a Firewall for MongoDB

The configuration process varies depending on the operating system and firewall used. Below are the steps for commonly used firewall tools:

1. Configuring Firewall on Linux (UFW - Uncomplicated Firewall

UFW is a simple yet powerful firewall management tool for Linux-based systems such as Ubuntu.

Step 1: Check Firewall Status

Before making any changes, check if UFW is enabled:

sudo ufw status

If UFW is inactive, enable it using:

sudo ufw enable

Step 2: Allow MongoDB Connections from Trusted IPs

To secure MongoDB, allow access only from specific IP addresses. For example, to allow access from 192.168.1.100, run:

sudo ufw allow from 192.168.1.100 to any port 27017

To allow access from a range of IPs (e.g., 192.168.1.0/24):

sudo ufw allow from 192.168.1.0/24 to any port 27017

Step 3: Deny All Other Connections

To block all other connections to MongoDB, execute:

sudo ufw deny 27017

Step 4: Reload UFW to Apply Changes

sudo ufw reload

Step 5: Verify Firewall Rules

Check the rules applied using:

sudo ufw status verbose

2. Configuring Firewall on Windows (Windows Defender Firewall)

For Windows-based MongoDB installations, you can configure Windows Defender Firewall to allow only specific IPs.

Step 1: Open Windows Defender Firewall with Advanced Security

  • Open Control Panel > Windows Defender Firewall.
  • Click on Advanced Settings.

Step 2: Create a New Inbound Rule

  • In the left pane, select Inbound Rules.
  • Click New Rule... in the right panel.
  • Choose Port and click Next.
  • Select TCP and enter 27017 as the specific port.
  • Click Next, then select Allow the connection.
  • In the next window, select Domain, Private, and Public based on your network security needs.
  • Click Next, give the rule a name (e.g., "MongoDB Access"), and click Finish.

Step 3: Restrict Access to Specific IPs

  • To further secure MongoDB, create a custom rule to allow only trusted IPs:
  • Open the newly created MongoDB Access rule.
  • Go to the Scope tab.
  • Under Remote IP Address, select These IP addresses and add the allowed IPs.
  • Click OK to save the settings.

3. Configuring Firewall with iptables (For Advanced Users)

iptables is a more advanced firewall tool available on Linux.

Step 1: Allow Connections from Specific IPs

To allow access from 192.168.1.100:

sudo iptables -A INPUT -p tcp -s 192.168.1.100 --dport 27017 -j ACCEPT

Step 2: Block All Other Connections

sudo iptables -A INPUT -p tcp --dport 27017 -j DROP

Step 3: Save and Apply Rules

To make the changes persistent:

sudo iptables-save | sudo tee /etc/iptables/rules.v4

Best Practices for Securing MongoDB with Firewall

1. Bind MongoDB to Localhost

If MongoDB is used only on a local server, restrict access to localhost:

sudo nano /etc/mongod.conf

Modify the bindIp setting:

net:

  bindIp: 127.0.0.1

Restart MongoDB:

sudo systemctl restart mongod

2. Use VPN or SSH Tunneling for Remote Access

If remote access is required, use VPN or SSH tunneling instead of exposing MongoDB directly.

3. Regularly Monitor Firewall Logs

Check firewall logs for unauthorized access attempts:

sudo cat /var/log/ufw.log

4. Keep MongoDB and Firewall Software Updated

Regular updates help fix security vulnerabilities.

5. Implement Role-Based Access Control (RBAC)

Restrict database access based on user roles.

db.createUser({  user: "secureUser",  pwd: "StrongPassword123",  roles: [ { role: "readWrite", db: "yourDatabase" } ]})

Conclusion

Securing MongoDB with a firewall is an essential step in preventing unauthorized access and ensuring data safety. Whether using UFW, Windows Defender Firewall, or iptables, restricting access to trusted IPs and implementing best security practices will significantly enhance MongoDB’s security. By following the steps outlined in this guide, you can protect your database from potential threats and maintain a robust security posture.


Next Article
Article Tags :

Similar Reads