Starting with MongoDB 3.6, MongoDB binaries, mongod and mongos, bind to localhost by default. From MongoDB versions 2.6 to 3.4, only the binaries from the official MongoDB RPM (Red Hat, CentOS, Fedora Linux, and derivatives) and DEB (Debian, Ubuntu, and derivatives) packages would bind to localhost by default.
Starting Nmap 6.49BETA3 ( https://nmap.org ) at 2019-01-19 14:17 CST Nmap scan report for 113.207.35.149 Host is up (0.042s latency). PORT STATE SERVICE 27017/tcp open mongod
Nmap done: 1 IP address (1 host up) scanned in 14.34 seconds
可知,27017端口是”open”的,这就意味着我们可以远程访问MongoDB数据库。
配置UFW防火墙
Ubuntu上默认的防火墙软件是
UFW
,配置起来非常简单。默认情况下,ufw并没有激活:
sudo ufw status Status: inactive
执行以下命令,即可配置ufw规则,并启动防火墙:
sudo ufw default deny incoming // 默认禁止访问本机所有端口 sudo ufw default allow outgoing // 允许本机访问外部网络 sudo ufw allow 22/tcp // 允许SSH登陆 sudo ufw allow from 192.168.59.100 to any port 27017 // 仅允许局域网内IP为192.168.59.100的服务器访问mongodb sudo ufw enable
Starting Nmap 6.49BETA3 ( https://nmap.org ) at 2019-01-19 14:40 CST Nmap scan report for 113.207.35.149 Host is up (0.053s latency). PORT STATE SERVICE 27017/tcp filtered mongod
Nmap done: 1 IP address (1 host up) scanned in 13.68 seconds