Linux sudo詳解


sudo:控制用戶對系統命令的使用權限,root允許的操作。通過sudo可以提高普通用戶的操作權限,不過這個權限是需要進行配置才可使用。

常用的命令展示

配置sudo的2種方式

1. visodu  編輯
   visudo: 編輯sudo配置文件(相當於 vi /etc/sudoers,但是不建議用,注意檢查語法) 
       visudo      編輯/etc/sudoers命令  ->98行                         
       visudo -c   檢查語法命令,有報錯可能導致系統起不來,所以編輯之前需要備份,編輯后最好檢查一下用法      
  ll   /etc/sudoers  這個是一個權限很小的文件(440)
2. vi /etc/sudoers(98gg 可以快速跳轉)  但是visudo功能更強大
Syntax:    user    MACHINE=COMMANDS      # sudo 語法
root    ALL=(ALL)       ALL              #  (All)表示允許用戶以哪個用戶的權限做事情
omd     ALL=(ALL)       ALL              #  omd用戶在任何機器上,可以只需任何用戶的任何命令 == root用戶
omd     ALL=(ALL)     NOPASSWD: ALL      #  免密而且omd用戶在任何機器上,可以只需任何用戶的任何命令
ftl     ALL=(ALL)   /bin/cp,/bin/touch   # 只允許ftl用戶只需root用戶的cp,touch命令

image

取消sudo必須需要tty才能執行的限制

 編輯 /etc/sudoers ,找到 Defaults    requiretty, 然后注釋掉這行: 

image

: /etc/sudoers  這個是一個權限很小的文件(440)

image

sudo基本的操作 

sudo omd ifconfig    ==>omd用戶只需root權限需要輸入密碼
[test@oldboy ~] $ sudo su - omd
    # 通過test用戶切換到omd,所以此時輸入的是test用戶的密碼
    test用戶下:su - root -c "ifconfig"   需要輸入密碼常用的參數:
sudo常用參數:
     -l : 登錄用戶下面,執行sudo -l  顯示當前用戶有哪些權限
     -k :刪除/var/db/sudo/下面對應的時間戳的信息,下次執行sudo需要輸入當前用戶的密碼
                --> 系統默認也是5分鍾失效
                --> 配置免密是另一種情況  NOPASSWD: ALL遠程sudo(有條件限制的)
sudo的原創操作:    
ssh –p lemFTL@HHH omd@10.105.163.162 sudo ls /root

sudo別名的應用

 1.主機別名

    # Host_Alias     FILESERVERS = fs1, fs2       -->注意逗號,空格
    # Host_Alias     MAILSERVERS = smtp, smtp2      -->注意逗號,空格
2.用戶別名
    # User_Alias ADMINS = jsmith, mikem,%omd  -->omd的用戶組添加上去
3.命令別名
    # Cmnd_Alias NETWORKING = /sbin/route, /sbin/iwconfig, /sbin/mii-tool
4.角色別名   -->運行時可以切換的角色
    #Runas_Alias OP = root,omd
5.實例
     user            MACHINE                         COMMANDS
    ADMINS     FILESERVERS=(Runas_Alias)            ALL    
      包含2個用戶的ADMINS
      可以再包含2個機器的FILESERVERS
      切換到包含2個角色別名Runas_Alias的服務器上,
      執行包含3個命令的Cmnd_Alias                   
另:不切換用戶而直接執行命令:

sudo原理

普通用戶運行sudo -> 檢查/var/db/sudo/下是否有時間戳文件(執行成功后會創建,且免密5分鍾),並檢查是否已經過期
                    未過期    -> 檢查/etc/sudoers里面是否配置了運行sudo和執行相應命令的權限
                                ->有權限 ->執行命令並且返回結果 ->退出
                                ->無權限 ->退出
                      過期  -> 輸入當前用戶自己的口令 ->檢查/etc/sudoers里面是否配置了運行sudo和執行相應命令的權限
                                ->有權限 ->執行命令並且返回結果 ->退出
                                ->無權限 ->退出

sudo配置的注意事項

1.命令別名下的成員必須是文件或者是目錄的絕對路徑
    2.別名是包含大寫字母,數字,下划線,如果是字母就必須全部大寫
    3.一個別名下有多個成員,成員與成員之間必須使用逗號分隔,且成員必須有效的存在
    4.別名成員受Host_Alias,User_Alias,Cmnd_Alias,Runas_Alias別名類型制約,定義什么類型的別名,就要有什么類型的成員匹配
    5.別名規則是每行算一個規則,如果一個別名規則一行容不下是,可以通過“\”來續行
    6.指定切換的用戶要用()括起來,如果省略括號,默認是root   
    7.如果不需要輸入密碼可以輸入NOPASSWD:ALL
    8.禁止的命令可以通過!來進行限制,根據測試,先執行后面的命令,
        所以禁止的命令必須放在允許執行的命令的后面
    9.用戶組前面必須用%號   
    10.所有的授權ALL字符都必須是大寫
    11.允許執行的命令都是有順序的,命令的順序是從后向前生效
        所以禁止的命令盡量向后放

sudo管理的2個文件

 /var/db/sudo/
    /etc/sudoers
    man sudo
    man sudoers

sudo實例--企業生產環境用戶權限集中管理方案實例

【更多參考】sudo實例--企業生產環境用戶權限集中管理方案實例

visudo的參數

The options are as follows:

     -c          Enable check-only mode.  The existing sudoers file will be checked for syntax errors, owner
                 and mode.  A message will be printed to the standard output describing the status of
     -c          Enable check-only mode.  The existing sudoers file will be checked for syntax errors, owner
                 and mode.  A message will be printed to the standard output describing the status of
                 sudoers unless the -q option was specified.  If the check completes successfully, visudo
                 will exit with a value of 0.  If an error is encountered, visudo will exit with a value of
                 1.

     -f sudoers  Specify and alternate sudoers file location.  With this option visudo will edit (or check)
                 the sudoers file of your choice, instead of the default, /etc/sudoers.  The lock file used
                 is the specified sudoers file with “.tmp” appended to it.  In check-only mode only, the
                 argument to -f may be ‘-’, indicating that sudoers will be read from the standard input.

     -h          The -h (help) option causes visudo to print a short help message to the standard output and
                 exit.

     -q          Enable quiet mode.  In this mode details about syntax errors are not printed.  This option
                 is only useful when combined with the -c option.

     -s          Enable strict checking of the sudoers file.  If an alias is used before it is defined,
                 visudo will consider this a parse error.  Note that it is not possible to differentiate
                 between an alias and a host name or user name that consists solely of uppercase letters,
                 digits, and the underscore (‘_’) character.

     -V          The -V (version) option causes visudo to print its version number and exit.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM