You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prometheus Basic Auth配置异常:UI可访问但采集目标报401错误

解决Prometheus Basic Auth采集/remote_write 401 Unauthorized问题

问题根因

Prometheus的scrape_configs和remote_write配置里的basic_auth.password字段要求填写明文密码,而非你用于Web UI登录的bcrypt哈希值。Web UI的认证逻辑是用bcrypt哈希做验证,但采集或remote_write时,Prometheus会直接把配置里的password值作为明文,经Base64编码后发送Basic Auth请求——用哈希值会导致验证不通过,返回401错误。

修复步骤

  1. 修改prometheus.yml的采集任务配置,将password替换为明文密码:
- job_name: "prometheus"
  scheme: http
  basic_auth:
    username: prometheus1
    password: your_plaintext_password  # 替换为你的实际明文密码
  static_configs:
    - targets: ["localhost:9090"]
  1. 若使用remote_write功能,同样要把对应配置里的basic_auth.password改成明文:
remote_write:
  - url: "your_remote_write_endpoint"
    basic_auth:
      username: prometheus1
      password: your_plaintext_password
  1. 重启Prometheus服务,使新配置生效。

验证方式

  • 查看Prometheus日志,确认不再出现server returned HTTP status 401 Unauthorized的报错。
  • 进入Prometheus Web UI的Targets页面,检查prometheus采集任务的状态是否变为UP。

安全优化建议

直接在配置文件中明文存密码存在安全风险,推荐用以下两种方式管理:

  • 环境变量注入:
    配置文件中引用环境变量:
    basic_auth:
      username: prometheus1
      password: "${PROMETHEUS_PASSWORD}"
    
    启动Prometheus时传入环境变量:
    PROMETHEUS_PASSWORD=your_password ./prometheus
    
  • 密码文件存储:
    创建仅包含密码的文件(比如/etc/prometheus/scrape_password),设置权限为600(仅允许Prometheus进程读取),然后配置:
    basic_auth:
      username: prometheus1
      password_file: "/etc/prometheus/scrape_password"
    

内容的提问来源于stack exchange,提问作者user20268898

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 01:03:20