Set up htpasswd protection for Prometheus

by Daniel Pham
Published: Updated:
This entry is part 3 of 14 in the series Install Prometheus and Grafana on Ubuntu 18

This article will guide you to set up htpasswd protection for Prometheus. You know, when accessing the Prometheus interface, there is no login/key window at all.

This is not safe, because you don’t want anyone to be able to see system monitor information. Right?

We have a solution which is htpasswd.

Install and setup htpasswd on Ubuntu 18

Continuing, I will still use the same Ubuntu 18.04 server that we used to install prometheus, grafana and nginx proxy.

First, because the web server is nginx and not apache, we need to install the apache ultis package.

apt install apache2-utils -y

Next, we create a folder containing the password file.

mkdir /etc/nginx/htpasswd

And then, we create a file containing the account used to authenticate for the prometheus domain.

htpasswd -c /etc/nginx/htpasswd/prometheus admin

The above command will create an account with the username admin, the password you will enter optionally during creation.

Set up htpasswd for nginx virtual host domain

Set up htpasswd protection for Prometheus
Set up htpasswd protection for Prometheus.

If you use nginx proxy for prometheus, now open the prometheus virtual host configuration file.

Find block / like below.

location / {

Then you add the block / lines below.

auth_basic              "Prometheus";
auth_basic_user_file    /etc/nginx/htpasswd/prometheus;

The block content will look like this.

location / {
    proxy_pass    ;
    include                 /etc/nginx/conf.d/resource/;
    auth_basic              "Prometheus";
    auth_basic_user_file    /etc/nginx/htpasswd/prometheus;

Then you test and reload nginx.

nginx -t
service nginx reload


So you have successfully set up htpasswd for prometheus. Now, every time you access the prometheus interface, you will have to enter the username and password you created. This makes it so that only you (or whoever holds the account) can access the prometheus window.

0 0 votes
Article Rating
Series Navigation«« Previous part: Set up nginx proxy for Prometheus and GrafanaNext part: Install Prometheus Blackbox exporter on Ubuntu 18 »»

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.