syslogファイルが大きくなる問題でネットの情報に疑問あり

ディスクが32GBしかない ASUS E200HA に Lubuntu を入れて遊んでいるんですが、Chrome で「ディスクの空きが少ないから、なにか消すか?」みたいなダイアログが出てきたので、df で調べたら / パーティションが 100%になっていました。ネットでいろいろ調べていると、「ubuntu syslog ローテーション」の検索上位の記事内容になんかもやもやしてきたので、私見を書いておきたいと思いました。

まず / で du コマンドで使用状況を確認したら圧倒的に /var が消費していました。

$ cd /
$ sudo du -sm *
0       bin
1       bin.usr-is-merged
227     boot
0       dev
13      etc
1618    home
0       lib
1       lib.usr-is-merged
0       lib64
1       lost+found
1       media
1       mnt
374     opt
0       proc
15      root
3       run
0       sbin
1       sbin.usr-is-merged
5157    snap
1       srv
2049    swapfile
0       sys
1       tmp
5506    usr
17533   var

/var で再度 du を実行してみます。

$ cd /var
$ sudo du -sm *
5       backups
3220    cache
10      crash
4058    lib
1       local
0       lock
10237   log
1       mail
1       metrics
1       opt
0       run
4       snap
1       spool
1       tmp

案の定 log です。/var/log でどのファイルが食っているか確認。

$ cd log
$ ls -lS
合計 849184
-rw-r-----  1 syslog            adm            4598349824 11月 23 05:11 syslog.1
-rw-r-----  1 syslog            adm            2977046528 11月 23 05:11 kern.log.1
-rw-r-----  1 syslog            adm             531364202 11月 26 16:50 syslog
-rw-r-----  1 syslog            adm              30148958 11月 26 15:57 kern.log
-rw-r--r--  1 root              root               122513  4月 30  2025 dpkg.log.7.gz                                                                                 
[...]

syslog.1 と kern.log.1 の2つで7GBも食っているので、こいつらを圧縮します。

$ sudo gzip syslog.1
$ sudo kern.log.1
$ ls -l syslog.1.gz kern.log.1.gz 
-rw-r----- 1 syslog adm 110471538 11月 23 05:11 kern.log.1.gz
-rw-r----- 1 syslog adm 196260094 11月 23 05:11 syslog.1.gz

上のコマンドを実行すると、syslog.1 は削除され、圧縮された syslog.1.gz が生成されます。kern.log.1 も同様に kern.log.1.gz だけになります。

後で logrotate の設定変更するためにネットを検索して驚いたのですが、syslog.1 は削除できない から mv して再起動してから削除すべしみたいな記事が散見されていましたが、これは誤りです。syslog や kern.log は、まあファイルシステムによっては削除できないことがあるかもしれませんが、通常は削除できます。末尾に「.1」がついているファイルは普通に削除でき、できないことはないはずです。

rm コマンドはディレクトリのファイルリストから特定のファイルを削除するといった機能で、ファイルシステムの物理的領域からファイル自体を削除することはしません。

ファイルシステムはファイルのディレクトリに関連付けられた数(リンクカウント)が0になり、そのファイルのオープン数が0になった時点で未使用領域として開放します。よって、プロセスがオープンしているファイルはディレクトリから rm されたとしても読み書きができる状態のまま維持されています。

つまり rsyslogd は起動時にログファイルをオープンしているので、その後、rm されたとしても影響は受けません。追加で書き込まれます。

ネットの記事には、再起動後に削除しろという記載もありますが、ログを保存せずにディスク領域を回復したいなら再起動は不要で、以下のようなコマンドを実行するだけでOKです。

$ sudo echo -n > /var/log/syslog

これで syslog ファイルの中身は空っぽになってディスクの空き容量が増え、今後出力されるログは正常に記録されます。

$ sudo cp /dev/null /var/log/syslog

こんなのでも同じです。昔はこっちを教えてもらった記憶がありますが、echo のほうがスマートじゃないかと思います。

おすすめはしませんが、rm して rsyslogd を再起動するだけでもディスク領域は開放されるはずです。

というゴタクはこれくらいにして、一旦ディスク使用量が100%でなくなったので、根本対策を行います。

根本対策は2つの大きくなる可能性のあるログファイルを毎日ローテーションして圧縮してあげる、というものになります。圧縮すれば1/10以下になるので、まぁなんとかなるかなと。

syslog と kern.log の logrotate 設定ファイルは /etc/logrotate.d/rsyslog です。/etc/logrotated.d/ にある設定ファイルは直接編集しないほうがいいのですが、logrotate はデフォルトでは同じログファイルに対して複数の設定を許さないので、注意が必要です。

ネットで検索した際は同じログファイルの設定は重複できないから rsyslog ファイルを直接編集した、みたいなのもありましたが、ちゃんと調べたら重複できました。

logrotate のマニュアルを確認します。関連するのは以下の2箇所。

File selection
     [...]
     ignoreduplicates
         Ignore any following matches of a log file.
logrotate(8)
Additional config files
    include file_or_directory
         Reads the file given as an argument as if it was included inline
         where the include directive appears. If a directory is given,
         most of the files in that directory are read in alphabetic order
         before processing of the including file continues.
ogrotate(8)

logrotate.d ディレクトリのファイルはアルファベット順で処理されるそうなので、rsyslog より前になるように 00rsyslog というファイルを作ることにしました。

もとの rsyslog は以下。

/var/log/syslog
/var/log/mail.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/cron.log
{
        rotate 4
        weekly
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        postrotate
                /usr/lib/rsyslog/rsyslog-rotate
        endscript
}

新しく作った 00rsyslog は以下。

# daily rotation files
/var/log/syslog
/var/log/kern.log
{
        rotate 31
        daily
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        postrotate
                /usr/lib/rsyslog/rsyslog-rotate
        endscript
}

毎日ローテーション(daily)して31回分保存する(rotate 31)という設定です。

他は rsyslog と同じなので、詳しくはマニュアルを参照してほしいですが、注意が必要なオプションについて記載しておきます。

sharedscripts を指定しておくと、syslog と kern.log のローテーションが終わったら1回だけ postrotate スクリプトを実行するようになります。sharedscripts を指定しないとそれぞれのローテーションごとに postrotate スクリプトが実行されます。なお postrotate スクリプトは、postrotate から endscript の間に記載されたスクリプトで、スクリプトは sh で実行されます。

postrotate スクリプトに指定されている /usr/lib/rsyslog/rsyslog-rotate は rsyslogd に HUP シグナルを送っています。rsyslogd は HUP シグナルを受け取るとオープンしているすべてのファイルをクローズします。

HUP This lets rsyslogd perform close all open files.

rsyslogd(8)

delaycompress はローテーションしたときすぐには圧縮しないで、次回ローテーション時に圧縮するというものです。これは、ローテーション後HUPシグナルを送信してログファイルをクローズしてから圧縮しないといけないからです。そうしないと圧縮中に書き込まれたログを失う可能性があります。

ネットで検索したときに、logrotate を実行してみたら圧縮されないから delaycompress を指定しないみたいなのを見ましたが、このへんの危険性は考慮されているのか疑問に感じました。

設定ファイルができたので、logrotate をデバッグモード(-d)で動かしてみます。

$ sudo logrotate -d /etc/logrotate.conf
warning: logrotate in debug mode does nothing except printing debug messages!  Cons
ider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.conf
including /etc/logrotate.d
reading config file 00rsyslog
reading config file alternatives
reading config file apport
reading config file apt
reading config file aptitude
reading config file bootlog
reading config file btmp
reading config file cups-daemon
reading config file dpkg
reading config file ppp
reading config file rsyslog
error: rsyslog:1 duplicate log entry for /var/log/syslog
error: rsyslog:1 duplicate log entry for /var/log/kern.log
error: found error in file rsyslog, skipping
reading config file sane-utils
[...]
Handling 19 logs

rotating pattern: /var/log/syslog
/var/log/kern.log
 after 1 days (31 rotations)
empty log files are not rotated, old logs are removed
switching euid from 0 to 0 and egid from 0 to 4 (pid 35114)
considering log /var/log/syslog
  Now: 2025-11-28 08:42
  Last rotated at 2025-11-28 06:36
  log does not need rotating (log has been rotated at 2025-11-28 06:36, which is less than a day ago)
considering log /var/log/kern.log
  Now: 2025-11-28 08:42
  Last rotated at 2025-11-28 06:36
  log does not need rotating (log has been rotated at 2025-11-28 06:36, which is less than a day ago)
not running postrotate script, since no logs were rotated
switching euid from 0 to 0 and egid from 4 to 0 (pid 35114)
[...]
otating pattern: /var/log/syslog
/var/log/mail.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/cron.log
 weekly (4 rotations)
empty log files are not rotated, old logs are removed
switching euid from 0 to 0 and egid from 0 to 4 (pid 35114)
considering log /var/log/mail.log
  log /var/log/mail.log does not exist -- skipping
considering log /var/log/auth.log
  Now: 2025-11-28 08:42
  Last rotated at 2025-11-23 05:11
  log does not need rotating (log has been rotated at 2025-11-23 05:11, which is less than a week ago)
considering log /var/log/user.log
  log /var/log/user.log does not exist -- skipping
considering log /var/log/cron.log
  log /var/log/cron.log does not exist -- skipping
not running postrotate script, since no logs were rotated
switching euid from 0 to 0 and egid from 4 to 0 (pid 35114)
[...]

7行目:00rsyslog ファイルが最初に読み込まれている。20行目:rsyslog ファイルで重複エラーがあったがスキップ。25〜39行目:00rsyslog ファイルの設定が適用されている。41行目以降:rsyslog ファイルの設定は syslog と kern.log には適用されていない。

という具合に、意図したとおりになっていました。(なお、設定変更当日の実行結果が残っていなかったので、上記は翌日に loglotate -d を実行した結果です。)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です