sudo授權(quán)需謹(jǐn)慎,否則親人兩行淚!6個(gè)超實(shí)用使用技巧
Sudo 授權(quán)需謹(jǐn)慎,否則親人兩行淚!
sudo 表示 “superuser do”,它允許已驗(yàn)證的用戶以其他用戶的身份來(lái)運(yùn)行命令。其他用戶可以是普通用戶或者超級(jí)用戶。然而,大部分時(shí)候我們用它來(lái)提權(quán)運(yùn)行命令,以替代直接使用 root 用戶的操作。sudo 命令與安全策略配合使用,安全策略可以通過(guò)文件 /etc/sudoers 來(lái)配置。其安全策略具有高度可拓展性,支持插件擴(kuò)展。默認(rèn)情況下 /etc/sudoers 是不能被任何人直接編輯的,因?yàn)樗臋?quán)限是 440,雖然也可以對(duì)其賦予寫權(quán)限后再編輯,但推薦使用 visudo 命令編輯該文件。
1. 工作模式理解
簡(jiǎn)述其使用工作流程和配置文件配置!
- [1] 理解 sudo 命令的工作流程
- sudo 會(huì)讀取和解析 /etc/sudoers 文件,查找調(diào)用命令的用戶及其權(quán)限。
- 然后提示調(diào)用該命令的用戶輸入密碼,或者也可以通過(guò) NOPASSWD 標(biāo)志來(lái)跳過(guò)密碼驗(yàn)證。
- 之后,sudo 創(chuàng)建一個(gè)子進(jìn)程,調(diào)用 setuid() 來(lái)切換到目標(biāo)用戶。
- 最好,它會(huì)在上述子進(jìn)程中執(zhí)行參數(shù)給定的 shell 或命令。
- [2] 理解 sudo 命令授權(quán)配置
- USER/GROUP HOST=(USER[:GROUP]) [NOPASSWD:] COMMANDS
- USER/GROUP: 表示需要被授權(quán)的用戶或者組;如果是組則需要以 % 開頭
- HOST: 表示允許從哪些主機(jī)登錄的用戶運(yùn)行 sudo 命令;ALL 表示允許從任何終端、機(jī)器訪問(wèn)
- (USER[:GROUP]): 表示使用 sudo 可切換的用戶或者組,組可以不指定;ALL 表示可以切換到系統(tǒng)的所有用戶
- NOPASSWD: 如果指定,則該用戶或組使用 sudo 時(shí)不必輸入密碼
- COMMANDS: 表示運(yùn)行指定的命令;ALL 表示允許執(zhí)行所有命令
- # 允許 sudo 組執(zhí)行所有命令
- %sudo ALL=(ALL:ALL) ALL
- # 允許用戶執(zhí)行所有命令,且無(wú)需輸入密碼
- escape ALL =(ALL) NOPASSWD: ALL
- # 僅允許用戶執(zhí)行 echo, ls 命令
- escape ALL =(ALL) NOPASSWD: /bin/echo /bin/ls
- # 運(yùn)行本機(jī)的用戶執(zhí)行關(guān)機(jī)命令
- escape localhost=/sbin/shutdown -h now
- # 允許 users 用戶組中的用戶像 root 用戶一樣使用 mount、unmount、chrom 命令
- %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
- [3] 配置 Defaults 選項(xiàng)
- # 指定用戶嘗試輸入密碼的次數(shù),默認(rèn)值為3
- Defaults passwd_tries=5
- # 設(shè)置密碼超時(shí)時(shí)間,默認(rèn)為 5 分鐘
- Defaults passwd_timeout=2
- 默認(rèn) sudo 詢問(wèn)用戶自己的密碼,添加 targetpw 或 rootpw 配置可以讓 sudo 詢問(wèn) root 密碼
- Defaults targetpw
- # 指定自定義日志文件
- Defaults logfile="/var/log/sudo.log"
- # 要在自定義日志文件中記錄主機(jī)名和四位數(shù)年份,可以加上 log_host 和 log_year 參數(shù)
- Defaults log_host, log_year, logfile="/var/log/sudo.log"
- # 保持當(dāng)前用戶的環(huán)境變量
- Defaults env_keep += "LANG LC_ADDRESS LC_CTYPE COLORS DISPLAY HOSTNAME EDITOR"
- Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"
- # 安置一個(gè)安全的 PATH 環(huán)境變量
- Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
2. 使用技巧總結(jié)
主要介紹使用 sudo 相關(guān)命令的使用技巧和問(wèn)題處理方式!
- [1] 如何將 visudo 編輯器從 nano 更改為 vim?
當(dāng)我使用 visudo 命令的時(shí)候,它總是用 nano 編輯器打開它,對(duì)應(yīng)平時(shí)習(xí)慣使用 vi 或者 vim 的用戶來(lái)說(shuō),顯得有些別扭,以及操作不夠自如。所以,如何更好的將 visudo編輯器從 nano 更改為 vim 就變得非常重要了。
最佳的解決辦法,就是調(diào)用命令,永久的將終端的編輯器更換成為 vim 編輯器,一勞永逸。我們只需要執(zhí)行如下命令,輸入自己需要的編輯器序號(hào)并按下回車鍵即可。下次執(zhí)行visudo 命令的時(shí)候,就會(huì)使用 vim 編輯器打開文件。
- # 因?yàn)?etc/sudoers普通用戶無(wú)法打開和使用
- $ sudo update-alternatives --config editor
- There are 4 choices for the alternative editor (providing /usr/bin/editor).
- Selection Path Priority Status
- ------------------------------------------------------------
- * 0 /bin/nano 40 auto mode
- 1 /bin/ed -100 manual mode
- 2 /bin/nano 40 manual mode
- 3 /usr/bin/vim.basic 30 manual mode
- 4 /usr/bin/vim.tiny 10 manual mode
- Press enter to keep the current choice[*], or type selection number: 3
其次的解決方法,就是通過(guò)環(huán)境變量修改當(dāng)前終端的默認(rèn)編輯器。
- # 在.zshrc或.profile文件中
- $ export EDITOR=vim;
- # 希望執(zhí)行對(duì)visudo生效
- $ sudo EDITOR=vim visudo
- # 或者修改/etc/sudoers文件的默認(rèn)編輯器
- Defaults editor=/usr/bin/vim
- [2] Vim 如何強(qiáng)制保存只讀文件?
在使用 vim 的時(shí)候,當(dāng)以普通用戶打開一個(gè)只有 root 用戶才有權(quán)限操作的文件時(shí),在編輯完成之后保存時(shí)發(fā)現(xiàn),這個(gè)文件沒(méi)有權(quán)限修改。好不容易把文件編輯完了,卻無(wú)法保存,就只能放棄,然后退出,再以 root 權(quán)限打開,重新編輯,是在痛苦!那有沒(méi)有好的方法來(lái)解決這個(gè)問(wèn)題呢?咳咳咳,肯定是有的。
- # Vim命令模式下執(zhí)行即可強(qiáng)制保存
- # w: 表示保存文件
- # !: 表示執(zhí)行外部命令
- # tee: 表示把數(shù)據(jù)重定向到給定文件和屏幕上
- # %: 在執(zhí)行外部命令時(shí),%會(huì)擴(kuò)展成當(dāng)前文件名
- :w !sudo tee %
上述方式非常完美的解決了不能保存只讀文件的問(wèn)題,但畢竟命令還是有些長(zhǎng),為了避免每次輸入一長(zhǎng)串的命令,可以將它映射為一個(gè)簡(jiǎn)單的命令加到 .vimrc 中。這樣,簡(jiǎn)單的運(yùn)行 :w!! 即可,命令后半部分 > /dev/null 作用為顯式的丟掉標(biāo)準(zhǔn)輸出的內(nèi)容。
- " Allow saving of files as sudo when I forgot to start vim using sudo.
- cmap w!! w !sudo tee > /dev/null %
- [3] 如何更加安全地編輯文件?
Sudoedit 是一個(gè)內(nèi)置命令,允許用戶安全地編輯文件。根據(jù) sudo 手冊(cè)頁(yè),sudoedit 等效于使用 -e 命令行選項(xiàng)執(zhí)行 sudo。
那么這個(gè)命令會(huì)做什么呢,它會(huì)首先創(chuàng)建你要編輯的文件的臨時(shí)副本。然后,命令將搜索SUDO_EDITOR,VISUAL 和 EDITOR 環(huán)境變量(按此順序),以確定應(yīng)調(diào)用哪個(gè)編輯器來(lái)打開剛剛創(chuàng)建的臨時(shí)副本。用戶完成修改工作后,更改將復(fù)制回原始文件。
- # Sudo命令手冊(cè)頁(yè)
- -e, --edit
- Edit one or more files instead of running a command. In lieu of a path name,
- the string "sudoedit" is used when consulting the security policy. If the user
- is authorized by the policy, the followingsteps are taken:
- 1. Temporary copies are made of the files to be edited with
- the owner set to the invoking user.
- 2. The editor specified by the policy is run to edit the
- temporary files. The sudoers policy uses the
- SUDO_EDITOR, VISUAL and EDITOR environment variables (in
- that order). If none of SUDO_EDITOR, VISUAL or EDITOR
- are set, the first program listed in the editor
- sudoers(5) option is used.
- 3. If they have been modified, the temporary files are
- copied back to their original location and the temporary
- versions are removed.
- If the specified file does not exist, it will be created. Note that unlike most
- commands run by sudo, the editor is run with the invoking user is environment
- unmodified. If, for some reason, sudo is unable to update a file with its edited
- version, the user will receive a warning and the edited copy will remain in a
- temporary file.
- # Sudo命令手冊(cè)頁(yè)
- -e, --edit
- Edit one or more files instead of running a command. In lieu of a path name,
- the string "sudoedit" is used when consulting the security policy. If the user
- is authorized by the policy, the followingsteps are taken:
- 1. Temporary copies are made of the files to be edited with
- the owner set to the invoking user.
- 2. The editor specified by the policy is run to edit the
- temporary files. The sudoers policy uses the
- SUDO_EDITOR, VISUAL and EDITOR environment variables (in
- that order). If none of SUDO_EDITOR, VISUAL or EDITOR
- are set, the first program listed in the editor
- sudoers(5) option is used.
- 3. If they have been modified, the temporary files are
- copied back to their original location and the temporary
- versions are removed.
- If the specified file does not exist, it will be created. Note that unlike most
- commands run by sudo, the editor is run with the invoking user is environment
- unmodified. If, for some reason, sudo is unable to update a file with its edited
- version, the user will receive a warning and the edited copy will remain in a
- temporary file.
- [4] 如何讓 sudo 會(huì)話時(shí)間隨心所欲?
其中 sudo 命令是權(quán)限委派的命令,在生產(chǎn)環(huán)境中是非常常用的,默認(rèn)情況下 sudo 命令會(huì)話時(shí)間是在 15 分鐘。要設(shè)置 sudo 密碼超時(shí)的值,需要使用 passwd_timeout 參數(shù)進(jìn)行設(shè)置。
可以以分鐘設(shè)置為你所需的任何時(shí)間,它會(huì)在超時(shí)之前一直等待。如果要為每個(gè)執(zhí)行的 sudo 命令彈出密碼提示,你也可以將時(shí)間設(shè)置為 0,或者通過(guò)設(shè)置值 -1 永久禁用密碼提示。
- # 設(shè)置timeout時(shí)間
- # 意味著sudo密碼提示將會(huì)在用戶使用20分鐘后過(guò)期
- Defaults env_reset,timestamp_timeout=20
- [5] 如何更加安全的授權(quán)服務(wù)器權(quán)限?
如何我們管理的服務(wù)器,開發(fā)或者其他人員需要登錄該服務(wù)器,進(jìn)行環(huán)境調(diào)試或者問(wèn)題復(fù)現(xiàn)等情況。這時(shí),就需要我們給對(duì)應(yīng)的用戶開通登錄的訪問(wèn)權(quán)限。但是如果我們直接編輯/etc/sudoers 文件的話,之后當(dāng)對(duì)方使用完成之后我就還需要手動(dòng)進(jìn)行清理。如果我們忘記的話,該開發(fā)或者其他人員將一直可以登錄該服務(wù)器,會(huì)有一定程度的安全問(wèn)題。
不幸的是,對(duì)應(yīng)臨時(shí)授權(quán)的話,/etc/sudoers 文件中沒(méi)有對(duì)應(yīng)的配置,可以對(duì)某個(gè)用戶或者用戶組進(jìn)行指定范圍的時(shí)間授權(quán)。當(dāng)用戶到達(dá)指定時(shí)間點(diǎn)之后,將拒絕該用戶再次進(jìn)行登錄了。對(duì)應(yīng)此種情況,我們可以通過(guò) crontab 定時(shí)任務(wù)與 /etc/sudoers.d 目錄的機(jī)制可以完美的解決上述問(wèn)題。
我們通過(guò)定時(shí)任務(wù)的定時(shí)執(zhí)行目錄,來(lái)定時(shí)刷掉 /etc/sudoers.d/ 目錄下的用戶或者用戶組的授權(quán)配置文件。比如,我們需要定時(shí)每日刷掉今日臨時(shí)授權(quán)的用戶或者用戶,可以在/etc/cron.daily 目錄下面創(chuàng)建用于刪除 /etc/sudoers.d/ 目錄的 rm -rf 命令,之后在固定的時(shí)間會(huì)自動(dòng)刪除。對(duì)應(yīng)授權(quán)用戶,我們使用在 /etc/sudoers.d/ 目錄下創(chuàng)建單獨(dú)的配置文件,而不是直接修改 /etc/sudoers 文件。
- # Crontab有多種定時(shí)機(jī)制
- # 下述分別表示每天、每時(shí)、每月、每周定時(shí)執(zhí)行
- $ ls -dl /etc/cron.* | grep -v cron.d$
- drwxr-xr-x 2 root root 4096 May 15 06:18 /etc/cron.daily
- drwxr-xr-x 2 root root 4096 Feb 14 2019 /etc/cron.hourly
- drwxr-xr-x 2 root root 4096 Feb 14 2019 /etc/cron.monthly
- drwxr-xr-x 2 root root 4096 Jun 18 09:57 /etc/cron.weekly
- # 創(chuàng)建單獨(dú)的授權(quán)配置文件
- $ ls -lh /etc/sudoers.d/
- -r--r----- 1 root root 666 Oct 6 2017 lisi
- -r--r----- 1 root root 958 Jan 18 2018 zhangsan
- # 查看授權(quán)配置文件的內(nèi)容
- $ cat /etc/sudoers.d/zhangsan
- ALL ALL = (root) NOPASSWD: zhangsan
- [6] 如何解決 sudo 命令找不到環(huán)境變量?
我們?nèi)粘T谑褂?sudo 命令的時(shí)候,常常會(huì)遇到,當(dāng)切換用戶之后,發(fā)現(xiàn)之前設(shè)置的環(huán)境變量怎么不見(jiàn)了呢?這是因?yàn)?,我們?zhí)行 sudo 命令之后會(huì)切換用戶,如果保留環(huán)境變量會(huì)有一定的安全問(wèn)題,系統(tǒng)會(huì)默認(rèn)重置環(huán)境變量為安全的環(huán)境變量。先前設(shè)置的變量都會(huì)失效,只有少數(shù)配置文件中指定的環(huán)境變量能夠保存下來(lái)。
我們可以看一下 sudo 配置文件 /etc/sudoers 來(lái)找找作用原因。我們執(zhí)行如下命令之后,可以看到如下輸入(有可能和我這里的不一樣)。其中 env_reset 表示默認(rèn)會(huì)重置環(huán)境變量,因此我們自定義的變量會(huì)在 sudo 命令執(zhí)行之后失效,也就不會(huì)正確獲取變量值了。而 env_keep 則表示用于保留部分環(huán)境變量不被重置,需要保留的變量就寫入雙引號(hào)之中,可自行追加需要保留的變量。最后就是 secure_path 變量,其作用就是包含的路徑將被當(dāng)做 sudo 環(huán)境的 PATH 變量來(lái)使用。如果在 sudo 環(huán)境無(wú)法找到某些命令,那么可以將這些命令的路徑加入該配置項(xiàng)之中。
- $ sudo sed '/^#/d;/^$/d' /etc/sudoers
- Defaults env_reset
- Defaults env_keep = "COLORS IDSPLAY HOSTNAME HISTSIZE LS_COLORS"
- Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS"
- Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
我們知道原因之后,就可以針對(duì)上述情況作出不用的處理方式,來(lái)解決 sudo 命令找不到環(huán)境變量的問(wèn)題。
第一種解決方法,就是在使用的時(shí)候,使用 -E 參數(shù)。加上 -E 選項(xiàng)后,用戶可以在sudo 執(zhí)行時(shí)保留當(dāng)前用戶已存在的環(huán)境變量,不會(huì)被 sudo 重置。另外,如果用戶對(duì)于指定的環(huán)境變量沒(méi)有權(quán)限,則會(huì)報(bào)錯(cuò)。需要注意的是,在內(nèi)部測(cè)試機(jī)器中,安全性要求不高的情況下使用。
- $ sudo sudo -E
第二種解決方法,就是修改 sudo 配置文件??梢酝ㄟ^(guò)修改 /etc/sudoers 文件的 env_keep 和 secure_path 配置項(xiàng),來(lái)指定 sudo 環(huán)境中需要保留的環(huán)境變量和路徑。當(dāng)然,我們也可以把配置文件的變量 !env_reset 給去掉,這樣就不會(huì)有限制了。
- $ sudo vim /etc/sudoers
- Defaults !env_reset
3. 參考鏈接地址
- 如何解決 sudo 命令找不到環(huán)境變量的問(wèn)題