Sunday, November 22, 2026
TR
LinuxOpen Source

How to Configure Remote Access for MongoDB on Ubuntu 20.04

How to Configure Remote Access for MongoDB on Ubuntu 20.04

Your MongoDB installation is not accessible from the internet. If you only intend to use MongoDB locally with applications running on the same server, this is the recommended and secure setting. However, if you want to be able to connect to your MongoDB server from a remote location, you must allow connections to the port the database listens on by adding a new UFW rule.

Begin by checking which port your MongoDB installation is listening on with the lsofcommand. This command normally returns a list of every open file on a system, but when combined with the -ioption, it lists only network-related files or data streams.

The following command will pipe the output produced by lsof -ito a grepcommand that searches for a string named mongo:

sudo lsof -i | grep mongo

This example output shows that MongoDB is listening for connections on its default port 27017:

Outputmongod    82221         mongodb   11u  IPv4 913411      0t0  TCP localhost:27017 (LISTEN)

In most cases, MongoDB should be accessible only from certain trusted locations, such as another server hosting an application or a local machine used to manage a remote MongoDB instance. One way to configure this is to run the following command on your MongoDB server, which opens access to MongoDB's default port while explicitly allowing the IP address of the other trusted machine.

Run the following command, making sure to replace trusted_machine_ipwith the IP address of the trusted remote computer you will use to access your MongoDB server.

Note : If you are not sure of the trusted machine's IP address, you can run the following curlcommand. This will access the icanhazip.comwebsite, which will return the IP address of the machine you run the command from:

curl -4 icanhazip.com

Also, if the output of the previous command showed that your MongoDB installation is listening on a non-default port, use that port number instead of 27017in this command:

sudo ufw allow from trusted_machine_ip to any port 27017

In the future, if you want to access MongoDB from another machine, you can run this command again with that machine's IP address in place of trusted_machine_ip.

You can verify the change in the firewall settings with ufw:

sudo ufw status

The output will show that traffic from the remote server to port 27017is now allowed:

OutputStatus: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
27017                      ALLOW       trusted_machine_ip
OpenSSH (v6)               ALLOW       Anywhere (v6)

Next, you will bind MongoDB to the server's public IP address so that you can access it from your remote machine.

Step 2 — Configuring a Public bindIP

At this point, even though the port is open, MongoDB is currently bound to 127.0.0.1, the local loopback network interface. This means that MongoDB can only accept connections originating from the server it is installed on.

To allow remote connections, you need to edit the MongoDB configuration file — /etc/mongod.conf— to additionally bind MongoDB to an IP address that is reachable by your trusted remote computer. That way, your MongoDB installation will be able to listen for connections to your MongoDB server from remote machines.

Open the MongoDB configuration file in your preferred text editor. The following example uses nano:

sudo nano /etc/mongod.conf

Find the network interfacessection and then the bindIpvalue:/etc/mongod.conf

. . .
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1
. . .

Append a comma followed by your MongoDB server's public IP address to this line:/etc/mongod.conf

. . .
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,mongodb_server_ip
. . .

Please note that this should be the IP address of the server where you installed MongoDB, not the IP address of your trusted remote machine.

Save and close the file. If you used nano, do so by pressing CTRL + X, Y, then ENTER.

Next, restart MongoDB for this change to take effect:

sudo systemctl restart mongod

After this, your MongoDB installation will be able to accept remote connections from the machines you have allowed to access port 27017. As a final step, you can test whether the trusted machine you allowed through the firewall in Step 1 can reach the MongoDB instance running on your server.

Step 3 — Testing Remote Connectivity

Now that you have configured your MongoDB installation to listen for connections originating from its publicly routable IP address, and allowed your remote machine access to Mongo's default port through your server's firewall, you can test that the remote machine can connect.

Note : As mentioned in the prerequisites section, this tutorial assumes that your remote machine is another server running Ubuntu 20.04. The procedure for enabling remote connections outlined in Steps 1 and 2 should work regardless of the operating system your remote machine runs, but the testing methods described in this Step will not work universally across operating systems.

One way to test that your trusted machine can connect to the MongoDB instance is to use the nccommand. nc, short for netcat, is a utility used to establish network connections with TCP or UDP. Because it allows you to specify both an IP address and a port number, it is useful for testing in situations like this.

First, log in to your trusted server using SSH:

ssh kerim@trusted_machine_ip

Then run the following nccommand, which includes the -zoption. This limits ncto only scanning for a daemon listening on the target server without sending it any data. Recall from the prerequisite installation tutorial that MongoDB runs as a service daemon, which makes this option useful for testing connectivity. The command also includes the voption, which increases the verbosity of the command and causes netcat to return some output it otherwise would not.

Run the following nccommand from your trusted remote server, making sure to replace mongodb_server_ipwith the IP address of the server where you installed MongoDB:

nc -zv mongodb_server_ip 27017

If the trusted server can reach the MongoDB daemon, the output will indicate that the connection succeeded:

OutputConnection to mongodb_server_ip 27017 port [tcp/*] succeeded!

Assuming you have a compatible version of the mongoShell installed on your remote server, you can at this point connect directly to the MongoDB instance installed on the host server.

One way to connect is to use a connection string URI like this:

mongo "mongodb://mongo_server_ip:27017"

or

mongo "mongodb://username@mongo_server_ip:27017"

The shell will automatically prompt you to enter the user's password.

Kerim Koç

Systems and infrastructure engineer. I write about Linux, open source, high availability and security.