Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Databases

Install Elasticsearch 9 on Ubuntu with security left on

Install Elasticsearch 9 on Ubuntu 24.04 or 26.04 from Elastic's repository, keep TLS and passwords on, bind the API to localhost and use API keys.

CloudSat teamNovember 18, 20267 min read
A magnifying glass drawn in thin luminous lines hovering over a grid of small glowing tiles.
On this pageSize the server first

Elasticsearch is a search and analytics engine built on Apache Lucene: you send it JSON documents, it indexes every field, and it answers full-text queries and aggregations in milliseconds. The 9.x line is current, at 9.5.6 in Elastic's repository when this was written. Recent versions ship with security on: TLS, a built-in superuser and mandatory passwords. One default still deserves attention: the automatic setup opens the HTTP API on every network interface. This guide installs Elasticsearch from Elastic's APT repository, keeps the defaults that protect you, closes the one that does not, and gives applications their own API keys.

Size the server first

Elasticsearch leans on two kinds of memory. The JVM heap holds its working data structures, and the operating system's file cache keeps index files hot, which is where much of the query speed comes from. By default, Elasticsearch sizes its own heap from the machine's total memory, so on a dedicated server you can leave it alone. For a test node, 4 GB of RAM leaves room for a modest heap and that file cache; if Kibana or an application shares the machine, plan for more. Fast local disk matters as much as RAM. On CloudSat, a High Frequency VPS gives a single node fast cores and NVMe storage, and Elasticsearch is also available as a one-click app.

Four ideas to know before you index anything

  • Index. A named collection of JSON documents, roughly what a table is to a relational database.
  • Mapping. The type of each field: text for analyzed full text you search in, keyword for exact values you filter and aggregate on. Elasticsearch guesses a mapping from the first documents it sees, and a guessed type cannot be changed later without reindexing, so define mappings for data you care about.
  • Shards. Each index is split into primary shards, the unit Elasticsearch spreads across nodes.
  • Replicas. Copies of each primary shard, placed on other nodes. On a single node they have nowhere to go, which explains the yellow health status covered in the FAQ.

Step 1: add Elastic's repository and install

The package bundles its own Java runtime, so there is nothing else to install first:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | \
  sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/9.x/apt stable main" | \
  sudo tee /etc/apt/sources.list.d/elastic-9.x.list
sudo apt-get update && sudo apt-get install elasticsearch

The repository path pins the major version. Updates within 9.x arrive through apt; a future major version would need a new repository line and an upgrade plan.

Step 2: start it and set the superuser password

sudo systemctl daemon-reload
sudo systemctl enable --now elasticsearch.service
sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic

On first start, Elasticsearch generates certificates for its HTTP and transport layers and writes the matching settings into /etc/elasticsearch/elasticsearch.yml. It does not print the password of the elastic superuser; the reset tool sets a new random one and shows it once. Add -i if you prefer to type your own. Store it in a password manager, not in a shell script.

Check that the node answers over HTTPS. curl asks for the password, and --cacert trusts the certificate authority the node created for itself:

sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200

The JSON reply shows the cluster name and the version number. Resist the habit of adding -k to skip certificate checks; it trains everyone to ignore the one warning that matters.

Step 3: take the HTTP API off the public network

The security setup adds http.host: 0.0.0.0 to elasticsearch.yml, so the API listens on every interface. Passwords and TLS protect it, but a login endpoint on the internet still attracts brute-force attempts and exploit scanners. If only applications on this server use Elasticsearch, bind it to loopback:

grep -n '^http.host' /etc/elasticsearch/elasticsearch.yml
sudo sed -i 's/^http.host: 0.0.0.0/http.host: 127.0.0.1/' /etc/elasticsearch/elasticsearch.yml
sudo systemctl restart elasticsearch
sudo ss -tlnp | grep 9200

The last command should show 127.0.0.1:9200. The transport port, 9300, already listens on localhost in a single-node setup. If other servers do need the API, keep the wider binding and allow only them through the firewall, for example sudo ufw allow from 198.51.100.40 to any port 9200 proto tcp.

Step 4: set the heap only when you must

When another service shares the machine, fix the heap size so Elasticsearch does not claim memory the neighbor needs. Create /etc/elasticsearch/jvm.options.d/heap.options:

-Xms2g
-Xmx2g

Three rules apply: use the same value for both lines, give the heap no more than half of the RAM so the file cache keeps the rest, and stay below roughly 30 GB, where the JVM stops using compressed object pointers. Restart the service afterward. Elastic also recommends keeping the heap out of swap, so on servers with swap enabled, lower vm.swappiness or turn swap off.

Step 5: give applications API keys

Applications should never log in as elastic. Create an API key limited to the indices an application needs:

sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic \
  -X POST "https://localhost:9200/_security/api_key" \
  -H "Content-Type: application/json" -d '
{
  "name": "blog-search",
  "role_descriptors": {
    "blog_writer": {
      "indices": [
        { "names": ["blog-*"], "privileges": ["create_index", "write", "read"] }
      ]
    }
  }
}'

The reply contains an encoded value, which the application sends in an Authorization: ApiKey header. Copy the certificate authority somewhere your application user can read it, then index and search a first document with the key:

sudo install -m 644 /etc/elasticsearch/certs/http_ca.crt /usr/local/share/es-http-ca.crt
curl --cacert /usr/local/share/es-http-ca.crt -H "Authorization: ApiKey ENCODED_VALUE" \
  -H "Content-Type: application/json" -X POST "https://localhost:9200/blog-posts/_doc" \
  -d '{"title": "Hello search", "body": "Our first indexed document"}'
curl --cacert /usr/local/share/es-http-ca.crt -H "Authorization: ApiKey ENCODED_VALUE" \
  "https://localhost:9200/blog-posts/_search?q=title:hello&pretty"

Keys can be invalidated one at a time, so a leaked key costs you one application's access, not the whole cluster.

Optional: Kibana on the same server

Kibana enrolls with a short-lived token, and it listens on localhost:5601 by default:

sudo apt-get install kibana
sudo /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
sudo /usr/share/kibana/bin/kibana-setup --enrollment-token PASTE_TOKEN_HERE
sudo systemctl enable --now kibana

Reach it from your computer through ssh -N -L 5601:127.0.0.1:5601 admin@203.0.113.10, then open http://localhost:5601 and log in as elastic.

Day two: disk space and snapshots

Elasticsearch watches free disk space. At 85% used it stops placing new shards on the node, at 90% it tries to move them away, and at 95% it puts a read-only block on indices, which lifts once usage drops again. On a single node, alert well before 85%.

For backups, use snapshots instead of copying data files. Add path.repo: ["/var/backups/elasticsearch"] to elasticsearch.yml, create that directory owned by the elasticsearch user, restart, then register the repository and take a snapshot:

sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X PUT \
  "https://localhost:9200/_snapshot/local" -H "Content-Type: application/json" \
  -d '{"type": "fs", "settings": {"location": "/var/backups/elasticsearch"}}'
sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X PUT \
  "https://localhost:9200/_snapshot/local/snap-$(date +%F)?wait_for_completion=true"

Then copy that directory off the server, because a snapshot that sits on the same disk shares its fate.

FAQ

Where do I find the elastic password?

Nowhere: current versions do not print it during setup. Run elasticsearch-reset-password -u elastic to set a new one; it takes effect immediately.

Why does cluster health say yellow?

Yellow means every primary shard is assigned but some replicas are not. With the default of one replica per index and a single node, that is expected, because a replica never shares a node with its primary. For single-node indices, set replicas to zero:

sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X PUT \
  "https://localhost:9200/blog-posts/_settings" -H "Content-Type: application/json" \
  -d '{"index": {"number_of_replicas": 0}}'

Should I choose OpenSearch instead?

OpenSearch is the Apache-2.0 fork of Elasticsearch 7.10.2, governed by the OpenSearch Software Foundation and now on its 3.x line. The two have diverged in APIs and features, so pick based on the license you prefer and on which one your client libraries and tools support.

Is Elasticsearch open source?

In 2024, Elastic added the AGPLv3 as a license option for the Elasticsearch source code, alongside the SSPL and the Elastic License 2.0. The packages in Elastic's repository are distributed under the Elastic License 2.0, which allows free self-hosted use with some restrictions, mainly on offering it as a managed service.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.