sudo的安全策略:阻止/允许用户执行特定命令

应用场景

我们知道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,该策略可以阻止修改特定用户的密码。也可以得知,该策略可以阻止特定参数的命令

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值