su和sudo命令及限制root遠程登錄
阿新 • • 發佈:2018-04-05
su sudo 限制root遠程登錄 su命令
- 切換用戶
- 加-徹底進入用戶,不加-進入不徹底
[root@aminglinux-02 ~]# su aming [aming@aminglinux-02 root]$ whoami aming [aming@aminglinux-02 root]$ pwd /root [root@aminglinux-02 ~]# su - aming Last login: Wed Apr 4 20:42:45 CST 2018 on pts/0 [aming@aminglinux-02 ~]$ whoami aming [aming@aminglinux-02 ~]$ pwd /home/aming
- 在root用戶下以aming用戶身份執行命令,su - -c "touch /tmp/aming.111" aming
[root@aminglinux-02 ~]# su - -c "touch /tmp/aming.111" aming [root@aminglinux-02 ~]# ls -l /tmp/ total 12 drwxr-xr-x 2 root root 4096 Apr 1 20:26 1 srwxr-xr-x 1 root root 0 Mar 14 16:52 Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)> -rw-rw-r-- 1 aming aming 0 Apr 4 20:48 aming.111
-
用戶配置文件模板目錄/etc/skel/,用戶沒有家目錄和配置文件,不能正常顯示命令行需要拷貝模板文件
root@aminglinux-02 ~]# ls -al /etc/skel/ 總用量 24 drwxr-xr-x. 2 root root 62 5月 28 18:43 . drwxr-xr-x. 77 root root 8192 6月 13 21:10 .. -rw-r--r--. 1 root root 18 8月 3 2016 .bash_logout -rw-r--r--. 1 root root 193 8月 3 2016 .bash_profile -rw-r--r--. 1 root root 231 8月 3 2016 .bashrc
具體操作如下
[root@aminglinux-02 ~]# su - user5 su: warning: cannot change directory to /home/user5: No such file or directory -bash-4.2$ pwd /root -bash-4.2$ exit logout [root@aminglinux-02 ~]# mkdir /home/user5 [root@aminglinux-02 ~]# cp /etc/skel/.bash .bash_logout .bash_profile .bashrc [root@aminglinux-02 ~]# cp /etc/skel/.bash* /home/user5/ [root@aminglinux-02 ~]# chown -R user5:user5 !$ chown -R user5:user5 /home/user5/ [root@aminglinux-02 ~]# su - user5 Last login: Wed Apr 4 20:56:11 CST 2018 on pts/0 [user5@aminglinux-02 ~]$ pwd /home/user5
sudo命令
- 可以讓普通用戶臨時以root身份運行命令
- visudo命令打開sudo的配置文件
- 給aming用戶增加root權限的命令,可以修改visudo文件格式如下:
91 root ALL=(ALL) ALL 92 aming ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/cat 第一個ALL表示在哪裏默認就行,第二個ALL表示所有的命令,也可以寫多個命令用逗號加空格隔開,必須寫命令的絕對路徑
- 具體操作如下
[aming@aminglinux-02 ~]$ ls /root/ ls: cannot open directory /root/: Permission denied [aming@aminglinux-02 ~]$ sudo ls /root/ [sudo] password for aming: 1.txt [aming@aminglinux-02 ~]$ sudo ls /root/ 1.txt
- visudo配置文件還可以設定主機組,用戶組及命令組來規定root權限的命令
## 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
限制root遠程登錄
- 給普通用戶賦予root用戶的權限,修改visudo文件
19 ## rather than USERALIAS 20 # User_Alias ADMINS = jsmith, mikem 21 User_Alias AMINGS = aming, user3
90 ## Allow root to run any commands anywhere 91 root ALL=(ALL) ALL 92 aming ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/cat 93 AMINGS ALL=(ALL) NOPASSWD: /usr/bin/su #NOPASSWD:這個可以使普通用戶sudo的時候不用輸入密碼 #root用戶限制遠程登錄,可以在普通用戶下su切換到root用戶且不用密碼
- 在普通用戶下su切換到root用戶且不用密碼
[root@aminglinux-02 ~]# su - aming Last login: Wed Apr 4 21:35:36 CST 2018 on pts/0 [aming@aminglinux-02 ~]$ sudo su - Last login: Wed Apr 4 20:46:18 CST 2018 on pts/0 [root@aminglinux-02 ~]# pwd /root
- 限制root用戶遠程登錄,修改/etc/ssh/sshd_config文件
47 48 #LoginGraceTime 2m 49 PermitRootLogin no
- systemctl restart sshd.service重啟ssh服務
su和sudo命令及限制root遠程登錄