Sphere
Log in Join

logrotate 'error: skipping because parent directory has insecure permissions' on Ubuntu 22.04

I noticed this because /var/log hit 91% on a small VPS and the weekly logrotate cron mail started arriving with a subject line I had been ignoring for weeks. Ubuntu 22.04, nginx 1.18.0 from apt, logrotate 3.19.0. The error had been there the whole time.

## The actual error

Running it by hand made it obvious:

$ sudo logrotate -d /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/nginx/*.log weekly (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
error: skipping "/var/log/nginx/access.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.

The directory in question:

$ ls -ld /var/log/nginx
drwxrwxr-x 2 www-data adm 4096 Mar 15 06:25 /var/log/nginx

Group-writable by adm, owned by www-data. logrotate refuses to rotate in a directory that is writable by a group it does not trust, because a malicious local user could swap the log file for a symlink. Fair enough.

## How it got that way

A previous setup guide told us to chown -R www-data:adm /var/log/nginx so nginx could write access logs directly. That works for nginx and breaks logrotate. The stock Ubuntu package does it differently: the log directory is root-owned and nginx's master process, running as root, opens the log files. Our "fix" had undone that.

## The two ways out

The clean fix is to put ownership back where the package expects it:

sudo chown -R root:adm /var/log/nginx
sudo chmod 755 /var/log/nginx
sudo chmod 640 /var/log/nginx/*.log
sudo systemctl reload nginx

nginx keeps the old file descriptors open after reload, so rotation continues to work and new files are created as root with the right mode.

The other option is to keep the permissions and declare them in the logrotate config, which is what you need when the app genuinely writes into its own directory:

/var/log/nginx/*.log {
weekly
rotate 14
missingok
notifempty
compress
delaycompress
su www-data adm
create 640 www-data adm
sharedscripts
postrotate
[ -s /run/nginx.pid ] && kill -USR1 $(cat /run/nginx.pid)
endscript
}

The su www-data adm line is the one that silences the error. Order matters: it has to come before create, otherwise logrotate complains about the order of directives.

## Testing without waiting

-d is a dry run and shows exactly what would happen. Once the config was fixed I forced a real rotation to be sure:

sudo logrotate -v -f /etc/logrotate.d/nginx

-f forces rotation even if the files are not old enough. The verbose output ended with rotating log /var/log/nginx/access.log, log->rotateCount is 14 and no errors. Two days later the cron mail contained only the normal summary.

## Why I am writing this down

The error message tells you the fix if you read the last sentence, but it is easy to skim past it in a cron email. We had 4.2GB of access logs sitting there because of a permissions change someone made eight months earlier and never mentioned. I added a check for log files older than the rotation period to our monitoring, which would have caught it in a week instead of eight months.