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.
| Target artifact | MongoDB'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 behaviour | MongoDB 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 state | MongoDB 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 Linux | sudo ss -tlnp '( sport = :27017 or sport = :9200 or sport = :9300 )' |
| Check in MongoDB | mongosh --eval 'db.adminCommand({ getCmdLineOpts: 1 }).parsed' |
| Check in Elasticsearch | sudo grep -nE '^\s*(network\.host|http\.host|transport\.host|xpack\.security\.enabled)' /etc/elasticsearch/elasticsearch.yml |
| Check from another device | nc -vz -w 5 SERVER_IP 27017, then the same for ports 9200 and 9300 |
| Fix for MongoDB | create a user administrator, set security.authorization: enabled with bindIp: 127.0.0.1 in /etc/mongod.conf, then sudo systemctl restart mongod |
| Fix for Elasticsearch | keep 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
| Port | Program | What answers there |
|---|---|---|
27017 | MongoDB | Client connections to a standalone mongod; shard members default to 27018, config servers to 27019 |
9200 | Elasticsearch | The HTTP REST API, from the default range 9200 to 9300 |
9300 | Elasticsearch | Transport between nodes and to remote clusters, from the default range 9300 to 9400 |
127.0.0.1or[::1]before the port: this computer only.0.0.0.0,[::]or*: every network interface.- In MongoDB's
parsedoutput,net.bindIplists the addresses; if there is nosecurityentry withauthorization: 'enabled'or akeyFile, 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/, orhttp://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
- On the server, open
mongoshand create a user administrator. This is MongoDB's example;passwordPrompt()asks for the password instead of showing it:
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: passwordPrompt(),
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" }
]
}
)- In
/etc/mongod.conf, check thenetsection and add asecuritysection:
net:
port: 27017
bindIp: 127.0.0.1
security:
authorization: enabled- Restart, then sign in;
-pcomes last so thatmongoshasks for the password:
sudo systemctl restart mongod
mongosh --port 27017 --authenticationDatabase "admin" -u "myUserAdmin" -pFrom 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:
docker run --name mongodb -p 127.0.0.1:27017:27017 -d mongodb/mongodb-community-server:latestElasticsearch
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
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:9200ELASTIC_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
- In
/etc/elasticsearch/elasticsearch.yml, if only this computer uses the API, change the line the setup added:
http.host: 127.0.0.1- Leave
network.hostandtransport.hostunset on a single node, so transport stays onlocalhost. Runsudo systemctl restart elasticsearch.serviceand checkssagain. - If the file says
xpack.security.enabled: false, Elastic's minimal security steps are: set it totrueon every node, adddiscovery.type: single-nodeon a one-node cluster, start the node, then set theelasticpassword withelasticsearch-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 mongodand 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 to127.0.0.1.
Related guides and lookups
Sources
- MongoDB: IP binding
- MongoDB: configuration file options
- MongoDB: use SCRAM to authenticate clients
- MongoDB 8.0: install on Ubuntu
- MongoDB 8.0: install with Docker
- MongoDB: getCmdLineOpts
- MongoDB Shell: options
- Elastic: networking settings
- Elastic: automatic security setup
- Elastic: minimal security setup
- Elastic: security settings
- Elastic: install with a Debian package
- Elastic: single-node cluster in Docker
- Elastic: get cluster info
- Docker: port publishing