技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

Linux下的sudo及其配置文件/etc/sudoers的詳細(xì)配置教程

來源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-10-22 06:22┆點(diǎn)擊:

  詳解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í)間戳文件來執(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目錄下,不過不管sudoers文件在哪兒,sudo都提供了一個(gè)編輯該文件的命令:visudo來對(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=http://www.3lian.com/sbin/mount /cdrom,/sbin/umount /cdrom

  # %users localhost=http://www.3lian.com/sbin/shutdown -h now

  ##################################################################

  1. 最簡單的配置,讓普通用戶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)證的話,則按照這樣的格式來配置

  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=http://www.3lian.com/sbin/mount /cdrom,/sbin/umount /cdrom

  # %users localhost=http://www.3lian.com/sbin/shutdown -h now

  ##################################################