Linux下的sudo及其配置文件/etc/sudoers的詳細配置說明

Linux下的sudo及其配置文件/etc/sudoers的詳細配置說明

1.sudo介紹

sudo是linux下經常使用的容許普通用戶使用超級用戶權限的工具,容許系統管理員讓普通用戶執行一些或者所有的root命令,如 halt,reboot,su等等。這樣不只減小了root用戶的登錄 和管理時間,一樣也提升了安全性。Sudo不是對shell的一個代替,它是面向每一個命令的。它的特性主要有這樣幾點: linux

  § sudo可以限制用戶只在某臺主機上運行某些命令。 shell

  § sudo提供了豐富的日誌,詳細地記錄了每一個用戶幹了什麼。它可以將日誌傳到中心主機或者日誌服務器。 安全

  § sudo使用時間戳文件來執行相似的「檢票」系統。當用戶調用sudo而且輸入它的密碼時,用戶得到了一張存活期爲5分鐘的票(這個值能夠在編譯的時候改變)。 服務器

  § sudo的配置文件是sudoers文件,它容許系統管理員集中的管理用戶的使用權限和使用的主機。它所存放的位置默認是在/etc/sudoers,屬性必須爲0411。 oracle

2. sudo的使用 工具

       權限:在 /etc/sudoers 中有出現的使用者 ui

  使用方式:sudo -V 日誌

  sudo -h code

  sudo -l blog

  sudo -v

  sudo -k

  sudo -s

  sudo -H

  sudo [ -b ] [ -p prompt ] [ -u username/#uid] -s

  用法:sudo command

  說明:以系統管理者的身份執行指令,也就是說,經由 sudo 所執行的指令就好像是 root 親自執行

  參數:

  -V 顯示版本編號

  -h 會顯示版本編號及指令的使用方式說明

  -l 顯示出本身(執行 sudo 的使用者)的權限

  -v 由於 sudo 在第一次執行時或是在 N 分鐘內沒有執行(N 預設爲五)會問密碼,這個參數是從新作一次確認,若是超過 N 分鐘,也會問密碼

  -k 將會強迫使用者在下一次執行 sudo 時問密碼(不論有沒有超過 N 分鐘)

  -b 將要執行的指令放在背景執行

  -p prompt 能夠更改問密碼的提示語,其中 %u 會代換爲使用者的賬號名稱, %h 會顯示主機名稱

  -u username/#uid 不加此參數,表明要以 root 的身份執行指令,而加了此參數,能夠以 username 的身份執行指令(#uid 爲該 username 的使用者號碼)

  -s 執行環境變數中的 SHELL 所指定的 shell ,或是 /etc/passwd 裏所指定的 shell

  -H 將環境變數中的 HOME (家目錄)指定爲要變動身份的使用者家目錄(如不加 -u 參數就是系統管理者 root )

  command 要以系統管理者身份(或以 -u 更改成其餘人)執行的指令

  範例:

  sudo -l 列出目前的權限

  sudo -V 列出 sudo 的版本資訊

  指令名稱:sudoers(在fc5下顯示不能找到此命令,但用man能夠查到其用法。)

  用來顯示可使用sudo的用戶

3.配置文件/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的全部權限
執行visudo以後,能夠看見缺省只有一條配置:
root    ALL=(ALL) ALL
那麼你就在下邊再加一條配置:
support ALL=(ALL) ALL

這樣,普通用戶support就可以執行root權限的全部命令

以support用戶登陸以後,執行:
sudo su -
而後輸入support用戶本身的密碼,就能夠切換成root用戶了

2. 讓普通用戶support只能在某幾臺服務器上,執行root能執行的某些命令
首先須要配置一些Alias,這樣在下面配置權限時,會方便一些,不用寫大段大段的配置。Alias主要分紅4種
Host_Alias
Cmnd_Alias
User_Alias
Runas_Alias

1) 配置Host_Alias:就是主機的列表
Host_Alias      HOST_FLAG = hostname1, hostname2, hostname3
2) 配置Cmnd_Alias:就是容許執行的命令的列表
Cmnd_Alias      COMMAND_FLAG = command1, command2, command3
3) 配置User_Alias:就是具備sudo權限的用戶的列表
User_Alias USER_FLAG = user1, user2, user3
4) 配置Runas_Alias:就是用戶以什麼身份執行(例如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
###############################################################
相關文章
相關標籤/搜索