CentOS下su和sudo命令對比
在CentOS系統中,由於root的許可權過大,一般情況都不使用它。只有在一些特殊情況下才採用登入root執行管理工作,一般情況下臨時使用root許可權多採用su和sudo命令。
一、su和sudo命令對比:
在普通使用者下輸入su命令後,會提示輸入root賬戶的密碼,然後就進入特權模式(跟用root登入系統完全一樣),輸入exit或者su - user 退出:
$su
Password:
#ls /root
anaconda-ks.cfg install.log install.log.syslog
#exit
$ls /root
ls: cannot open directory /root: Permission denied #提示沒有許可權 【Linux公社 http://www.linuxidc.com 】
而採用sudo命令,只需輸入目前使用者的密碼(也可以配置為不輸入密碼)即可執行需要root許可權執行的命令:
$ls /root
ls: cannot open directory /root: Permission denied #提示沒有許可權
$sudo ls /root
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password forOracle: #輸入普通使用者oracle的密碼
anaconda-ks.cfg install.log install.log.syslog
通過上面的對比可以看出,sudo比su有很多優點:
1、普通使用者不需要知道root的密碼即可執行需要root許可權的命令;
2、不會因忘記退出而採用root執行了會引起破壞性的命令(初學linux經常犯這個錯);
二、配置普通使用者有使用sudo命令的許可權:
在linux系統中,建立使用者並沒有執行sudo許可權,如建立一個AAA的使用者,輸入sudo命令會有如下提示:
aaa is not in the sudoers file. This incident will be reported.
這句的意思是在sudoers檔案中不存在aaa這個使用者,這個時間會被報告給管理員。
既然知道問題處於sudoers這個檔案,那我們就看看這個檔案為何方神聖:
[root@dbs aaa]# vi /etc/sudoers
## 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
輸入i,對檔案進行編輯,發現底部有唯讀提示:
-- INSERT -- W10: Warning: Changing a readonly file
應該是沒有許可權,退出編輯狀態後查看許可權:
[root@dbs aaa]# ll /etc/sudoers
-r--r-----. 1 root root 3825 Jul 22 01:05 /etc/sudoers
原來root也只有唯讀許可權,難怪,首先修改許可權以讓root有完全控制許可權:
[root@dbs aaa]#chmod 740 /etc/sudoers
再次編輯,這次沒有出現唯讀提示了,找到如下欄位:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
Oracle ALL=(ALL) ALL
aaa ALL=(ALL) ALL
添加aaa使用者,添加後如aaa ALL=(ALL) ALL
修改完後輸入:wq儲存,儲存後記得把許可權修改回去,否則會有錯誤提示
#chmod 440 /etc/sudoers
許可權一定是440,否則就會有錯誤提示。
OK,在aaa使用者下就可以正常使用sudo命令咯。
(完)