If your Linux distribution supports systemd, then starting from startup, it will download all processes and applications from the system every second Collect logs in the program. All these journal events are managed by systemd’s journald
daemon. journald collects all logs (information, warnings, errors, etc.) and stores them as binary data in disk files.
Since the log is retained on disk and collected every second, it takes up a huge amount of disk space; especially for old systems and servers. For example, on one of my test systems that's been running for a year or so, the log files are gigabytes in size.
If you manage multiple systems and servers, it is recommended that you must correctly manage journald logs for efficient operation. Let's take a look at how to manage log files.
Using systemd’s journalctl
tool, you can query these logs and perform various operations on them. For example, view log files from different startups to examine the last warnings and errors for a specific process or application. If you don’t know any of this, I suggest you take a quick look at this tutorial before following this guide: Using journalctl to view and analyze systemd logs (with examples).
systemd's journald daemon collects logs every time it is started. This means that it categorizes log files based on startup conditions.
The log is stored in binary form in the path /var/log/journal
, and the folder is the machine ID.
For example:
Screenshot of log file location-1
Screenshot of log file location-2
Also, please remember that depending on the system configuration, the runtime log file is stored in /run/log/journal/
. And these will be deleted on every startup.
You can, but don't do it. Instead, follow the instructions below to clear the log files to free up disk space using the journalctl
tool.
Open a terminal and run the following command.
journalctl --disk-usage
This should give you the actual number of log files in use on your system.
journalctl disk usage command
If you have a graphical desktop environment, you can open the file manager and browse the path /var/log/journal
, and check the properties.
The effective way to clean log files should be through the journald.conf
configuration file. Under normal circumstances, even though journalctl
provides a tool to delete log files, you should not delete these files manually.
让我们来看看如何 手动 删除它,然后我将解释 journald.conf
中的配置变化,这样你就不需要时不时地手动删除文件;相反,systemd 会根据你的配置自动处理它。
首先,你必须 flush
和 rotate
日志文件。轮换rotate是将当前活动的日志文件归档,并立即开始创建一个新的日志文件继续记录日志。冲洗flush 开关要求日志守护进程将存储在 /run/log/journal/
中的所有日志数据冲入 /var/log/journal/
,如果持久性存储被启用的话。
然后,在 flush
和 rotate
之后,你需要用 vacuum-size
、vacuum-time
和 vacuum-files
选项运行 journalctl
来强制 systemd 清除日志。
例 1:
sudo journalctl --flush --rotate
sudo journalctl --vacuum-time=1s
上面这组命令会删除所有存档的日志文件,直到最后一秒。这有效地清除了一切。因此,在运行该命令时要小心。
日志清理-例子
清理完毕后:
清理后--日志的占用空间
你也可以根据你的需要在 --vacuum-time
的数字后面提供以下后缀:
s
:秒m
:分钟h
:小时days
:天months
:月weeks
:周years
:年例 2:
sudo journalctl --flush --rotate
sudo journalctl --vacuum-size=400M
这将清除所有存档的日志文件,并保留最后 400MB 的文件。记住这个开关只适用于存档的日志文件,不适用于活动的日志文件。你也可以使用后缀,如下所示。
K
:KBM
:MBG
:GB例 3:
sudo journalctl --flush --rotate
sudo journalctl --vacuum-files=2
vacuum-files
选项会清除所有低于指定数量的日志文件。因此,在上面的例子中,只有最后两个日志文件被保留,其他的都被删除。同样,这只对存档的文件有效。
如果你愿意,你可以把两种选项结合起来,但我建议不要这样做。然而,如果同时使用两个选项,请确保先用 --rotate
选项运行。
虽然上述方法很好,也很容易使用,但建议你使用 journald 配置文件来控制日志文件的清理过程,该文件存在于 /etc/systemd/journald.conf
。
systemd 为你提供了许多参数来有效管理日志文件。通过组合这些参数,你可以有效地限制日志文件所占用的磁盘空间。让我们来看看。
journald.conf 参数 | 描述 | 实例 |
| 指定日志在持久性存储中可使用的最大磁盘空间 | |
| 指定在将日志条目添加到持久性存储时,日志应留出的空间量。 | |
# SystemMaxFileSize | Controls how large a single log file can grow in persistent storage before being rotated. | SystemMaxFileSize=100M |
RuntimeMaxUse | Specifies the maximum disk space that can be used in volatile storage (within the /run file system) . | RuntimeMaxUse=100M |
| /run file system) and is reserved for other uses. Number of spaces. | RuntimeMaxUse=100M | ##
指定单个日志文件在被轮换之前在易失性存储(在 | |
如果你在运行中的系统的 /etc/systemd/journald.conf
文件中添加这些值,那么在更新文件后,你必须重新启动 journald。要重新启动,请使用以下命令。
sudo systemctl restart systemd-journald
在你清理完文件后,检查日志文件的完整性是比较明智的。要做到这一点,请运行下面的命令。该命令显示了日志文件是否通过(PASS
)、失败(FAIL
)。
journalctl --verify
验证日志文件
希望本指南能帮助你了解 systemd 日志管理流程的基本情况。通过这些,你可以通过限制空间、清除旧的日志文件来管理系统或服务器中的日志文件所使用的磁盘空间。这些只是指导性的命令,你可以通过多种方式组合这些命令来实现你的系统需求。
The above is the detailed content of systemd log maintenance guide (with examples). For more information, please follow other related articles on the PHP Chinese website!