详解Linux下的sudo及其配置文件/etc/sudoers的详细配置

2019-09-23 09:08:13王旭

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

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 

# Defaults specification 

# User privilege specification 
root  ALL=(ALL) ALL 
support EPG=(ALL) NOPASSWD: SQUID 

# 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 
##################################################

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!