20180921 su與sudo命令、限制root用戶經過ssh遠程登陸

su 命令

用戶切換。shell

su  # 切換到root用戶
su username   # 切換到username用戶
# su 後面加-時,會初始化當前用戶的各類環境
su - username 

# 指定用戶執行某些命令  
su - -c "touch /tmp/testfile02.txt" test06 # 以test06用戶身份在/tmp
                                           # 下建立了testfile02.txt

[root@centos01 ~]# su - -c "touch /tmp/testfile02.txt" test06
[root@centos01 ~]# ls -lt /tmp
total 124
-rw-rw-r--. 1 test06 test06      0 Sep 21 09:04 testfile02.txt

# 當要切換的用戶沒有家目錄時
[root@centos01 ~]# useradd -M test08  # 建立不帶家目錄的用戶test08
[root@centos01 ~]# su - test08  # 切換到test08,會有以下提示
su: warning: cannot change directory to /home/test08: No such file or directory
-bash-4.2$ pwd
/root
-bash-4.2$ logout
[root@centos01 ~]# id test08
uid=1006(test08) gid=1007(test08) groups=1007(test08)
[root@centos01 ~]# mkdir /home/test08  # 爲test08建立家目錄
[root@centos01 ~]# chown 1006:1007 /home/test08  #更改家目錄有的用戶及屬組
[root@centos01 ~]# ls -l /home/test08 -d
drwxr-xr-x. 2 test08 test08 6 Sep 21 09:14 /home/test08
[root@centos01 ~]# su - test08 # 再次切換,還有提示,是由於缺乏shell配置
Last login: Fri Sep 21 09:13:05 CST 2018 on pts/0
-bash-4.2$ pwd
/home/test08
-bash-4.2$ ls
-bash-4.2$ ls -a
.  ..
[root@centos01 ~]# ls /etc/skel/ -la  # 查看系統shell配置模板
total 24
drwxr-xr-x.  2 root root   59 Sep  7 09:48 .
drwxr-xr-x. 73 root root 8192 Sep 21 09:22 ..
-rw-r--r--.  1 root root   18 Jun 10  2014 .bash_logout
-rw-r--r--.  1 root root  193 Jun 10  2014 .bash_profile
-rw-r--r--.  1 root root  231 Jun 10  2014 .bashrc
[root@centos01 ~]# cp /etc/skel/.bash* /home/test08/  # 把模板拷貝到test08家目錄
[root@centos01 ~]# id test08
uid=1006(test08) gid=1007(test08) groups=1007(test08)
[root@centos01 ~]# chown test08:test08 -R /home/test08/  #爲這些配置文件更改用戶及屬組
[root@centos01 ~]# su - test08  # 再次切換,OK
Last login: Fri Sep 21 09:16:37 CST 2018 on pts/0
[test08@centos01 ~]$   # 普通用戶shell最開始是$,root用戶是#

sudo 命令

用su命令切換帶root用戶時,須要輸入root用戶的密碼。這樣很不安全,所以有了sudo命令。sudo能夠讓普通用戶臨時以指定用戶的身份去執行一條命令。
visudo 打開sudo的配置文件(/etc/sudoers)。!!!不要使用vi命令直接打開,由於不會檢查語法錯誤!!!
打開配置文件找到root ALL=(ALL) ALLcentos

root 指定哪一個用戶有sudo 權限 左邊的ALL指的是全部的主機,右邊的ALL指的是獲取哪一個用戶的 身份
第三段指能夠使用sudo的命令有哪些,ALL表示全部的命令安全

在這行內容下面模仿寫一條其餘用戶,就能夠讓該用戶擁有sudo權限了。
test08 ALL=(ALL) /usr/bin/ls, /usr/bin/catbash

[root@centos01 ~]# visudo #添加test08 ALL=(ALL)	/usr/bin/ls, /usr/bin/cat
[root@centos01 ~]# su - test08 # 切換到test08
Last login: Sat Sep 22 05:27:12 CST 2018 on pts/1
[test08@centos01 ~]$ ls /root/  # 沒有權限
ls: cannot open directory /root/: Permission denied
[test08@centos01 ~]$ 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 for test08:  # 登陸後第一次使用sudo時須要輸入自身的密碼
anaconda-ks.cfg  a.txt  d0917  link_test  s_link0.log  test.txt
[test08@centos01 ~]$ sudo ls /root/  # 以後不用再輸入密碼
anaconda-ks.cfg  a.txt  d0917  link_test  s_link0.log  test.txt

設置使用sudo 不須要密碼 test07 ALL=(ALL) NOPASSWD: /usr/bin/ls, /usr/bin/catssh

[root@centos01 ~]# visudo # 添加test07 ALL=(ALL)  NOPASSWD: /usr/bin/ls, /usr/bin/cat
[root@centos01 ~]# su - test07
[test07@centos01 ~]$ sudo ls /root
anaconda-ks.cfg  a.txt  d0917  link_test  s_link0.log  test.txt

限制root用戶經過ssh遠程登陸:

對應的配置文件是/etc/ssh/sshd_config,修改配置文件中的#PermitRootLogin yes爲 PermitRootLogin no, 以後重啓ssh服務 systemctl restart sshd.serviceui

相關文章
相關標籤/搜索