su命令、sudo命令,禁止远程登录root

su命令

切换到普通用户:

[root@localhost ~]# su - hch1

[hch1@localhost ~]$ whoami

hch1

[hch1@localhost ~]$ ls /home/hch1

[hch1@localhost ~]$ pwd

/home/hch1

[hch1@localhost ~]$ ls -la

总用量 12

drwx------. 2 hch1 hch1  62 12月 26 10:35 .

drwxr-xr-x. 4 root root  31 12月 26 10:35 ..

-rw-r--r--. 1 hch1 hch1  18 8月  3 05:11 .bash_logout

-rw-r--r--. 1 hch1 hch1 193 8月  3 05:11 .bash_profile

-rw-r--r--. 1 hch1 hch1 231 8月  3 05:11 .bashrc

Ctrl+D 登出

以普通用户身份执行命令

[root@localhost ~]# su - -c "touch /tmp/test.txt" hch1

[root@localhost ~]# ls -lt /tmp/ |head

总用量 0

-rw-rw-r--. 1 hch1  hch1  0 12月 26 10:47 test.txt

drwxr-xr-x. 3 root  root  18 12月 25 11:33 pear

srwxrwxrwx. 1 mysql mysql  0 12月 25 11:08 mysql.sock

sudo命令

用su是可以切换用户身份,如果每个普通用户都能切换到root身份,如果某个用户不小心泄漏了root的密码,那岂不是系统非常的不安全?没有错,为了改进这个问题,产生了sudo这个命令。使用sudo执行一个root才能执行的命令是可以办到的,但是需要输入密码,这个密码并不是root的密码而是用户自己的密码。默认只有root用户能使用sudo命令,普通用户想要使用sudo,是需要root预先设定的,即,使用 visudo 命令去编辑相关的配置文件/etc/sudoers. 如果没有visudo这个命令,请使用 yum install -y sudo 安装。

默认root能够sudo是因为这个文件中有一行 “root ALL=(ALL) ALL” 在该行下面加入 “test ALL=(ALL) ALL” 就可以让test用户拥有了sudo的权利。使用 “visudo” 命令编辑/etc/sudoers配置文件,其实它的操作方法和前面阿铭介绍的 “vi” 命令使用方法是一样的,按 ‘i’ 进入编辑模式,编辑完成后,按 “Esc” ,再输入 ”:wq” 完成保存。

给普通用户添加root权限命令:visudo

[

root用户下,用visudo给普通用户添加root的权限命令,格式如下图:

内容行数显示:一般模式下输入 :set nu

写入格式:此目的是:给普通用户授权root用户才可以使用的一些相应命令!

普通用户 ALL=(root) ALL或root下命令的绝对路径

-## Allow root to run any commands anywhere

root ALL=(ALL) ALL

hch1 ALL=(root) /usr/bin/ls, /usr/bin/mv, /usr/bin/cat

## Allows members of the 'sys' group to run networking, software,

在普通用户下,运行ls命令查看root目录,是没有权限的,使用sudo 命令后即可

[root@localhost ~]# su - hch1

上一次登录:二 12月 26 10:47:04 CST 2017pts/0 上

[hch1@localhost ~]$ ls /root/

ls: 无法打开目录/root/: 权限不够

[hch1@localhost ~]$ sudo ls /root

我们信任您已经从系统管理员那里了解了日常注意事项。

总结起来无外乎这三点:

    #1) 尊重别人的隐私。

    #2) 输入前要先考虑(后果和风险)。

    #3) 权力越大,责任越大。

[sudo] hch1 的密码:

anaconda-ks.cfg

以上使用sudo命令需要输入密码,每次操作都需要输入密码的话,这样效率就会大大降低,由此我们可以通过更改sudo免去输入密码这一步

[root@localhost ~]# visudo

## Allow root to run any commands anywhere

root    ALL=(ALL)      ALL

hch1 ALL=(root) NOPASSWD: /usr/bin/ls, /usr/bin/mv, /usr/bin/cat

[root@localhost ~]# su - hch1

上一次登录:二 12月 26 11:04:45 CST 2017pts/0 上

[hch1@localhost ~]$ ls /root

ls: 无法打开目录/root: 权限不够

[hch1@localhost ~]$ sudo ls /root

anaconda-ks.cfg

限制root远程登录

修改sshd.service配置文件,设定限制root远程登录

[root@localhost ~]# vi /etc/ssh/sshd_config

-#LoginGraceTime 2m

-#PermitRootLogin yes //把这项改成:PermitRootLogin no (注意前面没有井号)

-#StrictModes yes

-#MaxAuthTries 6

-#MaxSessions 10

然后重启sshd.service服务就好

[root@localhost ~]# systemctl restart sshd.service

这样就可以禁止远程工具登录root了。

你可能感兴趣的:(su命令、sudo命令,禁止远程登录root)