Log inSign up

Run a secured Elasticsearch node on your VPS

Add the Elastic APT repository, run sudo apt install elasticsearch and copy the generated elastic password from the install output. Security is on from the first start: HTTP uses TLS with a local CA, so test with curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200. Then set a fixed heap of about half the RAM, bind HTTP to localhost and register a snapshot repository.

Topic: Databases · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13 · Updated 2026-12-03

How much memory does Elasticsearch need?

Give an Elasticsearch node at least 4 GB of RAM for real use, with roughly half as JVM heap and the rest left to the operating system file cache, which Lucene relies on for fast searches. A 2 GB server runs a small test index but has little headroom.

Elasticsearch suits full text search for a shop or documentation site, log search and analytics over JSON documents. For a single site search, it also helps to know the alternatives covered elsewhere in this library, such as Apache Solr and PostgreSQL full text search, which may already be enough.

Before installing, log in as a sudo user, update the system and turn swap off or keep it minimal, because a JVM that pages to disk stalls. Fast NVMe storage pays off when indexing.

How do you install Elasticsearch on Ubuntu or Debian?

Import the Elastic signing key, add the 9.x repository, install the package and note the password it prints.

sudo apt update
sudo apt install -y curl gnupg apt-transport-https
curl -fsSL 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 update
sudo apt install -y elasticsearch

The output contains a block titled Security autoconfiguration information with the password of the built-in elastic superuser. Save it in a password manager straight away. The package bundles its own Java runtime, so no separate JDK is needed. It does not start the service on its own:

sudo systemctl daemon-reload
sudo systemctl enable --now elasticsearch
sudo systemctl status elasticsearch --no-pager

The first start can take a minute while the node creates its keys and system indices.

What does security on by default mean?

From the first start, every request needs a username and password, the HTTP layer speaks TLS with a certificate from a CA the installer created, and traffic between nodes is encrypted too. Nothing answers anonymously.

sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200
sudo curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic "https://localhost:9200/_cluster/health?pretty"

A JSON reply with the version number and a cluster status of green (or yellow on a single node with replicas set) proves the install works. Plain http:// requests fail by design.

If you lose the elastic password, generate a new one:

sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic -i

The certificates live in /etc/elasticsearch/certs/. Clients on other machines need http_ca.crt, or the CA fingerprint, to trust the node.

How do you keep port 9200 off the internet?

Bind the HTTP layer to localhost. The installer writes http.host: 0.0.0.0 into /etc/elasticsearch/elasticsearch.yml so that other machines can enroll; for a single node, change it.

sudo nano /etc/elasticsearch/elasticsearch.yml
# change the line in the security block to:
http.host: 127.0.0.1
sudo systemctl restart elasticsearch
sudo ss -ltnp | grep -E ':9200|:9300'

Both ports should now show 127.0.0.1 only. Add a firewall rule as a second layer:

sudo ufw deny 9200/tcp
sudo ufw deny 9300/tcp

When an app on another server needs search, connect the servers with WireGuard and bind http.host to the tunnel address, or tunnel over SSH. TLS and passwords already protect the API, but a port that answers nobody is still the stronger default.

How do you size the JVM heap?

Set the minimum and maximum heap to the same value, about half the RAM, and stay below roughly 31 GB so the JVM keeps compressed object pointers. Put the setting in its own file under jvm.options.d:

echo -e '-Xms2g\n-Xmx2g' | sudo tee /etc/elasticsearch/jvm.options.d/heap.options
sudo systemctl restart elasticsearch
sudo curl -s --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic \
  "https://localhost:9200/_nodes/stats/jvm?filter_path=**.heap_max_in_bytes,**.heap_used_percent&pretty"

Without this file Elasticsearch picks a heap from the total RAM automatically, which is fine for a dedicated machine. Set it by hand when the server also runs a web stack, so the node does not claim memory other services need. On a 4 GB server shared with an app, 1.5 to 2 GB is reasonable; on an 8 GB dedicated node, 4 GB.

The package also raises the kernel limit on memory maps. Confirm it with sysctl vm.max_map_count; the value should be at least 262144.

How do you create a user for your application?

Create a role limited to the indices the app uses, then a user with that role. The elastic superuser belongs to administration only.

ES="https://localhost:9200"; CA=/etc/elasticsearch/certs/http_ca.crt
sudo curl -s --cacert $CA -u elastic -X PUT "$ES/_security/role/shop_rw" \
  -H 'Content-Type: application/json' \
  -d '{"indices":[{"names":["shop-*"],"privileges":["read","write","create_index","view_index_metadata"]}]}'
sudo curl -s --cacert $CA -u elastic -X PUT "$ES/_security/user/shop" \
  -H 'Content-Type: application/json' \
  -d '{"password":"App-Secret-Value-63","roles":["shop_rw"]}'

Test it by indexing and searching a document:

sudo curl -s --cacert $CA -u shop -X POST "$ES/shop-products/_doc?refresh=true" -H 'Content-Type: application/json' -d '{"name":"NVMe VPS","tags":["linux","crypto"]}'
sudo curl -s --cacert $CA -u shop "$ES/shop-products/_search?q=name:vps&pretty"

API keys, created with POST /_security/api_key, work well for services and can carry an expiry date.

How do you take snapshots and add nodes later?

Register a shared file system repository for snapshots, then snapshot on a schedule. List the directory in path.repo first:

sudo install -d -o elasticsearch -g elasticsearch /var/backups/es
echo 'path.repo: ["/var/backups/es"]' | sudo tee -a /etc/elasticsearch/elasticsearch.yml
sudo systemctl restart elasticsearch
sudo curl -s --cacert $CA -u elastic -X PUT "$ES/_snapshot/local" -H 'Content-Type: application/json' -d '{"type":"fs","settings":{"location":"/var/backups/es"}}'
sudo curl -s --cacert $CA -u elastic -X PUT "$ES/_snapshot/local/snap-$(date +%F)?wait_for_completion=true"

Snapshot lifecycle management (_slm/policy) can automate this daily. To grow into a cluster, create an enrollment token on the first node and pass it to each new node before its first start. The transport layer on port 9300 must then listen on a reachable address and be allowed between nodes only:

sudo /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node
# on the new node, before starting it
sudo /usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token TOKEN

How do you upgrade and troubleshoot Elasticsearch?

Upgrade within the 9.x line through apt after reading the release notes, one node at a time in a cluster, and read the cluster log first when something fails.

sudo apt update && sudo apt install --only-upgrade elasticsearch
sudo systemctl restart elasticsearch
sudo journalctl -u elasticsearch -n 50 --no-pager
sudo tail -n 80 /var/log/elasticsearch/elasticsearch.log

Common problems and fixes:

  • Service killed during start: the heap is too large for the RAM; lower -Xmx.
  • Cluster status yellow on one node: indices ask for replicas that have nowhere to go; set "number_of_replicas": 0 on them.
  • Writes blocked: the disk passed the flood-stage watermark (95 percent by default); free space and the block lifts automatically.
  • Certificate errors from clients: give them http_ca.crt or its fingerprint.

When the data outgrows the plan, deploy a larger one and restore a snapshot there; moving to a larger plan covers the server side.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4single node for site search2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A4-8larger indices with a 4 GB heap4 vCPU, 8 GB RAM, 180 GB NVMe$28.80/mo$86.40 for 3 months
A8-16log analytics or heavy indexing8 vCPU, 16 GB RAM, 350 GB NVMe$57.60/mo$57.60 for 1 month

Questions about Install Elasticsearch

Can I turn security off for testing?

You can set xpack.security.enabled: false, but then anyone who reaches the port can read and delete everything. Keep security on and use the generated password even for tests.

Why does curl say the certificate is untrusted?

The HTTP certificate comes from a CA the installer created on your server. Pass --cacert /etc/elasticsearch/certs/http_ca.crt so curl trusts it.

How much heap should I set?

About half the RAM, with the same value for -Xms and -Xmx, and never above about 31 GB. Leave the other half to the file system cache.

Do I need to install Java?

No. The package ships a bundled Java runtime that matches the release.

Can Kibana connect to this node?

Yes. Create a token with elasticsearch-create-enrollment-token -s kibana and paste it into Kibana on first start.

Which ports does Elasticsearch use?

Port 9200 for the HTTPS REST API and 9300 for traffic between nodes. Keep both closed to the public.