Httpd 目錄僅讀取權限問題

Httpd 目錄僅讀取權限問題

在我的場景中,我需要“/var/log/”中的“httpd”目錄可供“devs”群組中的使用者讀取(Amazon Linux 2)有人可以指導我如何完成此操作嗎?

我們有一些維護系統的開發人員,我希望他們能夠輕鬆讀取 /var/log/httpd 中的日誌文件,而無需 root 存取權限。

這樣有幫助嗎?

chmod -R go+r /var/log/httpd

或者chmod -R go+rX /var/log/httpd

或者我需要去:

chmod 644 /var/log/httpd

chgrp -R apache /var/log/httpd
chmod 02750 /var/log/httpd
chmod 0640 /var/log/httpd/*
create 0640 root apache

LS

[root@ip-10-0-10-165 httpd]# ls -la
total 48
drwx------ 19 root   devs   4096 Apr  3 03:42 .
drwxr-xr-x 11 root   devs   4096 Apr  8 07:45 ..
-rw-r--r--  1 root   root      0 Feb 15 14:55 access_log
drwxr-xr-x  2 root   devs     41 Mar  1 20:44 u1-dev.qwerty.com
-rw-r--r--  1 root   root   1648 Apr  3 03:42 error_log
-rw-r--r--  1 root   root    883 Mar 13 03:41 error_log-20220313
drwxr-xr-x  2 root   devs     41 Mar  1 20:44 u4-dev.qwerty.com
drwxr-xr-x  2 root   root     41 Mar  1 20:44 langs.qwerty.com
drwxr-xr-x  2 root   devs     41 Mar  1 20:44 u8-dev3.qwerty.com
-rw-r--r--  1 root   root      0 Feb 15 14:55 ssl_access_log
-rw-r--r--  1 root   root    314 Apr  3 03:42 ssl_error_log
-rw-r--r--  1 root   root    157 Mar  6 03:50 ssl_error_log-20220313
-rw-r--r--  1 root   root      0 Feb 15 14:55 ssl_request_log
drwxr-xr-x  2 apache apache  253 Apr  3 03:42 www.qwerty.com
drwxr-xr-x  2 root   devs     41 Mar  1 20:44 u13-dev.qwerty.com
[root@ip-10-0-20-173 httpd]#

如何修改這些權限集?這裡最好的方法是什麼?

答案1

您可以對該目錄使用acl並為devs群組授予權限。例如:

setfacl -m g:devs:rx httpd/

您可以為目錄設定檔案存取清單。在您的範例中,您有 httpd 目錄,並且僅為使用者/群組 root 設定了權限。

# ls -la| grep httpd
drwx------   2 root     root        4096 Apr  8 10:11 httpd

您可以檢查該目錄是否沒有acl列表:

getfacl httpd/
# file: httpd/
# owner: root
# group: root
user::rwx
group::---
other::---

我們為devs群組設定讀取和執行權限:

setfacl -m g:devs:rx httpd/

設定好權限後,會是這樣的:

getfacl httpd/
# file: httpd/
# owner: root
# group: root
user::rwx
group::---
group:devs:r-x
mask::r-x
other::---

請注意 ls 指令上任何現有 acl 的額外符號 (+)。

# ls -la| grep httpd
drwxr-x---+  2 root     root        4096 Apr  8 10:11 httpd

最好使用 -b 開關重複 setfacl 指令來設定預設權限。我通常這樣做。

-d, --default
           All operations apply to the Default ACL. Regular ACL entries in the input set are promoted to Default ACL entries. Default ACL entries in the input set are discarded. (A warning  is  issued
           if that happens).

問候

答案2

根據您如何標記問題,您正在使用 Redhat 風格的環境。假設您使用的是普通的 Apache httpd 安裝,您可能在幕後處理 logrotate。 logrotate 開箱即用,每天執行/etc/cron.daily。它將移開目前的日誌文件,建立新的日誌檔案並重新啟動,httpd以便它將開始使用新建立的空白日誌檔案。

查看/etc/logrotate.d/httpd。例如,庫存版本顯示:

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

如果您在大括號內新增這樣的行,logrotate 將正確旋轉並使用正確的擁有者和群組成員資格重新建立日誌檔案:

    create 0640 root devs

這指示 logrotate 建立新的日誌文件root,其擁有者devs為群組,權限為擁有者讀取/寫入和群組讀取。

然後,您可以使用權限和所有權來設定 httpd 日誌目錄,如下所示:

chown root:devs /var/log/httpd
chmod 0750 /var/log/httpd

在第一天,您可能還需要手動設定日誌檔案的權限,logrotate 將在接下來的幾天中啟動:

chown root:devs /var/log/httpd/*log
chmod 0640 /var/log/httpd/*log

您可以喜歡上面 @KuchnMar 演示的 ACL,但此解決方案使事情變得簡單並且跨各種 *nix 和檔案系統類型相容。

這在 Debian 風格的主機上可能類似,但可能會呼叫您的 logrotate 設定檔apache2

更多資訊可以在這裡找到:

相關內容