天天看點

linux詳解sudoers

sudo使用

  Linux是多使用者多任務的作業系統, 共享該系統的使用者往往不隻一個。出于安全性考慮, 有必要通過useradd建立一些非root使用者, 隻讓它們擁有不完全的權限; 如有必要,再來提升權限執行。

  sudo就是來解決這個需求的: 這些非root使用者不需要知道root的密碼,就可以提權到root,執行一些root才能執行的指令。

  執行sudo -u <使用者名> <指令>, 将允許目前使用者,提權到<使用者名>的身份,再執行後面的<指令>, 即使<指令>原本需要root權限。提權到<使用者名>身份時,是以<使用者名>的身份來執行指令的,是以建立的檔案預設屬于<使用者名>使用者。

  是以,當userB執行如下指令時:

sudo -u userA touch /tmp/belong-to-who.tmp      

  建立的/tmp/belong-to-who.tmp 檔案屬于使用者userA。

  如果不帶-u, 則預設使用root使用者,而大多數時候sudo都是要提權到root的,是以-u <使用者名>可以省略為:

sudo <指令>      

  需要注意的是: 執行sudo時輸入的密碼是目前使用者的密碼, 并非<使用者名>的密碼。

  sudo -u <使用者名>和su - <使用者名>相比:

sudo -u <使用者名>:需要輸入目前使用者的密碼,提權到<使用者名>身份執行指令後傳回目前使用者;

su - <使用者名>:則是輸入目标使用者的密碼,切換到目标使用者。

sudo指令執行過程

  将目前使用者切換到超級使用者下,或切換到指定的使用者下,然後以超級使用者或其指定切換到的使用者身份執行指令,執行完成後,直接退回到目前使用者。

  具體工作過程如下:

  當使用者執行sudo時,系統會主動尋找/etc/sudoers檔案,判斷該使用者是否有執行sudo的權限

  –>确認使用者具有可執行sudo的權限後,讓使用者輸入使用者自己的密碼确認

  –>若密碼輸入成功,則開始執行sudo後續的指令

賦予使用者sudo操作的權限

  通過useradd添加的使用者,并不具備sudo權限。在ubuntu/centos等系統下, 需要将使用者加入admin組或者wheel組或者sudo組。以root使用者身份執行如下指令, 将使用者加入wheel/admin/sudo組:

usermod -a -G wheel <使用者名>      

  如果提示wheel組不存在, 則還需要先建立該組:

groupadd wheel      

/etc/sudoers内容詳解

  sudo的權限控制可以在/etc/sudoers檔案中檢視到。一般來說,通過cat /etc/sudoers指令來檢視該檔案, 會看到如下幾行代碼:

root ALL=(ALL:ALL) ALL

%wheel ALL=(ALL) ALL

%sudo ALL=(ALL:ALL) ALL

  對/etc/sudoers檔案進行編輯的代碼公式可以概括為:

授權使用者/組 主機=[(切換到哪些使用者或組)] [是否需要輸入密碼驗證] 指令1,指令2,...

  凡是[ ]中的内容, 都能省略; 指令和指令之間用,号分隔,為了友善說明, 将公式的各個部分稱呼為字段1 - 字段5:

授權使用者/組 主機 =[(切換到哪些使用者或組)] [是否需要輸入密碼驗證] 指令1,指令2,...

字段1 字段2 =[(字段3)] [字段4] 字段5

字段3、字段4,是可以省略的。

  在上面的預設例子中:

  "字段1"不以%号開頭的表示"将要授權的使用者", 比如例子中的root;以%号開頭的表示"将要授權的組", 比如例子中的%wheel組 和 %sudo組。

  "字段2"表示允許登入的主機, ALL表示所有; 如果該字段不為ALL,表示授權使用者隻能在某些機器上登入本伺服器來執行sudo指令. 比如:

jack mycomputer=/usr/sbin/reboot,/usr/sbin/shutdown      

  表示: 普通使用者jack在主機(或主機組)mycomputer上, 可以通過sudo執行reboot和shutdown兩個指令。"字段3"和"字段4"省略。

  "字段3"如果省略, 相當于(root:root),表示可以通過sudo提權到root; 如果為(ALL)或者(ALL:ALL), 表示能夠提權到(任意使用者:任意使用者組)。請注意,"字段3"如果沒省略,必須使用( )雙括号包含起來。這樣才能區分是省略了"字段3"還是省略了"字段4"。

  "字段4"的可能取值是NOPASSWD:。請注意NOPASSWD後面帶有冒号:。表示執行sudo時可以不需要輸入密碼。比如:

lucy ALL=(ALL) NOPASSWD: /bin/useradd      

  表示: 普通使用者lucy可以在任何主機上, 通過sudo執行/bin/useradd指令, 并且不需要輸入密碼.

  又比如:

peter ALL=(ALL) NOPASSWD: ALL      

  表示: 普通使用者peter可以在任何主機上, 通過sudo執行任何指令, 并且不需要輸入密碼。

  "字段5"是使用逗号分開一系列指令,這些指令就是授權給使用者的操作; ALL表示允許所有操作。

  指令都是使用絕對路徑, 這是為了避免目錄下有同名指令被執行,進而造成安全隐患。如果你将授權寫成如下安全性欠妥的格式:

lucy ALL=(ALL) chown,chmod,useradd      

  那麼使用者就有可能建立一個他自己的程式, 也命名為userad, 然後放在它的本地路徑中, 如此一來他就能夠使用root來執行這個"名為useradd的程式"。這是相當危險的!

指令的絕對路徑可通過which指令檢視到: 比如which useradd可以檢視到指令useradd的絕對路徑: /usr/sbin/useradd

編輯/etc/sudoers

  在實踐中,去編輯/etc/sudoers檔案,系統提示我沒權限啊,怎麼辦?這是因為/etc/sudoers的内容如此敏感,以至于該檔案是隻讀的。是以,編輯該檔案前,請确認清楚你知道自己正在做什麼。

  強烈建議通過visudo指令來修改該檔案,通過visudo修改,如果配置出錯,會有提示。

  不過,系統文檔推薦的做法,不是直接修改/etc/sudoers檔案,而是将修改寫在/etc/sudoers.d/目錄下的檔案中。如果使用這種方式修改sudoers,需要在/etc/sudoers檔案的最後行,加上#includedir /etc/sudoers.d一行(預設已有):

#includedir /etc/sudoers.d      

  注意了,這裡的指令#includedir是一個整體, 前面的#号不能丢,并非注釋,也不能在#号後有空格。任何在/etc/sudoers.d/目錄下,不以~号結尾的檔案和不包含.号的檔案,都會被解析成/etc/sudoers的内容。

  文檔中是這麼說的:

# This will cause sudo to read and parse any files in the /etc/sudoers.d
# directory that do not end in \'~\' or contain a \'.\' character.

# Note that there must be at least one file in the sudoers.d directory (this
# one will do), and all files in this directory should be mode 0440.

# Note also, that because sudoers contents can vary widely, no attempt is
# made to add this directive to existing sudoers files on upgrade.

# Finally, please note that using the visudo command is the recommended way
# to update sudoers content, since it protects against many failure modes.      

指令作用域

papi ALL=(root) NOPASSWD: /bin/chown,/usr/sbin/useradd      

  表示: 使用者papi能在所有可能出現的主機上, 提權到root下執行/bin/chown, 不必輸入密碼; 但運作/usr/sbin/useradd 指令時需要密碼.

  這是因為NOPASSWD:隻影響了其後的第一個指令: 指令1。上面給出的公式隻是簡化版,完整的公式如下:

授權使用者/組 主機=[(切換到哪些使用者或組)] [是否需要輸入密碼驗證] 指令1, [(字段3)] [字段4] 指令2, ...

  在具有sudo操作的使用者下, 執行sudo -l可以檢視到該使用者被允許和被禁止運作的指令.

通配符以及取消指令

papi ALL=/usr/sbin/*,/sbin/*,!/usr/sbin/fdisk      

  指令前面加上!号表示取消該指令。該例子的意思是: 使用者papi在所有可能出現的主機上, 能夠運作目錄/usr/sbin和/sbin下所有的程式, 但fdisk除外。

輸入密碼時有回報

  當使用sudo後輸入密碼,并不會顯示任何東西 —— 甚至連正常的星号都沒有。有個辦法可以解決該問題。打開/etc/sudoers檔案找到下述一行:

Defaults env_reset      

  修改成:

Defaults env_reset,pwfeedback      

修改sudo會話時間

  如果你經常使用sudo 指令,你肯定注意到過當你成功輸入一次密碼後,可以不用再輸入密碼就可以運作幾次sudo指令。但是一段時間後,sudo 指令會再次要求你輸入密碼。預設是15分鐘,該時間可以調整。添加timestamp_timeout=分鐘數即可。時間以分鐘為機關,-1表示永不過期,但強烈不推薦。

  比如我希望将時間延長到1小時,還是打開/etc/sudoers檔案找到下述一行:

Defaults env_reset      

  修改成:

Defaults env_reset,pwfeedback,timestamp_timeout=60      

實踐

  針對MySQL資料庫的設定,讓test組中的test使用者具備/etc/init.d/mysqld的權限:

[root@test ~]# groupadd test
[root@test ~]# useradd -g test -m -d /home/test -s /bin/bash test
[root@test ~]# passwd test

[root@test ~]# visudo
# test ALL=(ALL) NOPASSWD: /etc/init.d/mysqld
test ALL=(ALL) /etc/init.d/mysqld

#start mysql 
[root@test ~]# su test
[test@test ~]$ sudo /etc/init.d/mysqld start

#stop mysql
[root@test ~]# su test
[test@test ~]$ sudo /etc/init.d/mysqld stop      

  針對tomcat的設定,讓test組中的test使用者具備tomcat操作的權限:

[root@test ~]# groupadd test
[root@test ~]# useradd -g test -m -d /home/test -s /bin/bash test
[root@test ~]# passwd test

[root@test ~]# visudo
# test ALL=(ALL) /usr/local/tomcat/bin/shutdown.sh,/usr/local/tomcat/bin/startup.sh
test ALL=(ALL) NOPASSWD: /usr/local/tomcat/bin/shutdown.sh,/usr/local/tomcat/bin/startup.sh
[root@test ~]# vim /usr/local/tomcat/bin/catalina.sh

### JDK 
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=$JAVA_HOME/jre

#start tomcat 
[root@test ~]# su test 
[test@test ~]$ sudo /usr/local/tomcat/bin/startup.sh
[test@test ~]$ ss -ntlup | grep Java
[test@test ~]$ curl -I http://localhost:8080

#stop tomcat 
[root@test ~]# su test 
[test@test ~]$ sudo /usr/local/tomcat/bin/shutdown.sh      

附錄:(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指令      
linux詳解sudoers