brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the su line in logrotate fixes "parent directory has insecure permissions"

Permissions
~/tips/tip-logrotate-su-directive

rotate 4

Let logrotate rotate logs in a folder that a normal user or app can write to.

Point logrotate at a log in a folder that an app’s own user or group can write to, and it refuses with skipping "/var/log/myapp/app.log" because parent directory has insecure permissions. That isn’t fussiness. logrotate runs as root, and renaming and creating files as root in a directory someone else controls is an old trick to make root write where it shouldn’t.

The fix is the su directive, which tells logrotate to handle that log as a given user and group:

/var/log/myapp/*.log {
    su myapp myapp
    create 0640 myapp myapp
    weekly
    rotate 4
    compress
}

Now the rotation in /var/log/myapp runs as myapp, which owns that folder already, so there’s nothing left to abuse. The man page recommends exactly this: when logrotate runs as root, use su for directories that are directly or indirectly under the control of non-privileged users. The full error message even tells you to set it.

Taking group and world write away from the folder also makes the message go away, but then the app may no longer be allowed to create its log there. With su you don’t have to change the permissions at all, which is why I’d go this way.

One thing to watch: the user in su must be allowed to create files with the owner and mode you ask for in create. Ask for root as owner while rotating as myapp and logrotate gives you an error.

Further reading: logrotate(8) man page, logrotate source code on GitHub

More Permissions tips sudo su -s /bin/bash www-data sudo setfacl -R -d -m g:media:rwX /srv/media sudo cp -r /srv/site /srv/site.r ls -ln /srv/data newgrp media All Permissions tips (30) All topics

Link