# Configure Elasticsearch and Kibana setup in ubuntu

Elasticsearch components are not included in Ubuntu's default package repositories. However, they can be installed via APT by adding Elastic’s official package source. To ensure security and prevent package spoofing, all packages are signed with a GPG key, allowing the package manager to verify their authenticity. To proceed with the installation, let's import the public GPG key and add the Elastic package source list.

```bash
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
```

<figure><img src="/files/dvlDn14utc3rRDco6UeU" alt=""><figcaption></figcaption></figure>

* <https://artifacts.elastic.co/GPG-KEY-elasticsearch>: Elasticsearch’s public **GPG key**, a cryptographic "signature" used to verify the authenticity of packages.
* **`--dearmor`**: Converts the GPG key from human-readable text to **binary format** because Debian’s `apt` expects keys in binary format for verification.

Next, let's add Elasticsearch Repository to APT Sources:

```bash
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
```

<figure><img src="/files/jsqiUNp6DsLsM0FeUYMU" alt=""><figcaption></figcaption></figure>

* Here I'm telling `apt` where to find Elasticsearch packages ([https://artifacts.elastic.co/packages/8.x/apt](https://artifacts.elastic.co/packages/8.x/apt\).)).
* The `[signed-by=...]` option ensures packages from this repository are verified using the GPG key.

Next, let's update our APT packages index with the new Elastic source:

```bash
sudo apt-get update
```

<figure><img src="/files/jrkGC1M0ruatrZuWCGSw" alt=""><figcaption></figcaption></figure>

Next, let's install the Elasticsearch Debian package.

```bash
sudo apt-get install elasticsearch
```

<figure><img src="/files/CDDGMOu2Ei0Z7EcFl8PW" alt=""><figcaption></figcaption></figure>

Next, we need to update the elasticsearch.yml with following network host and port configurations.

```bash
sudo nano /etc/elasticsearch/elasticsearch.yml
```

<figure><img src="/files/5masjR0P2ZcjIiU97HLN" alt=""><figcaption></figcaption></figure>

Now, let's enable Elasticsearch to start automatically on system boot.

<pre class="language-bash"><code class="lang-bash">sudo systemctl daemon-reload
<strong>sudo systemctl enable elasticsearch
</strong></code></pre>

<figure><img src="/files/VnpzD2amxrbUzy6OScie" alt=""><figcaption></figcaption></figure>

Next, let's start the elasticsearch Service:

```bash
sudo systemctl start elasticsearch
sudo systemctl status elasticsearch
```

<figure><img src="/files/7z8aUwsi4W2UEctMYjSG" alt=""><figcaption></figcaption></figure>

Now, we need to confirm that Elasticsearch is running correctly and is accessible via HTTPS on `localhost:9200`.

<figure><img src="/files/hlJ7Ux71dEnnSSK0PApT" alt=""><figcaption></figcaption></figure>

We can also confirm the service is up and accessible using this command:

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

<figure><img src="/files/4dQ8sfTfx0ZBVSYTlffB" alt=""><figcaption></figcaption></figure>

* The file `/etc/elasticsearch/certs/http_ca.crt` is the CA certificate generated during Elasticsearch installation.

Now, let's install and configure Kibana. It is part of the Elastic Stack, so it uses the same repository we added for Elasticsearch.

```bash
sudo apt-get install kibana
```

Now, we need to edit **`kibana.yml`**  file to determine how it connects to Elasticsearch and how it behaves.

```bash
sudo nano /etc/kibana/kibana.yml
```

<figure><img src="/files/6xg9hiU9LjKM7xH3CTqe" alt=""><figcaption></figcaption></figure>

* **`server.port: 5601`** : the port on which Kibana will run
* **`server.host: "0.0.0.0"`** : the IP address Kibana will bind to (Setting this to `0.0.0.0` allows Kibana to be accessed from other machines on the network.)
* **`elasticsearch.hosts: ["http://localhost:9200"]`** : the Elasticsearch instance Kibana will connect to

Next, let's enable Kibana to ensures it starts automatically when the system boots.

```bash
sudo systemctl enable kibana
```

Then, let's start the Kibana service:

```bash
sudo systemctl start kibana
```

<figure><img src="/files/LBrY31v8zhNLB9stokfR" alt=""><figcaption></figcaption></figure>

Now, let's make sure Kibana is running:

<figure><img src="/files/euTOz8Hrn8VXWuOc0MDK" alt=""><figcaption></figcaption></figure>

Now, we need to generate an **enrollment token** for Kibana and using it to securely connect Kibana to Elasticsearch.

```bash
sudo /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
```

<figure><img src="/files/vUOXG8wUHCWiRIwE9pbq" alt=""><figcaption></figcaption></figure>

Next, let's open Kibana, enter the copied token into the input field, and click **Configure Elastic** to proceed.

<figure><img src="/files/cvoO5iglSQj32srb075a" alt=""><figcaption></figcaption></figure>

After this Kibana prompted for Verification code.

<figure><img src="/files/J2MxvFSgCUmtPkkSftBW" alt=""><figcaption></figcaption></figure>

To generate Verification code , we need to navigate to Kibana installation directory and execute the following script.

```bash
sudo /usr/share/kibana/bin/kibana-verification-code
```

<figure><img src="/files/1G93k6WKj2z668Vg4xAm" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/6x9JhIr99XJukqQSqGcP" alt=""><figcaption></figcaption></figure>

Next, let's proceed with logging in using the provided username and password.

<figure><img src="/files/T7MsiMBH5n3LnAWKRwWw" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/tBjW01clQqMTIZBDTCnx" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://faresbltagy.gitbook.io/footprintinglabs/build-elk-lab/configure-elasticsearch-and-kibana-setup-in-ubuntu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
