DNS キャッシュのための systemd-resolved を Unbound へ変更してみるお話です。
前回はクエリーログが無事に専用のログファイルへ書き出すところまで完了したので、今回はそのログの運用ということでログローテーションさせてみます。
なんか自分で cron に処理を登録しないといけないのかと思っていたのですが、logrotate という仕組みがすでに用意されていたのでこれに定義を追加するだけでした。
というわけで、さっそく試してみましょう。
$ ls alternatives aptitude btmp dpkg pm-utils rsyslog speech-dispatcher wtmp apt bootlog cups-daemon mintupdate ppp sane-utils ufw
すでに LMDE にはいくつかのローテーション定義が登録されていました。mintupdate を参考にしてみますかね。
$ cat mintupdate
/var/log/mintupdate.log {
rotate 12
monthly
compress
missingok
notifempty
nocreate
}
mintupdate.log は月に一度だけローテーションする定義のようです。この定義をひな型にしてみましょう。
$ sudo cp mintupdate unbound
そしてファイルを編集します。
$ cat unbound
/var/log/unbound/unbound.log {
rotate 7
daily
missingok
ifempty
nocreate
dateext
}
ログファイルの保持期間は一週間にしました。(rotate 7)
古いログファイルには日付を付けるように指示しています。(dateext)
なので、必ず毎日ログファイルが作成されるように空であっても処理するように指示しました。(ifempty)
ローテーション定義が準備できたら定義内容の不備を点検することができるようです。
$ logrotate -d /etc/logrotate.d/unbound warning: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want. reading config file /etc/logrotate.d/unbound Reading state from file: /var/lib/logrotate/status error: error opening state file /var/lib/logrotate/status; assuming empty state: 許可がありません Allocating hash table for state file, size 64 entries Handling 1 logs rotating pattern: /var/log/unbound/unbound.log after 1 days empty log files are rotated, (7 rotations), old logs are removed considering log /var/log/unbound/unbound.log Creating new state Now: 2026-01-24 13:53 Last rotated at 2026-01-24 13:00 log does not need rotating (log has already been rotated)
なんか /var/lib/logrotate/status にアクセスする権限がない、というエラーが出ていますがこれは気にしなくても大丈夫そうです。
ということで、強制的に動かしてみましょう。
$ sudo logrotate -fv /etc/logrotate.d/unbound
1 回目は何も起きなかったようですが、もう一度実行したら動いたようです。
$ sudo logrotate -fv /etc/logrotate.d/unbound reading config file /etc/logrotate.d/unbound acquired lock on state file /var/lib/logrotate/status Reading state from file: /var/lib/logrotate/status Allocating hash table for state file, size 64 entries Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Creating new state Handling 1 logs rotating pattern: /var/log/unbound/unbound.log forced from command line empty log files are rotated, (7 rotations), old logs are removed considering log /var/log/unbound/unbound.log log /var/log/unbound/unbound.log does not exist -- skipping
なんか「ログファイルがないぞ」と言ってますね。ファイル名の指定が間違ってたかな?
$ ls -l 合計 2304 -rw-r--r-- 1 unbound unbound 1842473 1月 25 10:29 unbound.log-20260125 -rw-r--r-- 1 root root 508985 1月 24 08:34 unbound.log.2026-01-23
あ、確かにないですね。
下のファイルはワタシが手動でローテーションしたものです。上のファイルは logrotate で作成されたものです。
Unbound はファイルが存在しなければ自分で作成するものだと思って logrotate のパラメーターには nocreate を指定していたのですが、create に変更しないといけないようです。
このままだとログファイルが存在しないままなので Unbound を再起動させてログファイルを再作成させてみます。
$ ls -lh 合計 2.3M -rw-r--r-- 1 unbound unbound 6.6K 1月 25 12:30 unbound.log -rw-r--r-- 1 unbound unbound 1.8M 1月 25 10:50 unbound.log-20260125 -rw-r--r-- 1 root root 498K 1月 24 08:34 unbound.log.2026-01-23
無事にログファイルは復活しました。Unbound は起動時にのみログファイルを自動で作ってくれるようです。
unbound.log のパーミッションが unbound に付与されていますが、logrotate でファイルを再作成した場合は root がファイルオーナーになったちゃう気がしますね。
今日はもうローテーションできないと思われるので、明日の処理に向けて logrotate の設定を修正しておきます。
$ cat unbound
/var/log/unbound/unbound.log {
rotate 7
daily
missingok
ifempty
create
dateext
}
編集が終わったので構文チェックしてみます。
$ logrotate -d /etc/logrotate.d/unbound warning: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want. reading config file /etc/logrotate.d/unbound Reading state from file: /var/lib/logrotate/status error: error opening state file /var/lib/logrotate/status; assuming empty state: 許可がありません Allocating hash table for state file, size 64 entries Handling 1 logs rotating pattern: /var/log/unbound/unbound.log after 1 days empty log files are rotated, (7 rotations), old logs are removed considering log /var/log/unbound/unbound.log Creating new state Now: 2026-01-25 14:08 Last rotated at 2026-01-25 14:00 log does not need rotating (log has already been rotated)
構文エラーはなさそうです。これで明日まで待ってみましょう。(ログローテーションのテストって大変じゃないですか?これだと?)
ということで翌日になりました。新しい logrotate の定義内容は期待通りに動いてくれているでしょうか?
$ ls -lh /var/log/unbound/unbound* -rw-r--r-- 1 unbound unbound 438K 1月 27 19:55 /var/log/unbound/unbound.log -rw-r--r-- 1 unbound unbound 1.8M 1月 25 10:50 /var/log/unbound/unbound.log-20260125 -rw-r--r-- 1 unbound unbound 23K 1月 26 18:44 /var/log/unbound/unbound.log-20260126 -rw-r--r-- 1 unbound unbound 1.4M 1月 27 14:08 /var/log/unbound/unbound.log-20260127 -rw-r--r-- 1 root root 498K 1月 24 08:34 /var/log/unbound/unbound.log.2026-01-23
おお、大成功です。
1 月 26 日だけログ量がやたらと少ないのは事故が起きていたからです。 Unbound の serve-expired に yes を指定してみた日なのですが、なんかやたらと DNS 問い合わせが遅いなぁとは思いつつも、Unbound の設定変更の影響かと思っていたのでした。
ふと /etc/resolv.conf を確認してみると NetworkManager が乗っ取りするようになっていて、Unbound を使わない設定に差し替わっていることに気づいたのです。
NetworkManager の DNS サーバーの指定を 127.0.0.1 とかに変更することで NetworkManager に乗っ取りさせつつも Unbound が使われるように対応しました。
色々とありましたが、これにてログローテーションの設定導入は完了です。