Linux用户命令权限管理-sudo | IT运维网
  • 本站启用了账户登录密码错误就锁定模式,如有人误操作被锁请邮件(yvan.lu@ityww.cn)告知我账户名。
  • 本站为个人技术性站点,上面会更新一些系统、网络、虚拟化及云计算等相关的文章,与大家一起参考、学习和交流。
  • 欢迎访问本网站;本站QQ交流群:654792529;如果您觉得本站非常有看点,那么赶紧使用Ctrl+D收藏吧!

Linux用户命令权限管理-sudo

CentOS yvan 5年前 (2017-05-03) 1331次浏览 已收录 0个评论 扫描二维码

概述

       能够限制用户只在某台主机上运行某些命令。提供了丰富的日志,详细地记录了每个用户干了什么,它能够将日志传到中心主机或者日志服务器。
       使用时间戳文件来执行类似的“检票”系统,当用户调用sudo并且输入它的密码时,用户获得了一张存活期为5分钟的票(这个值可以在编译的时候改变)。
       配置文件是sudoers文件,它允许系统管理员集中的管理用户的使用权限和使用的主机,它所存放的位置默认是在/etc/sudoers,属性必须为0440。

原理

       普通用户运行sudo>>检查/var/db/sudo下是否有时间戳文件并检查是否超过五分钟>>时间未超过五分钟(超过五分钟需输入用户自己的口令)>>检查/etc/sudoers配置文件是否有运行sudo和执行命令的权限>>有权限则执行命令并返回结果(无权限在直接退出sudo)>>退出sudo

相关配置文件:/etc/sudoers

sudo配置文件没有特殊需求不要使用vim命令编辑;推荐直接用visudo来编辑,此命令可以检查语法(sudo-c)保证编辑无误;visudo等同于vim /etc/sudoers

[root@localhost ~]# visudo
## 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
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command.

## 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
# Host_Alias     MAILSERVERS = smtp, smtp2

## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
# User_Alias ADMINS = jsmith, mikem


## Command Aliases
## These are groups of related commands...

## Networking
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool

## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

## Services
# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable

## Updating the locate database
# Cmnd_Alias LOCATE = /usr/bin/updatedb

## Storage
# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount

## Delegating permissions
# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp

## Processes
# 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_home
Defaults    match_group_by_gid

# Prior to version 1.8.15, groups listed in sudoers that were not
# found in the system group database were passed to the group
# plugin, if any. Starting with 1.8.15, only groups of the form
# %:group are resolved via the group plugin by default.
# We enable always_query_group_plugin to restore old behavior.
# Disable this option for new behavior.
Defaults    always_query_group_plugin

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

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

## 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
使用环境

       编辑配置文件配置针对用户模式的命令权限,保留root账户不动,在下面添加需要被分配权限的账户,每个账户一行。我们以添加系统管理员账户为例(账户root不对外公布):

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
test    ALL=(ALL)       ALL

第一列:授权用户
第二列:第一个ALL为主机名(默认为ALL),第二个ALL为角色名(默认为ALL)
第三列:授权可执行命令,命令必需是全路径格式(通过命令which可查看命名路径),多命令用逗号分隔。ALL代表全部命令

用户查看命令权限:

[test@localhost ~]$ sudo -l
[sudo] password for test:
Matching Defaults entries for test on bogon:
    !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS
    LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET
    XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin

User test may run the following commands on bogon:
    (ALL) ALL

用户执行权限命令:

[test@localhost ~]$ sudo systemctl restart sshd

普通账户切换root身份(无需知道root密码):

[test@localhost ~]$ sudo -i
[sudo] password for test:
[root@localhost ~]#

       查看配置文件我们可以看到针对用户组模式的命令权限默认已经配有wheel组,我们仍以添加系统管理员账户为例(账户root不对外公布):

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

第一列:授权用户组
第二列:第一个ALL为主机名(默认为ALL),第二个ALL为角色名(默认为ALL)
第三列:授权可执行命令,命令必需是全路径格式(通过命令which可查看命名路径),多命令用逗号分隔。ALL代表全部命令

我们把需要管理员权限的账户添加进wheel组里:

[root@localhost ~]# usermod -aG wheel test
[root@localhost ~]# id test
uid=1000(test) gid=1000(test) groups=1000(test),10(wheel)
用户查看命令权限及执行权限命令等同用户环境

IT运维网 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Linux用户命令权限管理-sudo
喜欢 (3)
[yvan.lu@ityww.cn]
分享 (0)
yvan
关于作者:
聪明来自勤奋、知识在于积累、好记性不如烂键盘!

您必须 登录 才能发表评论!