詳解Linux下的sudo及其配置文件/etc/sudoers的詳細配置
詳解Linux下的sudo及其配置文件/etc/sudoers的詳細配置
1.sudo介紹
sudo是linux下常用的允許普通用戶使用超級用戶權限的工具,允許系統(tǒng)管理員讓普通用戶執(zhí)行一些或者全部的root命令,如halt,reboot,su等等。這樣不僅減少了root用戶的登陸 和管理時間,同樣也提高了安全性。Sudo不是對shell的一個代替,它是面向每個命令的。
它的特性主要有這樣幾點:
§ sudo能夠限制用戶只在某臺主機上運行某些命令。
§ sudo提供了豐富的日志,詳細地記錄了每個用戶干了什么。它能夠?qū)⑷罩緜鞯街行闹鳈C或者日志服務器。
§ sudo使用時間戳文件來執(zhí)行類似的“檢票”系統(tǒng)。當用戶調(diào)用sudo并且輸入它的密碼時,用戶獲得了一張存活期為5分鐘的票(這個值可以在編譯的時候改變)。
§ sudo的配置文件是sudoers文件,它允許系統(tǒng)管理員集中的管理用戶的使用權限和使用的主機。它所存放的位置默認是在/etc/sudoers,屬性必須為0411。
2.配置文件/etc/sudoers
它的主要配置文件是sudoers,linux下通常在/etc目錄下,如果是solaris,缺省不裝sudo的,編譯安裝后通常在安裝目錄的 etc目錄下,不過不管sudoers文件在哪兒,sudo都提供了一個編輯該文件的命令:visudo來對該文件進行修改。強烈推薦使用該命令修改 sudoers,因為它會幫你校驗文件配置是否正確,如果不正確,在保存退出時就會提示你哪段配置出錯的。
言歸正傳,下面介紹如何配置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. 最簡單的配置,讓普通用戶support具有root的所有權限
執(zhí)行visudo之后,可以看見缺省只有一條配置:
root ALL=(ALL) ALL
那么你就在下邊再加一條配置:
support ALL=(ALL) ALL
這樣,普通用戶support就能夠執(zhí)行root權限的所有命令
以support用戶登錄之后,執(zhí)行:
sudo su -
然后輸入support用戶自己的密碼,就可以切換成root用戶了
2. 讓普通用戶support只能在某幾臺服務器上,執(zhí)行root能執(zhí)行的某些命令
首先需要配置一些Alias,這樣在下面配置權限時,會方便一些,不用寫大段大段的配置。Alias主要分成4種
Host_Alias Cmnd_Alias User_Alias Runas_Alias
1) 配置Host_Alias:就是主機的列表
Host_Alias HOST_FLAG = hostname1, hostname2, hostname3
2) 配置Cmnd_Alias:就是允許執(zhí)行的命令的列表
Cmnd_Alias COMMAND_FLAG = command1, command2, command3
3) 配置User_Alias:就是具有sudo權限的用戶的列表
User_Alias USER_FLAG = user1, user2, user3
4) 配置Runas_Alias:就是用戶以什么身份執(zhí)行(例如root,或者oracle)的列表
Runas_Alias RUNAS_FLAG = operator1, operator2, operator3
5) 配置權限
配置權限的格式如下:
USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG
如果不需要密碼驗證的話,則按照這樣的格式來配置
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 ##################################################
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
用shell腳本在mysql表中批量插入數(shù)據(jù)的方法
這篇文章主要介紹了用shell腳本在mysql表中批量插入數(shù)據(jù)的方法,需要的朋友可以參考下2014-03-03shell腳本學習指南[二](Arnold Robbins & Nelson H.F. Beebe著)
這篇文章主要介紹了shell腳本學習指南[二](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以參考下2014-02-02Impala-shell命令參數(shù)的實現(xiàn)
這篇文章主要介紹了Impala-shell命令參數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12