"journalctl"的数据存储在哪里?

当我执行journalctl命令时,会得到一个包含所有系统服务的大量日志,但是这些信息都存储在哪里呢?

2使用Berkley ᴅʙ 4.3作为日志文件格式,我认为journalctl是你唯一的选择。 - user2284570
4个回答

man systemd-journald中得知:
FILES
       /etc/systemd/journald.conf
           Configure systemd-journald behavior. See journald.conf(5).

       /run/log/journal/machine-id/*.journal,
       /run/log/journal/machine-id/*.journal~,
       /var/log/journal/machine-id/*.journal,
       /var/log/journal/machine-id/*.journal~
           systemd-journald writes entries to files in
           /run/log/journal/machine-id/ or /var/log/journal/machine-id/ with
           the ".journal" suffix. If the daemon is stopped uncleanly, or if
           the files are found to be corrupted, they are renamed using the
           ".journal~" suffix, and systemd-journald starts writing to a new
           file.  /run is used when /var/log/journal is not available, or when
           Storage=volatile is set in the journald.conf(5) configuration file.

而正如man journalctl所说:
journalctl may be used to query the contents of the systemd(1) journal
as written by systemd-journald.service(8).

这些日志由 systemd-journald 服务管理,因此更适当的术语应该是 "journald 日志"。


1谢谢您的纠正,但是想象一下像我这样的菜鸟也会搜索这个问题,所以我觉得最好还是保留原样。接下来有一个问题——这些日志可以安全删除吗? - php_nub_qq
9嗯,除非你以后需要这些信息,否则日志是可以安全删除的。 - muru
请注意,默认情况下,systemd将在磁盘空间使用达到一定百分比时删除较旧的日志。 - mattdm
@muru,同时,如果日志位于/run/log目录下,在重启后将无法保存... - Alexis Wilke

请注意,Ubuntu默认情况下不使用持久的journald日志文件。只有易失性的/run/log/journal/<machine-id>/*.journal[~]会被保留直到下一次启动。每次重启都会丢失所有内容。
您可以通过以下命令查看日志中保留的引导列表:
journalctl --list-boot

日志仍然保存在一个文本文件中,位于/var/log下,除非您通过创建/var/log/journal目录来激活持久的journald日志使用。

6然而,可以说 journald 日志应该默认是持久的。[bug #1618188](https://bugs.launchpad.net/ubuntu/+source/systemd/+bug/1618188)已被打开以跟踪此更改的进展。请在那里检查最新状态。 - Mark Stosberg
1根据17.10版本,上述的错误已经修复。 - Klaas van Schelven

简短回答

通常存储目录是/var/log/journal/run/log/journal,但它不一定存在于您的系统中。

如果您只想检查日志在磁盘上占用的空间量,只需输入:

$ journalctl --disk-usage

长答案

存储目录取决于journald的配置。

配置文件为:

/etc/systemd/journald.conf
/etc/systemd/journald.conf.d/*.conf
/run/systemd/journald.conf.d/*.conf
/usr/lib/systemd/journald.conf.d/*.conf

在这里,"Storage="选项控制是否存储日志数据以及存储位置。可能的取值有"volatile"、"persistent"、"auto"和"none"。默认为"auto"。
如果选择"volatile",日志数据将仅存储在内存中,即在/run/log/journal层次结构下(如果需要会创建该目录)。
如果选择"persistent",数据将优先存储在磁盘上,即在/var/log/journal层次结构下(如果需要会创建该目录),在早期引导和磁盘不可写入时,会回退到/run/log/journal层次结构下(如果需要会创建该目录)。
"auto"与"persistent"类似,但如果需要,不会创建目录/var/log/journal,因此其存在性决定了日志数据的存储位置。
"none"关闭所有存储,所有接收到的日志数据都将被丢弃。

除了Muru的回答关于数据存储在哪里,还有其他相关的答案。

如何增加journalctl以查找先前的引导日志

$ sudo mkdir -p /var/log/journal
$ sudo systemd-tmpfiles --create --prefix /var/log/journal

如何保持journalctl文件大小不变

$ sudo journalctl --vacuum-size=200M
Deleted archived journal /var/log/journal/d7b25a27fe064cadb75a2f2f6ca7764e/system@00056515dbdd9a4e-a6fe2ec77e516045.journal~ (56.0M).
Deleted archived journal /var/log/journal/d7b25a27fe064cadb75a2f2f6ca7764e/user-65534@00056515dbfe731d-b7bab56cb4efcbf6.journal~ (8.0M).
Deleted archived journal /var/log/journal/d7b25a27fe064cadb75a2f2f6ca7764e/user-1000@1bbb77599cf14c65a18af51646751696-000000000000064f-00056444d58433e1.journal (112.0M).
Vacuuming done, freed 176.0M of archived journals on disk.

我不得不使用 sudo 来执行 --vacuum-size=。但还是谢谢。 - Doug Smythies
@DougSmythies 谢谢你指出这一点。我有一个名叫 cron 的机器人,每个月都会帮我做吸尘的工作 :). 所以我从来不需要在终端中输入命令。我已经更新了答案。 - WinEunuuchs2Unix