su命令 、sudo命令 和限制root遠程登陸

su命令

su命令是用來切換用戶的,其格式爲:su - username 經常使用的選項以下:less

   -l,##login 登陸並改變到所切換的用戶環境;ssh

  -c,##commmand=COMMAND 執行一個命令,而後退出所切換到的用戶環境;ui

[lichao@test-02 ~]$ ls /root
ls: 沒法打開目錄/root: 權限不夠
[lichao@test-02 ~]$ su - root
密碼:
上一次登陸:五 12月 29 02:37:30 CST 2017從 192.168.101.1pts/0 上
[root@test-02 ~]# ls /root
anaconda-ks.cfg
[root@test-02 ~]# 登出
[lichao@test-02 ~]$ su - root -c ls /root
密碼:
anaconda-ks.cfg
[lichao@test-02 ~]$

當使用普通用戶切換root的時候,須要輸入root的密碼,使用root切換到普通用戶則不須要密碼。rest

sudo命令

受權普通用戶擁有別的用戶的權限,大部分時候是受權普通用戶擁有root用戶的權限,要使用這個命令,須要編輯配置文件,etc/sudoers,可是直接編輯這個文件出錯後系統不會報錯,因此咱們用visudo命令來編輯,若是編輯有問題,系統會提示錯誤code

##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
lichao  ALL=(ALL)       /usr/bin/ls, /usr/bin/cat, /usr/bin/more

這樣編輯以後,lichao 就能夠擁有root的ls,cat ,more 的權限了,使用方式以下ci

[lichao@test-02 ~]$ ls /root
ls: 沒法打開目錄/root: 權限不夠
[lichao@test-02 ~]$ sudo ls /root
1.txt  anaconda-ks.cfg
[lichao@test-02 ~]$ cat /root/1.txt
cat: /root/1.txt: 權限不夠
[lichao@test-02 ~]$ sudo cat /root/1.txt
aaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeefffffffffffffffffffffggggggggggggg
hhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkkkkkllllllllllllllllll
[lichao@test-02 ~]$ less /root/1.txt
/root/1.txt: 權限不夠
[lichao@test-02 ~]$ sudo less /root/1.txt
對不起,用戶 lichao 無權以 root 的身份在 test-02 上執行 /bin/less /root/1.txt。
[lichao@test-02 ~]$ sudo more /root/1.txt
aaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeefffffffffffffffffffffggggggggggggg
hhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkkkkkllllllllllllllllll
[lichao@test-02 ~]$

限制遠程登陸root

編輯/etc/ssh/ssh_config文件,將#PermitRootLogin yes ,註釋符號刪掉,後面的yes改成no ,再重啓服務,就限制了遠程登陸rootit

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

[root@test-02 ~]# systemctl restart sshd.service重啓服務,就好了io

限制了遠程登陸root,而有些普通用戶又須要root權限怎麼弄呢? 這時候能夠visudo,給須要權限的用戶受權su ,就能夠了test

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
AD      ALL=(ALL)       NOPASSWD: /usr/bin/su
相關文章
相關標籤/搜索