天天看點

Linux中的sudoer詳解

Linux中的sudo詳解

目錄

  • Linux中的sudo詳解
    • 一、引言
    • 二、格式
    • 三、/etc/sudoers檔案
    • 四、sudoers檔案講解
      • 1、sudoers檔案主要有三部分組成:
      • 2、各部分詳細解析
    • 五、其他

一、引言

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密碼,在指令前面加上歎号表示不能執行該程式。
               
    Linux中的sudoer詳解
    **嘗試修改root的使用者密碼,失敗,然後嘗試修改user02的密碼,成功。

五、其他

通配符隻可以用在主機名、檔案路徑、指令行的參數清單中。下面是可用的通配符:

*:比對任意數量的字元

?:比對一個任意字元

[...]:比對在範圍内的一個字元

[!...]:比對不在範圍内的一個字元

\x:用于轉義特殊字元

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

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

2.當通配符用于檔案路徑時,不能跨\'/\'比對,如:/usr/bin/*能比對/usr/bin/who但不能比對/usr/bin/X11/xterm

3.如果指令的參數清單是""時,比對不包含任何參數的指令。

4.ALL這個關鍵字表示比對所有情況。