3.7 su 命令 把root用户切换到aming用户下

[root@centos-01 ~]# whoami                                                    //先查看是哪个用户下//
root
[root@centos-01 ~]# su - aming                                              //加-可以彻底切换到aming用户//

[aming@centos-01 ~]$ whoami                                               //查看是哪个用户下//
aming
[aming@centos-01 ~]$ id                                                        //id命令也可  以查看//
uid=1000(aming) gid=1000(aming) 组=1000(aming),1002(user2) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

3.8 sudo命令 sudo 可以让一个普通用户临时拥有root用户的身份,这样就可以避免root用户的密码给普通用户了

配置sudo必须通过编辑/etc/sudoers文件,而且只有超级用户才可以修改它,还必须使用visudo编辑。 之所以使用visudo有两个原因,一是它能够防止两个用户同时修改它;二是它也能进行有限的语法检查。所以,即使只有你一个超级用户,你也最好用visudo来检查一下语法。 visudo默认的是在vi里打开配置文件,用vi来修改文件。我们可以在编译时修改这个默认项。visudo不会擅自保存带有语法错误的配置文件,它会提示你出现的问题,并询问该如何处理, 就像: >>> sudoers file: syntax error, line 22 << 此时我们有三种选择:键入“e”是重新编辑,键入“x”是不保存退出,键入“Q”是退出并保存。如果真选择Q,那么sudo将不会再运行,直到错误被纠正。 现在,我们一起来看一下神秘的配置文件,学一下如何编写它。

让我们从一个简单的例子开始:让用户user2可以通过sudo执行所有root可执行的命令。以root身份用visudo打开配置文件,可以看到类似下面几行: ##Allow root to run any commands anywhere root ALL=(ALL) ALL user2 ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/cat //给用户添加的可以使用的权限,一般在92行//

查看行数的命令 #set nu

当切换user2时

[root@centos-01 ~]# su - user2                                                                               //切换user2用户//

[user2@centos-01 ~]$ ls /root                                                                                   //查看root目录权限//
11.txt  123  1.txt  1.txt~  234  2.txt  3.txt  anaconda-ks.cfg.1  default
 [root@centos-01 ~]# chmod u-s /bin/ls                                                                       //去除s权限//
[root@centos-01 ~]# su - user2

 [user2@centos-01 ~]$ ls /root/                                                                                   //权限去除成功,user2不能查看root目录//
ls: 无法打开目录/root/: 权限不够
  • sudo的用法

    [user2@centos-01 ~]$ sudo /usr/bin/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 user2: 
    

    11.txt 123 1.txt 1.txt~ 234 2.txt 3.txt anaconda-ks.cfg.1 default

3.9 限制root远程登录

#vi /etc/ssh/sshd_config

把 PermitRootLogin yes 改为 PermitRootLogin no 重启sshd服务

  #systemctl   restart  sshd.service