当前位置: 移动技术网 > 网络运营>服务器>Linux > 详解Linux下的sudo及其配置文件/etc/sudoers的详细配置

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

2017年12月08日  | 移动技术网网络运营  | 我要评论
详解linux下的sudo及其配置文件/etc/sudoers的详细配置 1.sudo介绍 sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许系统

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

1.sudo介绍

sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许系统管理员让普通用户执行一些或者全部的root命令,如halt,reboot,su等等。这样不仅减少了root用户的登陆 和管理时间,同样也提高了安全性。sudo不是对shell的一个代替,它是面向每个命令的。

它的特性主要有这样几点:

  § sudo能够限制用户只在某台主机上运行某些命令。

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

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

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

2.配置文件/etc/sudoers

它的主要配置文件是sudoers,linux下通常在/etc目录下,如果是solaris,缺省不装sudo的,编译安装后通常在安装目录的 etc目录下,不过不管sudoers文件在哪儿,sudo都提供了一个编辑该文件的命令: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

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

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网