日志轮换权限问题

14

我正在为某些Web应用编写自己的logrotate配置:

/home/me/public_html/logs/*.log {
    daily
    missingok
    rotate 15
    compress
    delaycompress
    notifempty
    create 0660 me www-data
    nosharedscripts
}

但是对于这些文件运行logrotate会导致:

$ sudo logrotate -d -v *.log
Ignoring logfile1.log because of bad file mode.
Ignoring logfile2.log because of bad file mode.
Ignoring otherlogfile.log because of bad file mode.

Handling 0 logs
$ ls -l
-rw-rw---- 1 me www-data  893584 Jan 27 16:01 logfile1.log
-rw-rw---- 1 me www-data  395011 Jan 27 16:01 logfile2.log
-rw-rw---- 1 me www-data 4949115 Jan 27 16:01 otherlogfile.log

这是否与目录中实际日志文件的文件权限或使用 create 0660 me www-data 指定的权限有关?

如果我将文件权限更改为 -rw-r----- 并将 create 行更改为

create 0640 me www-data

我明白了

$ sudo logrotate -d -v *.log
Ignoring logfile1.log because the file owner is wrong (should be root).
Ignoring logfile2.log because the file owner is wrong (should be root).
Ignoring otherlogfile.log because the file owner is wrong (should be root).

Handling 0 logs

我的系统是Debian Testing/Jessie。

1个回答

22

好的,这是一个愚蠢的情况。logrotate命令必须在配置文件上执行,而不是日志文件上。

$ sudo logrotate -d -v /etc/logrotate.d/my-app

似乎很重要的是日志文件的父目录不能是全局可写(------rw-),也不能被任何非root组写入(---rw----)。否则,你会看到:

error: skipping "/home/me/public_html/logs/logfile1.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.

我本来希望能在这里得到一个 sudo chmod [numbers] [files] 的实际解决方案。现在我必须继续寻找。 - MS Berends
我已将其更改为维基答案。您能添加相应的 chmod 命令吗? - white_gecko
这就是问题所在 - 我不太熟悉八进制和chmod :( 如果我找到了,我会添加的。 - MS Berends
我假设你需要运行 chown root /home/me/public_html/logs/logfile1.logchmod 600 /home/me/public_html/logs/logfile1.log,按照我之前的示例。如果你能尝试一下,就可以将其添加到答案中。 - white_gecko
事实上,许多系统都有一个上层全局配置文件。因此,仅使用该配置文件通常是错误的!为了解决这个问题,我编写了一个名为“logrotate_one”的脚本,它合并了两个文件,然后执行日志轮换... https://antofthy.gitlab.io/software/logrotate_one.sh.txt - anthony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接