sudoers的用户权限控制

http://manpages.ubuntu.com/manpages/bionic/en/man5/sudoers.5.html

sudo 是linux下常用的允许普通用户使用超级用户权限的工具,允许系统管理员让普通用户执行一些或者全部的root命令,如halt,reboot,su等等。这样不仅减少了root用户的登陆和管理时间,同样也提高了安全性。Sudo不是对shell的一个代替,它是面向每个命令的。它的特性主要有这样几点:
1.sudo能够限制用户只在某台主机上运行某些命令。

2.sudo提供了丰富的日志,详细地记录了每个用户干了什么。它能够将日志传到中心主机或者日志服务器。

3.sudo使用时间戳文件来执行类似的“检票”系统。当用户调用sudo并且输入它的密码时,用户获得了一张存活期为5分钟的票(这个值可以在编译的时候改变)。

4.sudo的配置文件是sudoers文件,它允许系统管理员集中的管理用户的使用权限和使用的主机。它所存放的位置默认是在/etc/sudoers,属性必须为0411。

 

我们可以通过sudo -l 列出当前用户的权限。那么如何修改sudoers的配置文件呢?

正确的姿势是通过visudo来对该文件进行修改。强烈推荐使用该命令修改 sudoers,因为它会帮你校验文件配置是否正确,如果不正确,在保存退出时就会提示你哪段配置出错的。
言归正传,下面介绍如何配置sudoers
首先写sudoers的缺省配置:
#############################################################
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#
# Host alias specification
# User alias specification
# Cmnd alias specification
# Defaults specification
# User privilege specification
root    ALL=(ALL) ALL
# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL
# Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL
# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now
##################################################################
1. 最简单的配置,让普通用户support具有root的所有权限
执行visudo之后,可以看见缺省只有一条配置:
root    ALL=(ALL) ALL
那么你就在下边再加一条配置:
support ALL=(ALL) ALL
这样,普通用户support就能够执行root权限的所有命令
以support用户登录之后,执行:
sudo su -
然后输入support用户自己的密码,就可以切换成root用户了
2. 让普通用户support只能在某几台服务器上,执行root能执行的某些命令
首先需要配置一些Alias,这样在下面配置权限时,会方便一些,不用写大段大段的配置。Alias主要分成4种
Host_Alias
Cmnd_Alias
User_Alias
Runas_Alias
1) 配置Host_Alias:就是主机的列表
Host_Alias      HOST_FLAG = hostname1, hostname2, hostname3
2) 配置Cmnd_Alias:就是允许执行的命令的列表,命令前加上!表示不能执行此命令.

命令一定要使用绝对路径,避免其他目录的同名命令被执行,造成安全隐患 ,因此使用的时候也是使用绝对路径!
Cmnd_Alias      COMMAND_FLAG = command1, command2, command3 ,!command4
3) 配置User_Alias:就是具有sudo权限的用户的列表
User_Alias USER_FLAG = user1, user2, user3
4) 配置Runas_Alias:就是用户以什么身份执行(例如root,或者oracle)的列表
Runas_Alias RUNAS_FLAG = operator1, operator2, operator3
5) 配置权限
配置权限的格式如下:
USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG
如果不需要密码验证的话,则按照这样的格式来配置
USER_FLAG HOST_FLAG=(RUNAS_FLAG) NOPASSWD: COMMAND_FLAG
配置示例:
############################################################################
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#
# Host alias specification
Host_Alias      EPG = 192.168.1.1, 192.168.1.2
# User alias specification
# Cmnd alias specification
Cmnd_Alias      SQUID = /opt/vtbin/squid_refresh, !/sbin/service, /bin/rm

Cmnd_Alias      ADMPW = /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd, !/usr/bin/passwd root
# Defaults specification
# User privilege specification
root    ALL=(ALL) ALL
support EPG=(ALL) NOPASSWD: SQUID
support EPG=(ALL) NOPASSWD: ADMPW
# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL
# Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL
# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now
###############################################################

 

http://www.manongjc.com/article/61772.html

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

你可能感兴趣的:(开源工作从Linux开始)