Linux中的sudoer詳解


Linux中的sudo詳解

一、引言

Liunx用戶只有兩類:

  • 管理員用戶---root
  • 普通用戶--其他

我們在學習sudo命令之前,肯定學習過su(switch user)命令,su命令是用來切換用戶身份的命令,而sudo命令是一種切換用戶來執行某種操作的命令

su和sudo的區別:

  • su命令的主要作用是讓你可以在已經登錄的會話中切換到另外一個用戶,換句話說,這個工具可以讓你在不登出當前用戶的情況下,登錄另外一個用戶。

  • sudo命令的主要作用是允許特定的用戶以root用戶或其他用戶的身份執行命令。

二、格式

sudo的作用剛才已經講了,就是允許去以的身份去執行命令

所以這里加粗的三個部分是關鍵,這里的配置首先需要我們以root用戶去修改/etc/sudoers文件,修改該文件我們使用visudo命令(命令行直接寫visudo,回車即可),它使用一個vi編輯器去打開sudoers文件,並且會幫我們檢查語法錯誤。非常有用。

user1 ALL=(ALL)  ALL

#我們來說一下這一行的配置的意思
#user1 表示該用戶user1可以使用sudo命令,第一個ALL指的是網絡中的主機(可以是主機名也可以是ip地址),它指明user1用戶可以在此主機上執行后面命令;第二個括號里的ALL是指目標用戶,也就是以誰的身份去執行命令。最后一個ALL是指命令路徑。

user1 localhost=(root)  /bin/kill
#表示user1用戶可以在本地以root的身份去執行kill命令

#注意: 命令必須是完整的路徑

三、/etc/sudoers文件

[root@test ~]# cat /etc/sudoers
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##該文件允許特定用戶像root用戶一樣使用各種各樣的命令,而不需要root用戶的密碼 
##
## 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.
## 該文件必須使用"visudo"命令編輯
## Host Aliases
#主機別名
## Groups of machines. You may prefer to use hostnames (perhap using 
## wildcards for entire domains) or IP addresses instead.
## 對於一組服務器,你可能會更喜歡使用主機名(可能是全域名的通配符)
## 或IP地址代替,這時可以配置主機別名

# 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...
## 指定一系列相互關聯的命令(當然可以是一個)的別名,通過賦予該別名sudo權限,  
## 可以通過sudo調用所有別名包含的命令,下面是一些示例

## 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
## Updating the locate database
#本地數據庫升級命令別名  
Cmnd_Alias LOCATE = /usr/sbin/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
#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear. 
#         You have to run "ssh -t hostname sudo <cmd>".
#
Defaults    requiretty
Defaults    env_reset
Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY"
## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## 下面是規則配置:什么用戶在哪台服務器上可以執行哪些命令(sudoers文件可以在多個系統上共享)
## Syntax:
##語法
##      user    MACHINE=COMMANDS
##  用戶 登錄的主機=(可以變換的身份) 可以執行的命令  
##
## The COMMANDS section may have other options added to it.
## 命令部分可以附帶一些其它的選項  
##
## Allow root to run any commands anywhere 
## 允許root用戶執行任意路徑下的任意命令 
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
## 允許sys中戶組中的用戶使用NETWORKING等所有別名中配置的命令

## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL
## 允許wheel用戶組中的用戶執行所有命令  
## Same thing without a password
## 允許wheel用戶組中的用戶在不輸入該用戶的密碼的情況下使用所有命令
# %wheel        ALL=(ALL)       NOPASSWD: ALL
## Allows members of the users group to mount and unmount the
## cdrom as root
## 允許users用戶組中的用戶像root用戶一樣使用mount、unmount、chrom命令 
# %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
## 允許users用戶組中的用戶像root用戶一樣使用shutdown命令

四、sudoers文件講解

1、sudoers文件主要有三部分組成:

  • sudoers的默認配置(default),主要設置sudo的一些缺省值
  • alias(別名),主要有Host_Alias|Runas_Alias|User_Alias|Cmnd_Alias。
  • 安全策略(規則定義)——重點

2、各部分詳細解析

  • Default部分

    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
    
    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"
    
    
  • 主機別名

    主機別名主要是用來創建主機分組,授予該組主機可以訪問哪些命令或命令別名,它的基本思想是,該文件由組織中的所有主機共同維護,然后拷貝到每台主機中的/etc中,其中有些主機,例如各種服務器,可以配置一個組來賦予用戶訪問特定命令的權限。比如可以啟停類似HTTPD、DNS以及網絡服務;可以掛載系統等等。

    ## 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   HOST1 = host1, host2
    Host_Alias   HOST2 = host3,host4
    Host_Alias   HOST1 = host1,host2  HOST2=host3,host4
    ##其中第7行等價於第5、6行的寫法。
    
  • 用戶別名

    用戶別名允許root將多個用戶整理成一組中,並按組來分配目標用戶的權限。這部分是可選的,定義的時候是User_Alias group1=user1,user2使用的時候是%group1。我們也可以直接使用/etc/groups中定義的組而不用自己設置的別名。

    ## 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 group1 = user1, user2
    
  • 目標別名

    Runas_Alias ex1=item_list
    
  • 命令別名

    就是將一部分命令進行歸類,方便系統管理員有效分配權限。

    ## 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
    
    #Command Aliases
    ## These are groups of related commands...
    ## Command Aliases
    ## These are groups of related commands...
    SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
    # 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
    
    Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
    
  • 安全策略(用戶規則)

    語法規則:

    User_List Host_List=(Runas_List1:Runas_List2) SELinux_Spec Tag_Spec Cmnd_List
    #User_List(必填項):指的是該規則是針對哪些用戶的
    #Host_List(必填項):指的是該規則針對來自哪些主機的用戶
    #Runas_List1(可選項):表示可以用sudo -u來切換的用戶
    #Runas_List2(可選項):表示可以用sudo -g來切換的用戶組
    #SELinux_Spec(可選項):表示SELinux相關的選項,可選值為ROLE=role或TYPE=type。本人對SELinux不太熟,以后再補充這里吧
    #Tag_Spec(可選項):用於控制后面Cmnd_List的一些選項,可選值有下面這些
    'NOPASSWD:' | 'PASSWD:' | 'NOEXEC:' | 'EXEC:' | 'SETENV:' | 'NOSETENV:' | 'LOG_INPUT:' | 'NOLOG_INPUT:' | 'LOG_OUTPUT:' | 'NOLOG_OUTPUT:'|'MAIL:'|'NOMAIL:'|
    
    ###注意####
    ##如果Runas_Alias和Runas_Alias都沒填的話,默認是以root用戶執行
    

    案例1:

    user01  localhost=(root)  NOPASSWD:/bin/more
    #user1用戶可以在本機上無密碼執行/usr/local/bin/python
    

    案例2:

    user01  localhost=(root)  NOPASSWD:/bin/more PASSWD:/bin/less
    #user1用戶可以在本機上無密碼執行/usr/local/bin/python,有密碼執行kill命令
    

    案例3:

    user01 localhost=(root) NOPASSWD:/usr/bin/passwd,!/usr/bin/passwd root
    #user01用戶可以執行passwd程序,但是不能修改root密碼,在命令前面加上嘆號表示不能執行該程序。
    

    **嘗試修改root的用戶密碼,失敗,然后嘗試修改user02的密碼,成功。

五、其他

通配符只可以用在主機名、文件路徑、命令行的參數列表中。下面是可用的通配符:

*:匹配任意數量的字符

?:匹配一個任意字符

[...]:匹配在范圍內的一個字符

[!...]:匹配不在范圍內的一個字符

\x:用於轉義特殊字符

在使用通配符時有以下的注意點:

1.使用[:alpha:]等通配符時,要轉義冒號':',如:[:alpha:]

2.當通配符用於文件路徑時,不能跨'/'匹配,如:/usr/bin/*能匹配/usr/bin/who但不能匹配/usr/bin/X11/xterm

3.如果指令的參數列表是""時,匹配不包含任何參數的指令。

4.ALL這個關鍵字表示匹配所有情況。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM