詳解Linux下的sudo及其配置文件/etc/sudoers的詳細(xì)配置
詳解Linux下的sudo及其配置文件/etc/sudoers的詳細(xì)配置
1.sudo介紹
sudo是linux下常用的允許普通用戶使用超級(jí)用戶權(quán)限的工具,允許系統(tǒng)管理員讓普通用戶執(zhí)行一些或者全部的root命令,如halt,reboot,su等等。這樣不僅減少了root用戶的登陸 和管理時(shí)間,同樣也提高了安全性。Sudo不是對(duì)shell的一個(gè)代替,它是面向每個(gè)命令的。
它的特性主要有這樣幾點(diǎn):
§ sudo能夠限制用戶只在某臺(tái)主機(jī)上運(yùn)行某些命令。
§ sudo提供了豐富的日志,詳細(xì)地記錄了每個(gè)用戶干了什么。它能夠?qū)⑷罩緜鞯街行闹鳈C(jī)或者日志服務(wù)器。
§ sudo使用時(shí)間戳文件來(lái)執(zhí)行類似的“檢票”系統(tǒng)。當(dāng)用戶調(diào)用sudo并且輸入它的密碼時(shí),用戶獲得了一張存活期為5分鐘的票(這個(gè)值可以在編譯的時(shí)候改變)。
§ sudo的配置文件是sudoers文件,它允許系統(tǒng)管理員集中的管理用戶的使用權(quán)限和使用的主機(jī)。它所存放的位置默認(rèn)是在/etc/sudoers,屬性必須為0411。
2.配置文件/etc/sudoers
它的主要配置文件是sudoers,linux下通常在/etc目錄下,如果是solaris,缺省不裝sudo的,編譯安裝后通常在安裝目錄的 etc目錄下,不過(guò)不管sudoers文件在哪兒,sudo都提供了一個(gè)編輯該文件的命令:visudo來(lái)對(duì)該文件進(jìn)行修改。強(qiáng)烈推薦使用該命令修改 sudoers,因?yàn)樗鼤?huì)幫你校驗(yàn)文件配置是否正確,如果不正確,在保存退出時(shí)就會(huì)提示你哪段配置出錯(cuò)的。
言歸正傳,下面介紹如何配置sudoers
首先寫sudoers的缺省配置:
############################################################# # sudoers file. # # This file MUST be edited with the 'visudo' command as root. # # See the sudoers man page for the details on how to write a sudoers file. # # Host alias specification # User alias specification # Cmnd alias specification # Defaults specification # User privilege specification root ALL=(ALL) ALL # Uncomment to allow people in group wheel to run all commands # %wheel ALL=(ALL) ALL # Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL # Samples # %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom # %users localhost=/sbin/shutdown -h now ##################################################################
1. 最簡(jiǎn)單的配置,讓普通用戶support具有root的所有權(quán)限
執(zhí)行visudo之后,可以看見缺省只有一條配置:
root ALL=(ALL) ALL
那么你就在下邊再加一條配置:
support ALL=(ALL) ALL
這樣,普通用戶support就能夠執(zhí)行root權(quán)限的所有命令
以support用戶登錄之后,執(zhí)行:
sudo su -
然后輸入support用戶自己的密碼,就可以切換成root用戶了
2. 讓普通用戶support只能在某幾臺(tái)服務(wù)器上,執(zhí)行root能執(zhí)行的某些命令
首先需要配置一些Alias,這樣在下面配置權(quán)限時(shí),會(huì)方便一些,不用寫大段大段的配置。Alias主要分成4種
Host_Alias Cmnd_Alias User_Alias Runas_Alias
1) 配置Host_Alias:就是主機(jī)的列表
Host_Alias HOST_FLAG = hostname1, hostname2, hostname3
2) 配置Cmnd_Alias:就是允許執(zhí)行的命令的列表
Cmnd_Alias COMMAND_FLAG = command1, command2, command3
3) 配置User_Alias:就是具有sudo權(quán)限的用戶的列表
User_Alias USER_FLAG = user1, user2, user3
4) 配置Runas_Alias:就是用戶以什么身份執(zhí)行(例如root,或者oracle)的列表
Runas_Alias RUNAS_FLAG = operator1, operator2, operator3
5) 配置權(quán)限
配置權(quán)限的格式如下:
USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG
如果不需要密碼驗(yàn)證的話,則按照這樣的格式來(lái)配置
USER_FLAG HOST_FLAG=(RUNAS_FLAG) NOPASSWD: COMMAND_FLAG
配置示例:
############################################################################ # sudoers file. # # This file MUST be edited with the 'visudo' command as root. # # See the sudoers man page for the details on how to write a sudoers file. # # Host alias specification Host_Alias EPG = 192.168.1.1, 192.168.1.2 # User alias specification # Cmnd alias specification Cmnd_Alias SQUID = /opt/vtbin/squid_refresh, /sbin/service, /bin/rm # Defaults specification # User privilege specification root ALL=(ALL) ALL support EPG=(ALL) NOPASSWD: SQUID # Uncomment to allow people in group wheel to run all commands # %wheel ALL=(ALL) ALL # Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL # Samples # %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom # %users localhost=/sbin/shutdown -h now ##################################################
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
shell 命令統(tǒng)計(jì)代碼行數(shù)的簡(jiǎn)單代碼
這篇文章主要介紹了shell 命令統(tǒng)計(jì)代碼行數(shù)的簡(jiǎn)單代碼,文中給大家提到了統(tǒng)計(jì)行數(shù)、字符數(shù)、字節(jié)數(shù)的shell腳本 ,需要的朋友可以參考下2018-04-04
linux shell腳本基礎(chǔ)知識(shí)學(xué)習(xí)
這篇文章主要介紹linux shell腳本基礎(chǔ)知識(shí),學(xué)習(xí)linux shell編程的朋友可以參考下2013-03-03
Bash 腳本實(shí)現(xiàn)每次登錄到 Shell 時(shí)可以查看 Linux 系統(tǒng)信息
這篇文章主要介紹了Bash 腳本實(shí)現(xiàn)每次登錄到 Shell 時(shí)可以查看 Linux 系統(tǒng)信息,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-12-12
shell腳本源碼安裝nginx的詳細(xì)過(guò)程
這篇文章主要介紹了shell腳本源碼安裝nginx,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10

