> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Prometheus Installation systemd

> Guide to install and configure Prometheus as a systemd service including user setup, directories, binaries, unit file, and enabling at boot

Running Prometheus directly from the terminal (for example, `./prometheus`) launches it in the foreground. If you close that terminal, Prometheus stops. It also won't start automatically after a reboot. To run Prometheus like any other long‑running service — in the background and enabled at boot — create a dedicated systemd unit and a system user that owns the Prometheus files.

This guide walks through:

* creating a system user and directories
* installing the Prometheus binaries and configuration
* creating a systemd unit file
* starting and enabling the service

***

## 1. Create a Prometheus user

Create a system user that cannot log in interactively. This user will own and run the Prometheus process.

```bash theme={null}
sudo useradd --no-create-home --shell /bin/false prometheus
```

<Callout icon="lightbulb" color="#1CB2FE">
  Using `--no-create-home` and `/bin/false` prevents shell login for the prometheus user. This is a security best practice for service accounts.
</Callout>

***

## 2. Create directories and set permissions

Create directories for configuration and time series storage, then assign ownership to the `prometheus` user and group.

```bash theme={null}
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus

# Verify:
ls -ld /etc/prometheus /var/lib/prometheus
```

Table: directory purposes

| Path                  | Purpose                                                                                   |
| --------------------- | ----------------------------------------------------------------------------------------- |
| `/etc/prometheus`     | Prometheus configuration and consoles (`prometheus.yml`, `consoles`, `console_libraries`) |
| `/var/lib/prometheus` | TSDB (time series database) storage                                                       |

After you copy files (next steps), make sure the directories and files are owned by the prometheus user:

```bash theme={null}
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
```

***

## 3. Download and extract Prometheus

Download the Prometheus release and extract it.

```bash theme={null}
wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz
tar xzf prometheus-2.37.0.linux-amd64.tar.gz
cd prometheus-2.37.0.linux-amd64
```

***

## 4. Install binaries and copy supporting files

Copy the `prometheus` binary and the `promtool` CLI to `/usr/local/bin`, and copy consoles and config files into `/etc/prometheus`.

```bash theme={null}
# Move binaries
sudo cp prometheus /usr/local/bin/
sudo cp promtool /usr/local/bin/
sudo chmod 0755 /usr/local/bin/prometheus /usr/local/bin/promtool
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool

# Copy consoles and console libraries for the web UI
sudo cp -r consoles /etc/prometheus
sudo cp -r console_libraries /etc/prometheus
sudo chown -R prometheus:prometheus /etc/prometheus/consoles /etc/prometheus/console_libraries

# Copy the example config (rename to prometheus.yml)
sudo cp prometheus.yml /etc/prometheus/prometheus.yml
sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml
sudo chmod 0644 /etc/prometheus/prometheus.yml
```

<Callout icon="lightbulb" color="#1CB2FE">
  `consoles` and `console_libraries` provide optional local web UI templates. If you are not using these right away, copying them still ensures the web UI works later without additional steps.
</Callout>

***

## 5. Test running Prometheus as the prometheus user

You can test the exact command the systemd unit will run:

```bash theme={null}
sudo -u prometheus /usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus/ \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries
```

This runs Prometheus in the foreground (for testing). When satisfied, stop it with Ctrl+C and proceed to create the systemd service.

***

## 6. Create the systemd service unit

Create `/etc/systemd/system/prometheus.service` with the contents below. This unit ensures Prometheus starts after the network is available and runs under the `prometheus` user.

```ini theme={null}
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus/ \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
```

Explanation highlights:

* `Wants=network-online.target` and `After=network-online.target` ensure the network is up before Prometheus starts.
* `User` / `Group` set the service account; `Type=simple` is sufficient for Prometheus.
* `ExecStart` is the full command to run Prometheus with the correct paths.

You can create the file with:

```bash theme={null}
sudo vi /etc/systemd/system/prometheus.service
# (paste the unit file contents, save and exit)
```

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/g3ob3hoM5yRC7KZS/images/Prep-Course-Prometheus-Certified-Associate-PCA-Certification/Prometheus-Fundamentals/Prometheus-Installation-systemd/prometheus-installation-systemd-cli.jpg?fit=max&auto=format&n=g3ob3hoM5yRC7KZS&q=85&s=a2ac1c754d2a36968fb4576a0c6d9aa1" alt="The image shows a dark-themed command line interface titled &#x22;Prometheus Installation systemd,&#x22; with no visible content or commands displayed." width="1920" height="1080" data-path="images/Prep-Course-Prometheus-Certified-Associate-PCA-Certification/Prometheus-Fundamentals/Prometheus-Installation-systemd/prometheus-installation-systemd-cli.jpg" />
</Frame>

***

## 7. Reload systemd, start and enable Prometheus

After adding or changing unit files, reload systemd and start the service:

```bash theme={null}
sudo systemctl daemon-reload
sudo systemctl start prometheus
```

Check the status:

```bash theme={null}
sudo systemctl status prometheus
```

To ensure Prometheus starts automatically on boot:

```bash theme={null}
sudo systemctl enable prometheus
sudo systemctl is-enabled prometheus   # should print "enabled"
```

<Callout icon="warning" color="#FF6B6B">
  If `systemctl status prometheus` reports failures, inspect the journal for details: `sudo journalctl -u prometheus -b`. Common issues include incorrect file paths, file ownership, or an invalid `prometheus.yml`.
</Callout>

***

## 8. Troubleshooting tips

* Confirm `prometheus` binary location: `which prometheus` or `ls -l /usr/local/bin/prometheus`.
* Confirm config file syntax: `promtool check config /etc/prometheus/prometheus.yml`.
* Check logs: `sudo journalctl -u prometheus -f` to tail live logs.

***

## References

* Prometheus releases: [https://github.com/prometheus/prometheus/releases](https://github.com/prometheus/prometheus/releases)
* systemd documentation: [https://www.freedesktop.org/wiki/Software/systemd/](https://www.freedesktop.org/wiki/Software/systemd/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/prometheus-certified-associate-pca/module/e03e8702-ef6c-4402-b626-4437fc40b513/lesson/9f512f96-05d4-420d-b643-4ab94a9b5c20" />
</CardGroup>
