Prometheus: Add authentication to Prometheus Node Exporter
This documentation is part of the Retrieving metrics from Linux guide. View the full guide here: How to retrieve Linux server metrics in Prometheus using Node Exporter.
👋 Welcome to the Stackhero documentation!
Stackhero delivers a fully managed Prometheus cloud platform designed for reliability and simplicity:
Alert Manageris built in, so you can route alerts directly toSlack,Mattermost,PagerDuty, and other popular destinations.- A dedicated email server lets you send unlimited email alerts without extra setup.
Blackboxis included, allowing you to probeHTTP,ICMP,TCP, and other protocols for comprehensive monitoring.- Configure your instance quickly using the online configuration file editor. There is no need to manage YAML by hand.
- Apply updates with a single click. Stackhero handles the upgrade process for you, minimizing downtime and manual intervention.
- High performance and strong security are built in, thanks to your own private, dedicated infrastructure.
Get up and running in about 5 minutes. Stackhero takes care of the setup so you can focus on monitoring, not maintenance. Try Prometheus cloud hosting on Stackhero to streamline your monitoring and alerting workflows.
By default, Node Exporter does not perform authentication, meaning anyone can retrieve the exposed metrics. To secure access, you can add basic authentication for a user.
Generating a random password
Generate a password using the following commands:
password=`openssl rand -base64 32`
passwordHashed=`echo ${password} | htpasswd -inBC 10 "" | tr -d ':'`
echo "Clear password to keep for Prometheus server: ${password}"
If you do not have the
htpasswdbinary, you can install it on Debian/Ubuntu usingapt-get install --no-install-recommends apache2-utils.
Keep the clear password secure as you will need it when configuring Prometheus.
Adding the user and restarting Node Exporter
Append the following lines to the configuration file to add a user called "prometheus" with the generated password hash:
sudo cat << EOF >> /etc/prometheus_node_exporter/configuration.yml
basic_auth_users:
prometheus: ${passwordHashed}
EOF
# Restart Node Exporter
sudo systemctl restart node_exporter
Finally, verify that authentication is working correctly:
- Running
curl http://localhost:9100/metricsshould return "Unauthorized". - Running
curl -u prometheus:${password} http://localhost:9100/metricsshould return the list of metrics.