溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何分析Linux下的sudo及其配置文件/etc/sudoers的配置

發(fā)布時(shí)間:2022-01-21 14:19:03 來(lái)源:億速云 閱讀:157 作者:柒染 欄目:開發(fā)技術(shù)

這篇文章的內(nèi)容主要圍繞如何分析Linux下的sudo及其配置文件/etc/sudoers的配置進(jìn)行講述,文章內(nèi)容清晰易懂,條理清晰,非常適合新手學(xué)習(xí),值得大家去閱讀。感興趣的朋友可以跟隨小編一起閱讀吧。希望大家通過(guò)這篇文章有所收獲!


1.sudo介紹

如何分析Linux下的sudo及其配置文件/etc/sudoers的配置

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ì)“如何分析Linux下的sudo及其配置文件/etc/sudoers的配置”這一問(wèn)題有一定的了解,快去動(dòng)手實(shí)踐吧,如果想了解更多相關(guān)知識(shí)點(diǎn),可以關(guān)注億速云網(wǎng)站!小編會(huì)繼續(xù)為大家?guī)?lái)更好的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI