logrotate 無法輪換,root 使用者權限被拒絕

logrotate 無法輪換,root 使用者權限被拒絕

我遇到了一個我無法解決的問題。我在 centos 7 伺服器上設定了 logrotate 來輪換我的應用程式日誌。

該檔案位於 /etc/cron.daily 中(我甚至添加了 whoami 來驗證它是否以 root 身份運行):

#!/bin/sh

whoami

/usr/sbin/logrotate -v /etc/logrotate.d/myapp
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

這會執行我的 logrotate 設定...

"/opt/myapp/log/*.log" {
  daily
  create 644 root root
  rotate 5
  size 20M
  copytruncate
  missingok
  notifempty
  compress
}

這是我的日誌目錄...

[root@server log]# ls -la
total 97684
drwxr-xr-x. 3 root root      4096 Sep 28 12:41 .
drwxr-xr-x. 5 root root      4096 Sep 27 20:57 ..
-rw-r--r--. 1 root root 100015457 Sep 28 12:36 myapp.log

每次我們以 root 身份手動運行它時,它都會輪換日誌......但是當我們通過 cron 運行它時,它會失敗。這是它發送的郵件...

From root@server  Thu Sep 28 12:30:02 2017
Return-Path: <root@server>
X-Original-To: root
Delivered-To: root@server
Received: by server (Postfix, from userid 0)
        id 9C82B419; Thu, 28 Sep 2017 12:30:02 -0500 (CDT)
From: "(Cron Daemon)" <root@server>
To: root@server
Subject: Cron <root@server> run-parts /etc/cron.daily
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=10746>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20170928173002.9C82B419@server>
Date: Thu, 28 Sep 2017 12:30:01 -0500 (CDT)
/etc/cron.daily/myapp:

root <<<<<<<<< whoami output
reading config file /etc/logrotate.d/myapp
Handling 1 logs
rotating pattern: /opt/myapp/log/*.log  20971520 bytes (5 rotations)
empty log files are not rotated, old logs are removed
considering log /opt/myapp/log/myapp.log
  log needs rotating
rotating log /opt/myapp/log/myapp.log, log->rotateCount is 5
error: error opening /opt/myapp/log/myapp.log: Permission denied
set default create context

有人遇到過這種情況或有任何想法可能導致這種情況嗎?

答案1

事實證明這是一個 SELinux 問題,文件上下文不是 logrotate 有權存取的文件上下文,因此多次被拒絕存取。

請參閱此處的答案:SELinux fcontext

相關內容