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的用戶
首先寫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的全部權限
配置示例:
############################################################################ # 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 ###############################################################