美文网首页Linux每日命令Linux我用 Linux
详解Linux下的sudo及其配置文件/etc/sudoers

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

作者: 快乐程序员 | 来源:发表于2019-05-23 15:52 被阅读7次

    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

    3、最简单的配置,让普通用户support具有root的所有权限 

    执行visudo之后,可以看见缺省只有一条配置: 

    root    ALL=(ALL) ALL 

    那么你就在下边再加一条配置: 

    support ALL=(ALL) ALL

    这样,普通用户support就能够执行root权限的所有命令

    以support用户登录之后,执行: 

    sudo su - 

    然后输入support用户自己的密码,就可以切换成root用户了

    4、让普通用户support只能在某几台服务器上,执行root能执行的某些命令 

    首先需要配置一些Alias,这样在下面配置权限时,会方便一些,不用写大段大段的配置。Alias主要分成4种

    Host_Alias Cmmd_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 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

    User_Alias U_DEVELOPER=%developer

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

    Cmnd_Alias C_DEVELOPER = /sbin/*,/usr/sbin/*,/bin/*,/usr/bin/*,/usr/local/php/bin/*

    ## Services

    # Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig

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

    U_DEVELOPER ALL = C_DEVELOPER

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

    Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC 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

    观注快乐程序员公众号,每日分享一点小知识。爱编程,爱生活!

    相关文章

      网友评论

        本文标题:详解Linux下的sudo及其配置文件/etc/sudoers

        本文链接:https://www.haomeiwen.com/subject/ivhbzqtx.html