Port 27017 is the default port for MongoDB. Drivers, mongosh and replica set members connect to it. It should not be reachable from the public internet.
- Port
- 27017
- Protocol
- TCP
- Service
- MongoDB
- Expose to internet?
- No
- Also used by
- MongoDB, Percona Server for MongoDB, FerretDB
Is port 27017 safe to open?
MongoDB does not enforce authentication unless security.authorization is enabled. Tens of thousands of exposed instances have been wiped and replaced with ransom notes, and many data leaks trace back to open MongoDB. Since 3.6 it binds to localhost by default, so a public instance is usually the result of setting bindIp: 0.0.0.0.
How to secure MongoDB
- Enable authentication:
security.authorization: enabledin mongod.conf, then create users. - Set
net.bindIpto 127.0.0.1 and private addresses only. - Use keyfile or x.509 authentication between replica set members.
- Enable TLS for connections across networks.
Check if port 27017 is open
Linux: what is listening on this port
sudo ss -tlpn | grep ':27017 'Windows (PowerShell): what is listening
Get-NetTCPConnection -LocalPort 27017 -State Listen | Select-Object LocalAddress, OwningProcess, @{n='Process';e={(Get-Process -Id $_.OwningProcess).Name}}From another machine: is it reachable
nc -zv HOST 27017
# or
nmap -p 27017 HOST
# Windows
Test-NetConnection HOST -Port 27017Test the MongoDB service itself
mongosh "mongodb://HOST:27017" --eval "db.runCommand({ ping: 1 })"Restrict port 27017 in the firewall
Replace 10.0.0.0/8 with the network or address that needs access. Docker publishes container ports through its own iptables rules, which bypass ufw; bind published ports to 127.0.0.1 (-p 127.0.0.1:27017:27017) instead of relying on the host firewall.
ufw: allow only your private network, deny everyone else
sudo ufw allow from 10.0.0.0/8 to any port 27017 proto tcp
sudo ufw deny 27017/tcpfirewalld: allow one source network only
sudo firewall-cmd --permanent --remove-port=27017/tcp
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" port port="27017" protocol="tcp" accept'
sudo firewall-cmd --reloadiptables
sudo iptables -A INPUT -p tcp --dport 27017 -s 10.0.0.0/8 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 27017 -j DROPWindows Firewall (PowerShell, as Administrator)
New-NetFirewallRule -DisplayName "Block 27017 from internet" -Direction Inbound -Protocol TCP -LocalPort 27017 -RemoteAddress Internet -Action BlockMonitor port 27017
Run a TCP port monitor against your public IP on port 27017: here, a failed connection is the healthy result, so you know the firewall is still doing its job. On Linux, the Statance agent also reports every listening port on the server. TCP port monitoring.
Related ports
FAQ
Is MongoDB port 27017 safe to expose?
No. Even with authentication enabled, a database should only be reachable from your application servers. Without authentication, an exposed MongoDB can be read or wiped by anyone.
What is port 27017 used for?
Port 27017 is the default port for MongoDB. Drivers, mongosh and replica set members connect to it. It is also used by MongoDB, Percona Server for MongoDB, FerretDB.
Is port 27017 TCP or UDP?
MongoDB uses TCP on port 27017.
How do I check if port 27017 is open?
On the server, run sudo ss -tulpn | grep ':27017 ' (Linux) or Get-NetTCPConnection -LocalPort 27017 (Windows) to see what is listening. From another machine, run nc -zv HOST 27017 or Test-NetConnection HOST -Port 27017.
Know the moment a port stops answering.
TCP, HTTP, DNS and SSL checks from six regions, plus server metrics from a one-line agent.