Skip to content

Instantly share code, notes, and snippets.

@royvandam
Last active February 17, 2026 15:40
Show Gist options
  • Select an option

  • Save royvandam/aed05a26e152daeece14bbd27017b104 to your computer and use it in GitHub Desktop.

Select an option

Save royvandam/aed05a26e152daeece14bbd27017b104 to your computer and use it in GitHub Desktop.
Automated Prometheus Node Exporter Installation as Systemd Service
#!/usr/bin/env bash
set -euo pipefail
USER="node_exporter"
BIN_PATH="/usr/local/bin/node_exporter"
SERVICE_PATH="/etc/systemd/system/node_exporter.service"
need_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Missing required command: $1"
exit 1
}
}
need_cmd curl
need_cmd tar
need_cmd uname
need_cmd mktemp
need_cmd sudo
# Detect architecture for Prometheus release naming
ARCH="$(uname -m)"
case "$ARCH" in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
armv7l) ARCH="armv7" ;;
*)
echo "Unsupported architecture: $ARCH"
exit 1
;;
esac
echo "Resolving latest Node Exporter version from GitHub..."
LATEST_TAG="$(
curl -fsSL "https://api.github.com/repos/prometheus/node_exporter/releases/latest" \
| sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' \
| head -n 1
)"
if [[ -z "${LATEST_TAG}" ]]; then
echo "Failed to determine latest release tag"
exit 1
fi
VERSION="${LATEST_TAG#v}"
TARBALL="node_exporter-${VERSION}.linux-${ARCH}.tar.gz"
URL="https://github.com/prometheus/node_exporter/releases/download/${LATEST_TAG}/${TARBALL}"
echo "Latest tag: ${LATEST_TAG}"
echo "Downloading: ${URL}"
TMP_DIR="$(mktemp -d)"
cleanup() { rm -rf "$TMP_DIR"; }
trap cleanup EXIT
cd "$TMP_DIR"
curl -fL -o "$TARBALL" "$URL"
echo "Extracting..."
tar xzf "$TARBALL"
EXTRACT_DIR="node_exporter-${VERSION}.linux-${ARCH}"
if [[ ! -f "${EXTRACT_DIR}/node_exporter" ]]; then
echo "Expected binary not found after extraction"
exit 1
fi
echo "Installing binary to ${BIN_PATH}..."
sudo install -m 0755 "${EXTRACT_DIR}/node_exporter" "$BIN_PATH"
echo "Creating service user..."
if ! id "$USER" >/dev/null 2>&1; then
sudo useradd --no-create-home --shell /bin/false "$USER"
fi
echo "Creating/Updating systemd service..."
sudo tee "$SERVICE_PATH" >/dev/null <<EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=$USER
Group=$USER
Type=simple
ExecStart=$BIN_PATH
[Install]
WantedBy=multi-user.target
EOF
echo "Reloading systemd..."
sudo systemctl daemon-reload
echo "Enabling service..."
sudo systemctl enable node_exporter
echo "Starting (or restarting) service..."
if sudo systemctl is-active --quiet node_exporter; then
sudo systemctl restart node_exporter
else
sudo systemctl start node_exporter
fi
echo "Done."
echo "Status: systemctl status node_exporter"
echo "Metrics: http://localhost:9100/metrics"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment