1. 程式人生 > >sudo的使用和限制root直接遠程登錄

sudo的使用和限制root直接遠程登錄

限制root用戶遠程登錄 sudo

3.7 su命令

su - 用戶名 切換用戶 - 的含義是完全意義上的把用戶身份切換過去
使用su切換用戶身份來執行命令su - -c "touch /tmp/user.txt" user,如下:

[root@localhost user]# su - -c "touch /tmp/user.txt" user
[root@localhost user]# ll /tmp/user.txt 
-rw-rw-r-- 1 user user 0 6月 11 22:32 /tmp/user.txt

su 切換用戶時會出現bash環境不完全的情況,這裏刪除了user的用戶bash環境文件

用戶家目錄下包含.bash_logout、.bash_profile、.bashrc的系統環境文件,這些文件定義了用戶登錄系統後再什麽樣的特定環境中運行,如果這些文件缺失或被刪除,用戶登錄系統後會在命令輸入行前會有跟正常顯示不同的登錄提示

[root@localhost user]# su user
bash-4.2$                                            登錄提示非正常
bash-4.2$ su - root                          切換回root用戶來對user用戶操作
密碼:
[root@localhost user]# 
[root@localhost user]# ls -la /etc/skel/           /etc/skel目錄下的bash用戶變量模板可拷貝到用戶家目錄下來恢復
總用量 24
drwxr-xr-x. 3 root root 78 5月 28 22:17 . 
drwxr-xr-x. 82 root root 8192 6月 11 22:05 ..
-rw-r--r--. 1 root root 18 8月 3 2016 .bash_logout               查看.bash的文件
-rw-r--r--. 1 root root 193 8月 3 2016 .bash_profile
-rw-r--r--. 1 root root 231 8月 3 2016 .bashrc
drwxr-xr-x. 4 root root 39 5月 28 22:17 .mozilla
[root@localhost user]# cp /etc/skel/.bash* /home/user                  拷貝.bash文件到user家目錄下
[root@localhost user]# chmod -R user:user /home/user                重新為這些文件指定所屬主所屬組
[root@localhost user]# su - user                                             root下切換到user用戶,恢復正常的命令行前綴提示
上一次登錄:一 6月 11 22:42:03 CST 2018pts/0 上

3.8 sudo 命令

普通用戶授權的命令,讓普通用戶臨時以root權限執行命令
set nu 在vi中查看行號

[root@localhost user]# visudo
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin        
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin        
##Next comes the main part: which users can run what software on
##which machines (the sudoers file can be shared between multiple
##systems).
##Syntax: 
#user MACHINE=COMMANDS
                       #給指定的用戶給予權限,權限需要寫絕對路徑
#The COMMANDS section may have other options added to it.           
#Allow root to run any commands anywhere
root ALL=(ALL) ALL
user ALL=(ALL) /usr/bin/ls,/usr/bin/mv,/usr/bin/cat          以root權限執行ls\mv\cat命令
xiangchen ALL=(ALL)    NOPASSWD: /usr/bin/mv,/usr/bin/cat    NOPASSWD:   執行sudo時不輸入密碼
保存退出即可生效

這樣user用戶就可以以root身份執行sudo使用ls\mv\cat命令,第一次使用時會驗證user自己的用戶密碼
讓user以root權限查看root下的文件

[user@localhost ~]$ ls /root
ls: 無法打開目錄/root: 權限不夠
[user@localhost ~]$ sudo ls /root             使用sudo能查看,需要輸入當前普通用戶密碼驗證
[sudo] password for user: 
anaconda-ks.cfg

visudo的命令別名,用戶組別名

[root@localhost ~]# visudo
##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
User_Alias YUNWEI = user, xiangchen  指定一個用戶組的別名,這個用戶組別名裏可以添加多個用戶來使用

##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
Cmnd_Alias XIANG = /usr/bin/ls, /usr/bin/mv, /usr/bin/cat       自定義命令組別名
.......................................................................
##Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
YUNWEI    ALL=(ALL)       XIANG         將用戶修改為用戶組,將命令修改為命令組
xiangchen ALL=(ALL)    NOPASSWD: /usr/bin/ls,/usr/bin/mv,/usr/bin/cat     
##Allows members of the ‘sys‘ group to run networking, software,
##service management apps and more.
#%sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

##Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL             指定多個用戶的用戶組,在這個組內可以添加多個用戶來分配權限

保存退出即生效

測試用戶組和命令組是否生效(ls命令):

[root@localhost ~]# su - user
上一次登錄:一 6月 11 23:15:39 CST 2018pts/0 上
[user@localhost ~]$ ls /root
ls: 無法打開目錄/root: 權限不夠                    不使用sudo執行命令會提示權限不夠
[user@localhost ~]$ sudo ls /root
[sudo] password for user:                        使用sudo能以root身份來查看不能訪問的內容
anaconda-ks.cfg

3.9 限制root用戶遠程登錄

限制root用戶遠程登錄後,需要使用普通用戶登錄系統然後再su到root用戶,但是,這裏有個情景,普通用戶不知道root用戶密碼,su到root時卻需要驗證密碼,這時我們把su這個命令加入到sudo中,這樣就可以以root權限的身份su到root用戶下,root權限su到root是不需要密碼驗證的

修改ssh配置文件,限制root用戶遠程登錄

[root@localhost ~]# vim /etc/ssh/sshd_config
PermitRootLogin no                                 找到PermitRootLogin yes這項去掉註釋並修改為PermitRootLogin no
[root@localhost ~]# systemctl restart sshd          保存並重啟sshd服務

測試,重啟ssh服務後使用密碼拒絕root用戶登錄驗證:
技術分享圖片
我們再次修改ssh配置文件解除root用戶遠程登錄的限制

[root@localhost ~]# vim /etc/ssh/sshd_config
#PermitRootLogin no                                            vi編輯中按下/輸入Root即可鎖定到該行
[root@localhost ~]# systemctl restart sshd          重啟sshd服務

技術分享圖片

sudo的使用和限制root直接遠程登錄