Prometheus 开启基本认证 basic_auth 防止未授权访问怎么配

文章导读
Prometheus 服务端本身不支持在 prometheus.yml 里直接开账号密码,要防止未授权访问,推荐使用官方支持的 web.config.file 配置文件配合启动参数,或者在前层挂 Nginx 做代理认证。
📋 目录
  1. A 命令速用版
  2. B 为什么会这样
  3. C 分步处理
  4. D 怎么验证是否生效
  5. E 常见坑
  6. F 参考来源
A A

Prometheus 服务端本身不支持在 prometheus.yml 里直接开账号密码,要防止未授权访问,推荐使用官方支持的 web.config.file 配置文件配合启动参数,或者在前层挂 Nginx 做代理认证。

先说结论:原生 Prometheus 开放端口默认无认证,需通过独立配置文件开启基本认证,不要尝试在主配置文件中寻找相关选项。

  • 先判断:确认 Prometheus 版本支持 web.config.file 功能(大多数近年稳定版均支持)。
  • 优先做:生成 bcrypt 加密的密码哈希,编写 web_config.yml 文件,注意格式为 username: hash 映射。
  • 再验证:启动后使用 curl 带账号密码访问,确认未带密码被拒绝。

命令速用版

如果你已经准备好了密码哈希,可以直接参考以下配置片段和启动命令。

1. 配置文件 web_config.yml 示例:

注意密码字段必须是 bcrypt 哈希值,不能是明文。格式为用户名直接映射哈希字符串。

basic_auth_users:\n  admin: "$2y$05$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

2. 启动命令:

注意参数中间没有多余符号,直接指定文件路径。

prometheus `--web`.config.file=./web_config.yml

为什么会这样

Prometheus 设计之初主要面向内网可信环境,因此开源版本的服务端 UI 和 API 默认是不带认证机制的。很多用户会在 prometheus.yml 里找不到 basic_auth 选项,这是因为该文件主要用于配置抓取目标(scrape_config),而不是保护服务端本身。

为了在不引入反向代理的情况下提供基础保护,官方后续引入了 web.config.file 机制,允许通过独立 YAML 文件配置 TLS 和基本认证。这是一种轻量级的止血方案,适合内网暴露面收敛的场景。

分步处理

第一步:生成密码哈希

Prometheus 开启基本认证 basic_auth 防止未授权访问怎么配

Prometheus 的 basic_auth 不接受明文密码,必须使用 bcrypt 算法哈希。你可以使用 Apache 的 htpasswd 工具或在线生成器。

如果使用 htpasswd 命令:

htpasswd -nbB admin your_password

复制输出中以 $2y$ 或 $2a$ 开头的字符串,这就是配置需要的内容。

第二步:编写配置文件

新建一个 web_config.yml,内容结构如下:

basic_auth_users:\n  admin: "$2y$05$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

确保该文件权限仅管理员可读,避免哈希值泄露。

第三步:修改文件权限

为了防止权限不足导致 Prometheus 启动失败,需将文件属主修改为运行用户并限制权限。

chown prometheus:prometheus web_config.yml\nchmod 600 web_config.yml

第四步:修改启动参数

Prometheus 开启基本认证 basic_auth 防止未授权访问怎么配

在 systemd 或启动脚本中,添加 `--web`.config.file 参数指向该文件。如果是 systemd 管理,编辑 /etc/systemd/system/prometheus.service,在 ExecStart 行追加参数。

ExecStart=/path/to/prometheus `--web`.config.file=/path/to/web_config.yml ...

修改完成后,重载配置并重启服务:

systemctl daemon-reload\nsystemctl restart prometheus

怎么验证是否生效

1. 无密码访问测试

在终端执行 curl 命令,不带认证信息应返回 401 状态码:

curl -o /dev/null -s -w "%{http_code}" http://localhost:9090/graph

预期输出应为 401。

2. 带密码访问测试

带上账号密码访问,应返回 200 状态码:

curl -u admin:your_password -o /dev/null -s -w "%{http_code}" http://localhost:9090/graph

预期输出应为 200。

常见坑

1. 配置格式错误

Prometheus 开启基本认证 basic_auth 防止未授权访问怎么配

web_config.yml 中 basic_auth_users 下应为 username: hash 的映射关系,不要写成列表对象(如 - username: admin)。格式错误会导致 Prometheus 启动失败或认证功能不生效。

2. 密码格式错误

配置文件中密码字段如果填入明文密码,Prometheus 启动会报错或认证直接失败。必须确保是 bcrypt 哈希。

3. 配置文件路径权限

web_config.yml 包含密码哈希,建议权限设置为 600,属主为 prometheus 用户。如果权限过大或属主错误,systemd 启动时可能因安全策略拒绝读取。

4. 与反向代理冲突

如果你已经在 Nginx 层做了 basic_auth,Prometheus 服务端再开一层认证会导致客户端需要输入两次密码,通常建议只保留一层,优先推荐 Nginx 层处理,便于统一日志和管理。

5. 抓取配置混淆

不要在 prometheus.yml 的 scrape_config 里配置 basic_auth 来保护服务端,那是用来配置 Prometheus 去抓取其他目标时的认证信息,方向反了。

参考来源

  • Prometheus Official Documentation, Configuration / HTTPS, https://prometheus.io/docs/prometheus/latest/configuration/https/
  • Prometheus Exporter Utils, Web Configuration, https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md