su命令
su:切换用户
1、root用户切换test用户不需要密码
[root@test ~]# su - test
[test@test ~]$
2、普通用户切换为root用户就需要密码
[test@test ~]$ su -
Password: #root密码
[root@test ~]#
3、需要加上-,这个表示真正切换了用户
sudo命令
1、是限制版的su命令。
su命令缺点:当十个用户切换到root用户来执行命令时,10个人就需要指定root账号的密码,很不安全。
sudo:可以使用root身份才执行命令,可以分配权限给用户
[root@test ~]# visudo
#新增用户别名
User_Alias ADMINS = test,gztest
#新增命令别名
## ls
Cmnd_Alias LS=/bin/ls,/bin/rm
#针对op组做权限设置
%op ALL=(root) LS
[root@test ~]# su - test
[test@test ~]$ ls /root/ #没权限
ls: cannot open directory /root/: Permission denied
[test@test ~]$ sudo ls /root/ #加上sudo 就有权限了
[sudo] password for test: #输入test用户密码
123.txt 2.txt2.txt.bak anaconda-ks.cfg get-pip.pyindex.php test
[test@test ~]$ sudo rm -f /root/123.txt
[test@test ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[test@test ~]$ sudo ls /root/
2.txt 2.txt.bak anaconda-ks.cfg get-pip.py index.php test
限制root远程登录
设置test用户无密码访问root
[root@test-7 ~]# visudo
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
test ALL=(ALL) NOPASSWD: ALL
1、[root@test-7 ~]# vi /etc/ssh/sshd_config
PermitRootLogin no
2、用test用户登录
[test@test-7 ~]$ whoami
test
[test@test-7 ~]$ sudo su -
[root@test-7 ~]# whoami
root