Home DevOpsHow to Set Up Prometheus and Grafana Monitoring for Linux Servers

How to Set Up Prometheus and Grafana Monitoring for Linux Servers

by Knowledge Base Linux
How to Set Up Prometheus and — How to Set Up Prometheus and Grafana Monitoring for Linux Servers

You cannot fix what you cannot see. When a server slows down at 3 a.m., the difference between guessing and knowing is a monitoring stack that recorded CPU, memory, disk, and network history all along. The most popular open-source combination for this job is Prometheus and Grafana monitoring: Prometheus collects and stores metrics, and Grafana turns them into dashboards and alerts. This guide installs both on Ubuntu and monitors the host itself. This article covers How to Set Up Prometheus and in detail.

How the Pieces Fit Together – How to Set Up Prometheus and

  • Prometheus scrapes HTTP endpoints called targets at an interval (15 seconds by default) and stores the samples in a time-series database. You query it with the PromQL language.
  • Node Exporter is a small agent that exposes Linux system metrics (CPU, RAM, disk, network) on port 9100 in a format Prometheus understands.
  • Grafana connects to Prometheus as a data source and renders dashboards. It can also send alerts to email, Slack, Telegram, and more.

Prerequisites – How to Set Up Prometheus and

  • An Ubuntu 22.04 or 24.04 server with sudo access.
  • Ports 9090 (Prometheus), 9100 (Node Exporter), and 3000 (Grafana) reachable from your workstation, ideally restricted by firewall to your IP.

Step 1: Install Prometheus

Create a dedicated system user and download the current release from the Prometheus download page. The commands below use version 3.1.0 as an example; adjust the version number to the latest release:

sudo useradd --no-create-home --shell /bin/false prometheus
wget https://github.com/prometheus/prometheus/releases/download/v3.1.0/prometheus-3.1.0.linux-amd64.tar.gz
tar xvf prometheus-3.1.0.linux-amd64.tar.gz
cd prometheus-3.1.0.linux-amd64
sudo cp prometheus promtool /usr/local/bin/
sudo mkdir -p /etc/prometheus /var/lib/prometheus
sudo cp -r consoles console_libraries /etc/prometheus/
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus

Create a minimal configuration file at /etc/prometheus/prometheus.yml:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

Then create the systemd service /etc/systemd/system/prometheus.service:

[Unit]
Description=Prometheus
After=network.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

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus

Open http://YOUR_SERVER_IP:9090 and check Status > Targets. This stack is the foundation of Prometheus and Grafana monitoring, and the built-in Prometheus UI is also where you will test PromQL queries such as rate(node_cpu_seconds_total{mode="idle"}[5m]) after Node Exporter joins.

Step 2: Install Node Exporter

Install Node Exporter the same way on every server you want to monitor (here, the same host):

sudo useradd --no-create-home --shell /bin/false node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvf node_exporter-1.8.2.linux-amd64.tar.gz
sudo cp node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

Create /etc/systemd/system/node_exporter.service with ExecStart=/usr/local/bin/node_exporter, the same [Unit]/[Install] pattern as above, then enable it. Finally, add a scrape job to prometheus.yml and restart Prometheus:

  - job_name: "node"
    static_configs:
      - targets: ["localhost:9100"]
Prometheus and Grafana monitoring - metrics collection and alerts illustration

Step 3: Install Grafana and Add the Data Source

Grafana publishes an APT repository:

sudo apt-get install -y apt-transport-https software-properties-common wget
sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt-get update && sudo apt-get install -y grafana
sudo systemctl enable --now grafana-server

Open http://YOUR_SERVER_IP:3000 and sign in with admin / admin, then set a new password. Under Connections > Data sources, add Prometheus with the URL http://localhost:9090 and click Save & test.

Step 4: Import a Ready-Made Dashboard

You do not need to build panels from scratch. Go to Dashboards > New > Import, enter dashboard ID 1860 (the popular “Node Exporter Full” dashboard), select your Prometheus data source, and import. Within seconds you have CPU, memory, disk, and network panels for the host. This moment is where Prometheus and Grafana monitoring starts paying for itself: the next incident becomes a matter of reading history instead of speculation.

Troubleshooting Common Problems

Target shows as DOWN in Prometheus

Verify the exporter is running (systemctl status node_exporter), that you can fetch curl http://localhost:9100/metrics locally, and that the target address in prometheus.yml matches reality. Prometheus also needs a restart or reload after configuration changes.

Grafana cannot connect to Prometheus

The data source URL is resolved from the Grafana server’s perspective. If Grafana runs on the same host, http://localhost:9090 is correct; otherwise use the Prometheus server’s reachable IP or DNS name.

Dashboards show “No data”

Usually a time-range or job filter mismatch. Widen the time window in the top-right corner and check that the dashboard’s job variable matches your scrape job name (node in this guide).

Alerting Done Right with Alertmanager

Dashboards inform; alerts wake people up, so they must earn their place. Prometheus hands alert evaluation to rules, and Alertmanager routes the notifications, grouping duplicates and respecting silences. Start with a small set of symptom-based alerts rather than dozens of cause-based ones:

groups:
  - name: server-alerts
    rules:
      - alert: InstanceDown
        expr: up == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Instance {{ $labels.instance }} is down"
      - alert: HighCpuUsage
        expr: 100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "CPU above 90% on {{ $labels.instance }}"

The for: clause is your friend: it prevents one brief spike from paging anyone. Route critical alerts to a channel someone actually watches, review alert quality monthly, and delete alerts that never lead to action. Alert fatigue is a monitoring failure, not a people failure.

Conclusion

You have built a complete Prometheus and Grafana monitoring stack: Prometheus scraping itself and Node Exporter, Grafana visualizing everything through a proven community dashboard. Extend it by installing Node Exporter on more servers, adding application exporters, and configuring alert rules. If you prefer an agent-based alternative for quick overviews, compare it with our Netdata monitoring guide, and go deeper with the Prometheus official documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.