如何配置 sudoers 以允許在其他群組和用戶名下運行 sudo 命令?

如何配置 sudoers 以允許在其他群組和用戶名下運行 sudo 命令?

如何配置sudoers以防止Sorry, user ****** is not allowed to execute錯誤訊息出現。

背景

為了測試 python 腳本如何在權限較低的使用者和群組守護程式帳戶下運行,需要運行:

$ sudo -u _denyhosts -g _denyhosts python /usr/local/bin/denyhosts.py 
-c /usr/share/denyhosts/denyhosts.cfg -n --purge --sync --verbose

結果是:

Sorry, user ****** is not allowed to execute 
'/usr/bin/python /usr/local/bin/denyhosts.py -c /usr/share/denyhosts/denyhosts.cfg 
-n --purge --sync --verbose' as _denyhosts:_denyhosts on ***.***.***.

猜測是該指令由於sudoers.

$ sudo -l
Matching Defaults entries for *** on this host:
    editor=/usr/bin/nano, env_reset, env_keep+=BLOCKSIZE, env_keep+="COLORFGBG
    COLORTERM", env_keep+=__CF_USER_TEXT_ENCODING, env_keep+="CHARSET LANG
    LANGUAGE LC_ALL LC_COLLATE LC_CTYPE", env_keep+="LC_MESSAGES LC_MONETARY
    LC_NUMERIC LC_TIME", env_keep+="LINES COLUMNS", env_keep+=LSCOLORS,
    env_keep+=SSH_AUTH_SOCK, env_keep+=TZ, env_keep+="DISPLAY XAUTHORIZATION
    XAUTHORITY", env_keep+="EDITOR VISUAL", env_keep+="HOME MAIL"

User *** may run the following commands on this host:
    (ALL) ALL

已嘗試透過執行並插入以下行_denyhosts來新增群組:sudoerssudo visudo

%_denyhosts    ALL=(ALL) ALL

保存並重試並沒有改善。

答案1

該行%_denyhosts ALL=(ALL) ALL表示允許_denyhosts群組中的使用者以任何使用者身分執行任何命令。這不是您想要做的:您需要允許使用者以 user和 group******身分執行命令。就像是:_denyhosts_denyhosts

****** ALL = (_denyhosts : _denyhosts) ALL

答案2

您需要確保即使 _denyhosts 中的使用者已新增至 sudoers 檔案中,您希望能夠執行此命令的使用者也已新增至 /etc/group 中的 _denyhosts 群組中。

$ sudo vi /etc/group
*snip*
_denyhosts:x:###:username1,username2,username3

儲存“群組”檔案並重試。

相關內容