温馨提示×

ubuntu filebeat怎么配置证书

小樊
34
2026-10-25 16:28:41
栏目: 智能运维

在 Ubuntu 上配置 Filebeat 使用证书,通常出现在以下两种场景:

  1. Filebeat → Elasticsearch(HTTPS + TLS)
  2. Filebeat → Logstash(TLS)
  3. Filebeat 自身作为 HTTPS Server(较少见)

下面以 最常见场景:Filebeat 向 Elasticsearch / Logstash 发送数据并使用 TLS 证书 为例说明。


一、准备工作(证书文件)

通常需要以下文件:

  • ca.crt:CA 证书(用于验证服务端)
  • client.crt:Filebeat 客户端证书(可选,双向认证时必需)
  • client.key:客户端私钥

示例目录:

/etc/filebeat/certs/
├── ca.crt
├── client.crt
└── client.key

设置权限:

sudo chown -R root:root /etc/filebeat/certs
sudo chmod 600 /etc/filebeat/certs/*

二、Filebeat 连接 Elasticsearch(HTTPS + TLS)

1️⃣ 修改 /etc/filebeat/filebeat.yml

output.elasticsearch:
  hosts: ["https://es-node1:9200"]
  username: "filebeat_writer"
  password: "your_password"

  ssl:
    enabled: true
    certificate_authorities: ["/etc/filebeat/certs/ca.crt"]
    certificate: "/etc/filebeat/certs/client.crt"
    key: "/etc/filebeat/certs/client.key"

如果 Elasticsearch 只验证服务端(单向 TLS),可以只配置 certificate_authorities


三、Filebeat 连接 Logstash(TLS)

1️⃣ filebeat.yml 配置

output.logstash:
  hosts: ["logs.example.com:5044"]

  ssl:
    enabled: true
    certificate_authorities: ["/etc/filebeat/certs/ca.crt"]
    certificate: "/etc/filebeat/certs/client.crt"
    key: "/etc/filebeat/certs/client.key"

2️⃣ Logstash 对应配置(示例)

input {
  beats {
    port => 5044
    ssl => true
    ssl_certificate => "/etc/logstash/certs/server.crt"
    ssl_key => "/etc/logstash/certs/server.key"
    ssl_certificate_authorities => ["/etc/logstash/certs/ca.crt"]
    ssl_verify_mode => "force_peer"
  }
}

四、自签名证书常见坑

1️⃣ 证书 CN / SAN 必须匹配

Elasticsearch / Logstash 的主机名必须出现在:

  • Common Name (CN)
  • 或 Subject Alternative Name (SAN)

否则会报:

x509: certificate is valid for xxx, not yyy

2️⃣ 时间同步

证书对时间敏感,确保:

timedatectl status

五、测试配置

sudo filebeat test config
sudo filebeat test output

六、重启 Filebeat

sudo systemctl restart filebeat
sudo systemctl status filebeat

七、查看日志排查问题

journalctl -u filebeat -f

八、常见错误速查

错误 原因
x509: unknown authority 没配 ca.crt
tls: failed to verify certificate CN/SAN 不匹配
permission denied 证书权限不对
no such file 路径写错

如果你能告诉我:

  • ✅ Ubuntu 版本
  • ✅ Filebeat 版本
  • ✅ 是连 ES 还是 Logstash
  • ✅ 用的是 Elastic Stack 官方证书还是自签

我可以给你一份 完全可直接用的配置示例。

0 踩