sudo的安全策略:阻止/允许用户执行特定命令
添加新用户
adduser shi
按照提示一步一步操作
安装sudo
apt-get update
apt-get install sudo -y
配置sudo
首先,将用户移除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/shi #其中“shi”为测试建立的用户名
比如,我们期望用户test 可以以管理员权限执行/usr/bin、/bin下的所有命令,但是不能修改其他用户密码以及kill其他用户进程,可以配置如下:
shi ALL=/usr/bin/, !/usr/bin/passwd, /bin/, !/bin/kill
测试一下,会发现passwd被阻止了:
$ sudo passwd
[sudo] password for shi:
Sorry, user shi is not allowed to execute '/usr/bin/passwd'
在策略文件中配置:!/usr/bin/passwd user_name,该策略可以阻止修改特定用户的密码。也可以得知,该策略可以阻止特定参数的命令
参考资料: