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這個關鍵字表示匹配所有情況。