为prometheus配置访问密码
基于basic-auth
通过prometheus-web-config.yml配置
Prometheus 支持通过web.config.file参数加载包含认证配置的文件,实现basic-auth。
- 创建密码文件(使用htpasswd)
安装htpasswd(属于apache2-utils或httpd-tools):
# Debian/Ubuntu
sudo apt-get install apache2-utils
# CentOS/RHEL
sudo yum install httpd-tools
生成密码文件(例如/etc/prometheus/web.yml):
# 创建密码文件(用户名为prometheus,会提示输入密码)
htpasswd -nBC 10 "" | tr -d ':\n' > /etc/prometheus/htpasswd
# 注意:上述命令会生成加密后的密码,后续需手动填入配置文件
- 创建prometheus-web-config.yml
# 路径示例:/etc/prometheus/web.yml
basic_auth_users:
prometheus: $2y$10$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # 替换为上一步生成的加密密码
说明:basic_auth_users下的键为用户名,值为htpasswd生成的加密密码(去掉开头的用户名和冒号)。
3. 启动 Prometheus 时加载认证配置
命令行启动:
./prometheus \
--config.file=./prometheus.yml \
--web.enable-lifecycle \
--storage.tsdb.path=./prometheus_data \
--web.console.libraries=./console_libraries \
--web.console.templates=./consoles \
--web.config.file=./web.yml \
--storage.tsdb.retention.time=20d

浙公网安备 33010602011771号