设置 sudo 用户

来源:互联网 发布:苹果自动拨号软件 编辑:程序博客网 时间:2024/05/01 20:54

Linux版本:CentOS 6.5 64位/CentOS 7 64位

如想在非 root 用户下执行 root 权限命令,需切换至 root 用户或者使用 sudo 命令。前者在命令较少时显得有些麻烦,而后者则需当前用户具有 sudo 权限,即用户在 sudo 分组中。

将用户加入 sudo 分组:

#切换至 root 用户[admin@localhost ~]$ su密码:#编辑 sudo 分组[root@localhost admin]# visudo...截取部分配置如下...root    ALL=(ALL)       ALLadmin   ALL=(ALL)       ALL    #将 admin 用户添加至 sudo 分组......

然而,如仅像上文那样设置,则在使用 ssh 远程连接主机时无法使用 sudo 命令,提示信息如下:

sudo: 没有终端存在,且未指定 askpass 程序

这时需要将 admin 用户设置为免密使用 sudo 命令,截取部分配置具体如下:

...root    ALL=(ALL)       ALLadmin   ALL=(ALL)       NOPASSWD: ALL    #免密配置...

原以为这样就可以了,然而在使用 ssh 远程连接进行 sudo 操作时仍然报错并显示相同信息,查看配置文件后发现,是配置文件后的组权限配置覆盖了之前的用户权限,即将之前 admin 用户的免密配置被之后的组权限配置覆盖了,还原成了需要密码才能使用 sudo 命令。再次配置组权限如下:

...root    ALL=(ALL)       ALLadmin   ALL=(ALL)       NOPASSWD: ALL...%wheel  ALL=(ALL)       ALL    #此配置覆盖了之前的用户免密配置%admin  ALL=(ALL)       NOPASSWD: ALL    # admin 组免密...

后来想想,完全可以直接配置免密使用 sudo 的用户组,然后将需要此权限的用户加入用户组,真是白费了一番功夫。
最后的配置:

## Sudoers allows particular users to run various commands as## the root user, without needing the root password.#### Examples are provided at the bottom of the file for collections## Host Aliases## Groups of machines. You may prefer to use hostnames (perhaps using## wildcards for entire domains) or IP addresses instead.# Host_Alias     FILESERVERS = fs1, fs2## User Aliases## These aren't often necessary, as you can use regular groups# User_Alias ADMINS = jsmith, mikem## Command Aliases## These are groups of related commands...## Networking## Installation and management of software# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum## Services## Updating the locate database# Cmnd_Alias LOCATE = /usr/bin/updatedb## Storage# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall## Drivers# Cmnd_Alias DRIVERS = /sbin/modprobe# Defaults specification## Refuse to run if unable to disable echo on the tty.#Defaults   !visiblepw## Preserving HOME has security implications since many programs# use it when searching for configuration files. Note that HOME# is already set when the the env_reset option is enabled, so# this option is only effective for configurations where either# env_reset is disabled or HOME is present in the env_keep list.#Defaults    always_set_homeDefaults    match_group_by_gidDefaults    env_resetDefaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"## Adding HOME to env_keep may enable a user to run unrestricted# commands via sudo.## Defaults   env_keep += "HOME"Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin## Next comes the main part: which users can run what software on## which machines (the sudoers file can be shared between multiple## systems).## Syntax:####      user    MACHINE=COMMANDS#### The COMMANDS section may have other options added to it.#### Allow root to run any commands anywhereroot    ALL=(ALL)       ALL## Allows members of the 'sys' group to run networking, software,## service management apps and more.# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS## Allows people in group wheel to run all commands%wheel  ALL=(ALL)       ALL%admin  ALL=(ALL)       NOPASSWD: ALL    # admin 组免密## Same thing without a password# %wheel        ALL=(ALL)       NOPASSWD: ALL## Allows members of the users group to mount and unmount the## cdrom as root# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom## Allows members of the users group to shutdown this system# %users  localhost=/sbin/shutdown -h now## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)#includedir /etc/sudoers.d
原创粉丝点击