How to enable persistent logging for the systemd journal?

在 Red Hat Enterprise Linux 7 和 8 中,systemd 默认将日志存储在 /run/log/journal 的小环形缓冲区中,这在系统重启后会丢失。本文介绍如何通过修改配置,使日志持久保存在 /var/log/journal 目录下。

https://access.redhat.com/solutions/696893

 SOLUTION 已验证 - 已更新 2019年一月18日00:39 - 

English 

环境

  • Red Hat Enterprise Linux 7
  • Red Hat Enterprise Linux 8

问题

  • The systemd journal is configured by default to store logs only in a small ring-buffer in /run/log/journal, which is not persistent
  • Journal database logs do not survive a system reboot

决议

  • Configure systemd to persistently store journal logs in /var/log/journal by running the following commands.

Raw

# mkdir -p /var/log/journal
  • You may decide if you need to keep or remove the rsyslog package installed on the system at this point, depending on your requirements
  • See man journalctl for more information on querying the journal database for specific logs

Instead of manually creating the /var/log/journal directory, one can also change the systemd-journald configuration file:

Raw

# sed -i 's/#Storage=auto/Storage=persistent/' /etc/systemd/journald.conf
# systemctl restart systemd-journald.service

With storage option "persistent", data will be stored preferably on disk, i.e. below the /var/log/journal hierarchy (which is created if needed), with a fallback to /run/log/journal (which is created if needed), during early boot and if the disk is not writable.

根源

  • By default in Red Hat Enterprise Linux 7 and 8, systemd is not configured to maintain logs persistently
<think>我们正在处理用户关于systemd-journal配置及使用方法的问题。根据用户的问题,我们需要提供关于systemd-journald服务的配置和使用方法的详细说明。首先,我们需要明确systemd-journald是systemd的一部分,负责日志记录。它收集来自内核、系统服务、用户进程等的日志,并将其存储在二进制文件中(通常是/var/log/journal目录下)。与传统的syslog相比,它提供了结构化日志和更高效的存储。配置systemd-journald的主要配置文件是`/etc/systemd/journald.conf`。我们可以通过编辑这个文件来调整日志记录的行为。常见配置选项包括:-Storage:指定日志存储方式(persistent,volatile,auto,none)。persistent表示持久存储到磁盘,volatile表示只保存在内存(重启丢失),auto表示如果/var/log/journal存在则持久存储,否则使用volatile。-Compress:是否压缩存储的日志文件。-Seal:是否使用FSS(ForwardSecureSealing)来保护日志。-SystemMaxUse,SystemKeepFree,SystemMaxFileSize:控制磁盘使用量。-RuntimeMaxUse,RuntimeKeepFree,RuntimeMaxFileSize:控制运行时(内存中)日志的大小。使用journalctl命令来查看和分析日志。常用命令包括:-`journalctl`:显示所有日志-`journalctl-uunit`:显示指定单元的日志-`journalctl-f`:跟踪日志(类似tail-f)-`journalctl--since"2020-01-0100:00:00"--until"2020-01-0212:00:00"`:按时间范围查看-`journalctl-perr`:显示错误级别及以上的日志-`journalctl-b`:显示本次启动的日志-`journalctl-k`:显示内核日志此外,还可以通过`-o`选项指定输出格式(如json、short、verbose等)。注意:修改配置文件后需要重启systemd-journald服务:`systemctlrestartsystemd-journald`。同时,用户可能需要了解如何清理日志以释放磁盘空间。可以使用`journalctl--vacuum-size=`或`journalctl--vacuum-time=`命令来清理旧的日志。根据用户提供的引用内容,我们注意到引用[2]提到systemd-journald会自动删除最早的存档日志文件以限制磁盘使用,这可以通过在`journald.conf`中设置`SystemMaxUse`等参数来控制。下面我们按照要求生成回答。</think>### systemd-journald 配置及使用指南 #### **核心功能** systemd-journald 是 systemd 的日志服务,提供结构化、集中化的日志管理: 1. **二进制存储**:日志以二进制格式存储于 `/var/log/journal/`(持久化)或 `/run/log/journal/`(临时) 2. **多源采集**:自动收集内核(`/dev/kmsg`)、系统服务、用户进程及 syslog 兼容数据[^2] 3. **元数据记录**:每条日志包含时间戳、服务单元、优先级等结构化信息 --- #### **配置文件** 主配置文件:`/etc/systemd/journald.conf` ```ini # 存储方式 (auto/persistent/volatile) Storage=auto # auto: 若/var/log/journal存在则持久化存储 # 磁盘空间管理 SystemMaxUse=500M # 最大磁盘占用 SystemKeepFree=1G # 保留磁盘空闲空间 RuntimeMaxUse=100M # 内存日志最大占用 # 日志保留策略 MaxRetentionSec=1month # 最长保留时间 Compress=yes # 启用压缩 ``` > **配置生效**:修改后需重启服务 > `sudo systemctl restart systemd-journald` --- #### **关键操作命令** 1. **实时日志监控** `journalctl -f` # 追踪新日志(类似 tail -f) 2. **按服务单元过滤** `journalctl -u nginx.service` # 查看指定服务日志 3. **按优先级筛选** ```bash journalctl -p err # 仅显示错误日志 journalctl -p warning..emerg # 范围筛选 ``` 4. **时间范围查询** ```bash journalctl --since "2023-06-01 09:00:00" --until "1 hour ago" ``` 5. **磁盘空间维护** ```bash journalctl --vacuum-size=200M # 清理至200MB journalctl --vacuum-time=2weeks # 删除2周前日志 ``` --- #### **高级特性** 1. **结构化查询** ```bash journalctl _PID=1234 + _SYSTEMD_UNIT=ssh.service ``` - 支持字段:`_UID`, `_COMM`, `_EXE` 等 2. **日志导出格式** ```bash journalctl -o json-pretty # JSON格式 journalctl -o short-iso # ISO时间戳格式 ``` 3. **FSS密封验证**(防篡改) ```bash journalctl --verify --file /var/log/journal/.../system.journal ``` --- #### **常见问题解决** - **日志不持久化**: 创建持久化目录:`sudo mkdir /var/log/journal && sudo systemctl restart systemd-journald` - **磁盘占满**: 调整 `SystemMaxUse` 或使用 `--vacuum-*` 命令清理[^2] - **服务未启动**: 检查状态:`systemctl status systemd-journald.socket` --- #### **日志流转示意图** ```mermaid graph LR A[/dev/kmsg] -->|内核日志| B[journald] C[系统服务] -->|stdout/stderr| B D[用户进程] -->|syslog API| B B --> E[持久存储 /var/log/journal] B --> F[运行时存储 /run/log/journal] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值