LoginSignup
3
0

More than 1 year has passed since last update.

webサーバをubuntuにしたらlogrotateできなくなった

Last updated at Posted at 2022-12-22

はじめに

WebサーバをCentOSからubuntu22.04に引っ越しをしたらlogrotateがうまく動かなくなりました。
「/usr/local/apache/logs」とフォルダを作成してログを書いていたけど

Xxx  X 00:00:00 webt3-1 logrotate[96234]: error: failed to rename /usr/local/apache/logs/access_log to /usr/local/apache/logs/access_log.2022XXXX: Read-only file system

Read-only file systemだから書けなくて怒っていました。

原因

検索してみるとlogrotateのsystemdユニットファイルの設定に「ProtectSystem=」があり、
その設定がCentOSだと

$ systemctl show logrotate.service --property ProtectSystem
ProtectSystem=no

でしたがUbuntu22.03だと

$ systemctl show logrotate.service --property ProtectSystem
ProtectSystem=full

になっています。「ProtectSystem=full」になっているので「/usr」などが読み込み専用でマウントされるらしいです。
https://askubuntu.com/questions/1275668/logrotate-succeeds-when-manually-run-as-root-but-fails-with-read-only-file-sys

対応

$ cat /lib/systemd/system/logrotate.service

[Unit]
Description=Rotate log files
Documentation=man:logrotate(8) man:logrotate.conf(5)
RequiresMountsFor=/var/log
ConditionACPower=true

[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.conf

# performance options
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7

# hardening options
#  details: https://www.freedesktop.org/software/systemd/man/systemd.exec.html
#  no ProtectHome for userdir logs
#  no PrivateNetwork for mail deliviery
#  no NoNewPrivileges for third party rotate scripts
#  no RestrictSUIDSGID for creating setgid directories
LockPersonality=true
MemoryDenyWriteExecute=true
PrivateDevices=true
PrivateTmp=true
ProtectClock=true
ProtectControlGroups=true
ProtectHostname=true
ProtectKernelLogs=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectSystem=full
RestrictNamespaces=true
RestrictRealtime=true

/lib/systemd/system/logrotate.serviceの「ProtectSystem=full」をCentOSと同じく「ProtectSystem=no」に変更するとCentOSと同じくかけると思います。
しかし、せっかくある設定なので生かしたいので
logrotate.serviceファイルの一番下に「ReadWritePaths=/usr/local/apache/logs/」を追加します。
すると、ログを書くフォルダだけWriteが出来るようになります。

最後に

CentOSとubuntuは結構違うなと思いました。

3
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0