centos系統中的sudoer詳解
1. sudoer簡介
centos系統中的使用者可以分為兩類:
- 超級使用者-- root
- 普通使用者--其他
su(switch user)命令是通過切換使用者身份,來執行某種操作的命令。
su和sudo的區別如下:
- su命令主要作用是讓你可以在已經登入的會話中(不登出當前使用者的情況下)切換到另外一個使用者。
- sudo命令容許特定使用者以root使用者或者其他使用者的身份執行特定命令。
2.sudo配置簡介
sudo命令作用是:容許當前使用者以特定使用者去執行特定命令
配置檔案為:/etc/sudoers,修改該檔案使用visudo命令,該命令使用VI編輯器編輯sudoer檔案,並且會自動檢查語法錯誤。
配置格式如下:
user1 ALL=(ALL) ALL #我們來說一下這一行的配置的意思 #user1 表示該使用者user1可以使用sudo命令. 第一個ALL指的是網路中的主機(可以是主機名也可以是ip地址),它指明user1使用者可以在此主機上執行後面命令; 第二個括號裡的ALL是指目標使用者,也就是以誰的身份去執行命令。 最後一個ALL是指命令路徑。 user1 localhost=(root) /bin/kill #表示user1使用者可以在本地以root的身份去執行kill命令 #注意: 命令必須是完整的路徑
3./etc/sudoer配置檔案詳解
[root@ansible ~]# 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檔案主要有三部分組成:
- sudoers的預設配置(defaults),主要設定sudo的一些預設值
- alias(別名),主要有Host_Alias|Runas_Alias|Cmnd_Alias.
- 安全策略(規則定義)-重點
各部分詳解如下:
3.1 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"
3.2 主機別名
主機別名主要是用來建立主機分組,授予該組主機可以訪問哪些命令或命令別名,它的基本思想是,該檔案由組織中的所有主機共同維護,然後拷貝到每臺主機中的/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行的寫法。
3.3 使用者別名
使用者別名允許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
3.4 目標別名
Runas_Alias ex1=item_list
3.5 命名別名
就是將一部分命令進行歸類,方便系統管理員有效分配許可權。
## 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
3.6 安全策略(使用者規則)
語法規則:
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使用者可以在本機上無密碼以root身份執行/usr/local/bin/python
案例2:
user01 localhost=(root) NOPASSWD:/bin/more PASSWD:/bin/less
#user1使用者可以在本機上無密碼以root身份執行bin/more,有密碼以root身份執行less命令
案例3
slview localhost=(root) NOPASSWD:/usr/bin/passwd,!/usr/bin/passwd root #slview使用者可以執行passwd程式,但是不能修改root密碼,在命令前面加上歎號表示不能執行該程式。
[root@ansible ~]# su - slview
[slview@ansible ~]$ sudo passwd root
Sorry, user slview is not allowed to execute '/bin/passwd root' as root on ansible.
[slview@ansible ~]$ sudo passwd yjtest
Changing password for user yjtest.
New password:
BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
Retype new password:
passwd: all authentication tokens updated successfully.
嘗試修改root的使用者密碼,失敗,然後嘗試修改yjtest的密碼,成功。
4. 其他
萬用字元只可以用在主機名、檔案路徑、命令列的引數列表中。下面是可用的萬用字元:
*:匹配任意數量的字元
?:匹配一個任意字元
[...]:匹配在範圍內的一個字元
[!...]:匹配不在範圍內的一個字元
\x:用於轉義特殊字元
在使用萬用字元時有以下的注意點:
1.使用[:alpha:]等萬用字元時,要轉義冒號':',如:[:alpha:]
2.當萬用字元用於檔案路徑時,不能跨'/'匹配,如:/usr/bin/*能匹配/usr/bin/who但不能匹配/usr/bin/X11/xterm
3.如果指令的引數列表是""時,匹配不包含任何引數的指令。
4.ALL這個關鍵字表示匹配所有情況。