應用場景
我們知道sudo用戶組的用戶可以使用sudo以root權限運行命令。某些應用場景可能要求對用戶的sudo執行權限細分,比如可以執行部分命令,或者不能執行部分命令。這時可以通過配置sudo的默認安全策略插件sudoers來達到目的。
- 將用戶移除sudo用戶組(如果原來在的話)
- 確認/etc/sudoers配置策略
要使用visudo來編輯,默認該文件帶有描述:“This file MUST be edited with the ‘visudo’ command as root”。如果沒有改動過該文件,其大致如下:
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
- 編輯用戶安全策略配置
在/etc/sudoers.d/目錄下建立與用戶同名的策略文件,比如:
visudo -f /etc/sudoers.d/test #其中“test”爲測試建立的用戶名
比如,我們期望用戶test 可以以管理員權限執行/usr/bin、/bin下的所有命令,但是不能修改其他用戶密碼以及kill其他用戶進程,可以配置如下:
test ALL=/usr/bin/, !/usr/bin/passwd, /bin/, !/bin/kill
測試一下,會發現passwd被阻止了:
test@zzz:/home/sscm$ sudo passwd
[sudo] password for test:
Sorry, user test is not allowed to execute '/usr/bin/passwd'
as root on zzz.
test@zzz:/home/sscm$
在策略文件中配置:!/usr/bin/passwd user_name,該策略可以阻止修改特定用戶的密碼。也可以得知,該策略可以阻止特定參數的命令