在 CentOS 上设置 Filebeat 开机自启,取决于 CentOS 版本 和 Filebeat 的安装方式。下面是最常见的几种方式,按推荐程度说明。
如果你是通过 RPM 包 安装的 Filebeat(最常见),systemd 会自动生成服务。
systemctl list-unit-files | grep filebeat
或
ls /usr/lib/systemd/system/filebeat.service
sudo systemctl enable filebeat
sudo systemctl start filebeat
sudo systemctl status filebeat
✅ 看到 active (running) 即成功
如果你使用的是 CentOS 6 或老版本系统:
sudo chkconfig filebeat on
sudo service filebeat start
sudo service filebeat status
如果你是直接下载 filebeat 二进制文件,没有 systemd 服务,需要手动创建服务。
sudo vi /etc/systemd/system/filebeat.service
示例内容(按实际路径修改):
[Unit]
Description=Filebeat sends log files to Logstash or directly to Elasticsearch.
Documentation=https://www.elastic.co/products/beats/filebeat
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml
Restart=always
User=root
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable filebeat
sudo systemctl start filebeat
systemctl is-enabled filebeat
✅ 输出应为:
enabled(CentOS 7/8)on(CentOS 6)查看日志:
journalctl -u filebeat -f
filebeat test config
确保 filebeat.yml 权限正确:
chmod 600 /etc/filebeat/filebeat.yml
| CentOS 版本 | 安装方式 | 命令 |
|---|---|---|
| CentOS 7/8 | RPM | systemctl enable filebeat |
| CentOS 6 | RPM | chkconfig filebeat on |
| 任意 | 二进制 | 手动创建 systemd 服务 |
如果你愿意,可以告诉我:
cat /etc/centos-release)我可以给你 完全匹配你环境的命令 ✅