SaferComputing.com

Reference · Databases

MongoDB port 27017, Elasticsearch ports 9200 and 9300: check binding and sign-in

MongoDB listens on localhost but checks no passwords until security.authorization is enabled. Elasticsearch 8.0 and later turn security on at first start, but open the HTTP API on 0.0.0.0. Check bindIp, http.host and xpack.security.enabled.

Published
Updated
Last checked
Applies to
MongoDB 8.0 and later from MongoDB's packages, and Elasticsearch 8.0 and later, on Linux and in Docker
Technical specification
Target artifactMongoDB's net.bindIp and security.authorization in /etc/mongod.conf, for TCP port 27017; Elasticsearch's network.host, http.host, transport.host and xpack.security.enabled in /etc/elasticsearch/elasticsearch.yml, for the HTTP API on port 9200 and traffic between nodes on port 9300.
Default behaviourMongoDB binds to localhost, and MongoDB's package starts it with bindIp set to 127.0.0.1, but security.authorization defaults to disabled: any client that connects can do anything. Elasticsearch binds to loopback addresses by default; since 8.0 its first start turns security on, with TLS and a password for the elastic user, and the current documentation says that setup binds the HTTP API to 0.0.0.0 while transport stays on localhost.
Safe stateMongoDB with security.authorization: enabled and bindIp: 127.0.0.1, or a private address behind a firewall; Elasticsearch with xpack.security.enabled: true, port 9200 on 127.0.0.1 unless other machines need it, and port 9300 reachable only from the other nodes; a test from another device gets no reply, or a refusal for missing credentials.
Check on Linuxsudo ss -tlnp '( sport = :27017 or sport = :9200 or sport = :9300 )'
Check in MongoDBmongosh --eval 'db.adminCommand({ getCmdLineOpts: 1 }).parsed'
Check in Elasticsearchsudo grep -nE '^\s*(network\.host|http\.host|transport\.host|xpack\.security\.enabled)' /etc/elasticsearch/elasticsearch.yml
Check from another devicenc -vz -w 5 SERVER_IP 27017, then the same for ports 9200 and 9300
Fix for MongoDBcreate a user administrator, set security.authorization: enabled with bindIp: 127.0.0.1 in /etc/mongod.conf, then sudo systemctl restart mongod
Fix for Elasticsearchkeep xpack.security.enabled: true, set http.host: 127.0.0.1 when only this computer uses the API, then sudo systemctl restart elasticsearch.service

Read the result

PortProgramWhat answers there
27017MongoDBClient connections to a standalone mongod; shard members default to 27018, config servers to 27019
9200ElasticsearchThe HTTP REST API, from the default range 9200 to 9300
9300ElasticsearchTransport between nodes and to remote clusters, from the default range 9300 to 9400
  • 127.0.0.1 or [::1] before the port: this computer only. 0.0.0.0, [::] or *: every network interface.
  • In MongoDB's parsed output, net.bindIp lists the addresses; if there is no security entry with authorization: 'enabled' or a keyFile, access control is off. If the command is refused as unauthorized, access control is on: sign in first, as below.
  • From another device: if mongosh --host SERVER_IP --eval 'db.adminCommand({ getCmdLineOpts: 1 }).parsed' prints the settings, anyone who reaches the port can read and change every database.
  • From another device: if curl -sk --max-time 5 https://SERVER_IP:9200/, or http:// without TLS, returns the cluster's name and version, the API answers without credentials. A rejection for missing credentials means security is on, but the port is reachable.

MongoDB

MongoDB's documentation says mongod binds to localhost by default, and its Ubuntu guide says the package starts MongoDB with bindIp set to 127.0.0.1 in /etc/mongod.conf. Sign-in is separate: security.authorization defaults to disabled, where, in MongoDB's words, a user can access any database and perform any action. MongoDB's configuration reference says to secure the deployment before binding to a publicly accessible address, and at minimum to consider enabling authentication.

Turn on access control
  1. On the server, open mongosh and create a user administrator. This is MongoDB's example; passwordPrompt() asks for the password instead of showing it:
mongosh, on the server
use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(),
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)
  1. In /etc/mongod.conf, check the net section and add a security section:
Lines in /etc/mongod.conf
net:
  port: 27017
  bindIp: 127.0.0.1
security:
  authorization: enabled
  1. Restart, then sign in; -p comes last so that mongosh asks for the password:
Ubuntu, as a user with sudo
sudo systemctl restart mongod
mongosh --port 27017 --authenticationDatabase "admin" -u "myUserAdmin" -p

From then on every client, apps included, has to sign in and can do only what its roles allow. If access control is on before any user exists, MongoDB's localhost exception lets you create the first user administrator on the server itself.

Other machines must connect

Add the server's private address, as in bindIp: 127.0.0.1,10.0.0.5; MongoDB says to bind to the private or internal interface when there are several. Keep access control on and open port 27017 to the app servers alone; Guide 5, step 4 has the UFW rules.

Docker

MongoDB's Docker guide uses -p 27017:27017, which publishes the port on every host address and bypasses UFW (Docker published ports). Publish it on the loopback address:

Linux, as a user in the docker group
docker run --name mongodb -p 127.0.0.1:27017:27017 -d mongodb/mongodb-community-server:latest

Elasticsearch

By default Elasticsearch binds only to loopback addresses: network.host defaults to _local_. In Elasticsearch 8.0 and later, the first start sets up security: TLS certificates for the HTTP and transport layers, a password for the elastic superuser and a Kibana enrollment token. The current documentation says this setup adds http.host: 0.0.0.0 to elasticsearch.yml, so the HTTP API listens on every interface, behind TLS and sign-in, while transport stays on localhost. Elastic warns never to expose an unprotected node to the public internet, where anyone could download, modify or delete the data.

xpack.security.enabled defaults to true; false disables the security features, which Elastic says is not recommended. The automatic setup is skipped when, for example, xpack.security.enabled already has a value, TLS settings exist or the data folder is not empty; check such nodes yourself.

Check Elasticsearch
Ubuntu, as a user with sudo
sudo ss -tlnp '( sport = :9200 or sport = :9300 )'
sudo grep -nE '^\s*(network\.host|http\.host|transport\.host|xpack\.security\.enabled)' /etc/elasticsearch/elasticsearch.yml
sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200

ELASTIC_PASSWORD holds the elastic user's password. The Debian package does not show it at the first start: set a new one with sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic.

Fix: API on 127.0.0.1, security on
  1. In /etc/elasticsearch/elasticsearch.yml, if only this computer uses the API, change the line the setup added:
Line in /etc/elasticsearch/elasticsearch.yml
http.host: 127.0.0.1
  1. Leave network.host and transport.host unset on a single node, so transport stays on localhost. Run sudo systemctl restart elasticsearch.service and check ss again.
  2. If the file says xpack.security.enabled: false, Elastic's minimal security steps are: set it to true on every node, add discovery.type: single-node on a one-node cluster, start the node, then set the elastic password with elasticsearch-reset-password. Elastic says this minimal setup is not sufficient for production mode clusters.
Docker

Elastic's single-node command publishes -p 9200:9200 on every host address, past UFW, and prints the elastic password and a Kibana enrollment token on the first start only. Write -p 127.0.0.1:9200:9200 when only this computer needs the API.

Troubleshooting

A server does not start after the change
  • MongoDB: sudo systemctl status mongod and the log in /var/log/mongodb/ give the reason; check the indentation of the new lines.
  • Elasticsearch: binding to a non-local address turns some warnings into fatal errors. The log in /var/log/elasticsearch/ names them; fix them, or go back to 127.0.0.1.

Sources