su ,sudo

來源:互聯網
上載者:User

標籤:使用者登入   恰恰   最好   

一、su

su命令就是切換使用者的工具,怎麼理解呢?比如我們以普通使用者beinan登入的,但要添加使用者任務,執行useradd ,beinan使用者沒有這個許可權,而這個許可權恰恰由root所擁有。解決辦法無法有兩個,一是退出beinan使用者,重新以root使用者登入,但這種辦法並不是最好的;二是我們沒有必要退出beinan使用者,可以用su來切換到root下進行添加使用者的工作,等任務完成後再退出root。我們可以看到當然通過su 切換是一種比較好的辦法;

通過su可以在使用者之間切換,如果超級許可權使用者root向普通或虛擬使用者切換不要求輸入密碼,什麼是權力?這就是!而普通使用者切換到其它任何使用者都要求輸入密碼驗證;

2、su 的用法:

su [OPTION選項參數] [使用者]

-, -l, ——login 登入並改變到所切換的使用者環境;

-c, ——commmand=COMMAND 執行一個命令,然後退出所切換到的使用者環境;

二、sudo

root使用者權力無限大,基於安全和生產各方面考慮,root賬戶一般不允許使用,但是很多命令沒有不是root使用者無法運行。這裡就需要sudo來幫忙了。

1)檔案/etc/sudoers

[[email protected] tls]# ls -l /etc/sudoers-r--r-----. 1 root root 4002 Mar  1  2012 /etc/sudoers[[email protected] tls]# lsattr /etc/sudoers-------------e- /etc/sudoers

想要編輯就需要#visudo

[[email protected] tls]# visudo## Sudoers allows particular users to run various commands as## the root user, without needing the root password.#### Examples are provided at the bottom of the file for collections## of related commands, which can then be delegated out to particular## users or groups.#### This file must be edited with the ‘visudo‘ command.## Host Aliases## Groups of machines. You may prefer to use hostnames (perhaps using## wildcards for entire domains) or IP addresses instead.# Host_Alias     FILESERVERS = fs1, fs2# Host_Alias     MAILSERVERS = smtp, smtp2## User Aliases## These aren‘t often necessary, as you can use regular groups## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname## rather than USERALIAS# User_Alias ADMINS = jsmith, mikem## Command Aliases## These are groups of related commands...## Networking# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool## Installation and management of software# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum## Services# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig

 

2)格式:

who                       where=(whom)                         command

  1. username  ALL=(ALL)   ALL //為使用者"username"授予sudo訪問權
  2. %users   ALL=(ALL)   ALL //為屬於wheel使用者組的所有使用者授予sudo訪問權
sudo [-klv]參數:-b在後台執行指令-h 顯示協助資訊-H將HOME環境變數設為新身份的HOME環境變數-k結束密碼的有效期間限,也就是下次再執行sudo時,便需要輸入密碼-l列出目前使用者可執行與無法執行的指令-p改變詢問密碼的提示符號-s執行指定的shell-u<使用者>以指定的使用者作為新的身份,若不加上此參數,則預設以root作為新的身份-v延長密碼有效期間限5分鐘-V顯示版本資訊-S從標準輸入資料流替代終端擷取密碼

3.用法:

%GroupName 對組中的使用者授權

 

sudoers檔案支援使用別名對同類對象進行分組,組名必須使用全大寫字母

Host_Alias;User_Alias;Runas_Alias;Cmnd_Alias

eg.

Cmnd_Alias USERADMINCMNDS = /usr/sbin/useradd,/usr/sbin/usermod,/usr/sbin/groupadd,/usr/bin/passwd,!/usr/bin/passwd root

User_Alias USERADMINS = tom,cloud,jerry

Host_Alias USERHOSTS =192.168.0.1/24

USERADMINS                USERHOSTS=(root)                         USERADMINCMNDS

 

註:添加命令時,若需要授權修改使用者密碼時,需要禁止修改root使用者密碼

/usr/bin.passwd [a-zA-Z]*, !/usr/bin/passwd root

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.