su命令 、sudo命令 和限制root远程登录

su命令

su命令是用来切换用户的,其格式为:su - username 常用的选项如下:

  • l,login 登录并改变到所切换的用户环境;
  • c,commmand=COMMAND 执行一个命令,然后退出所切换到的用户环境;
[lichao@test-02 ~]$ ls /root
ls: 无法打开目录/root: 权限不够
[lichao@test-02 ~]$ su - root
密码:
上一次登录:五 12月 29 02:37:30 CST 2017从 192.168.101.1pts/0 上
[root@test-02 ~]# ls /root
anaconda-ks.cfg
[root@test-02 ~]# 登出
[lichao@test-02 ~]$ su - root -c ls /root
密码:
anaconda-ks.cfg
[lichao@test-02 ~]$ 

当使用普通用户切换root的时候,需要输入root的密码,使用root切换到普通用户则不需要密码。

sudo命令

授权普通用户拥有别的用户的权限,大部分时候是授权普通用户拥有root用户的权限,要使用这个命令,需要编辑配置文件,etc/sudoers,但是直接编辑这个文件出错后系统不会报错,所以我们用visudo命令来编辑,如果编辑有问题,系统会提示错误

##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
lichao  ALL=(ALL)       /usr/bin/ls, /usr/bin/cat, /usr/bin/more

这样编辑之后,lichao 就可以拥有root的ls,cat ,more 的权限了,使用方式如下

[lichao@test-02 ~]$ ls /root
ls: 无法打开目录/root: 权限不够
[lichao@test-02 ~]$ sudo ls /root
1.txt  anaconda-ks.cfg
[lichao@test-02 ~]$ cat /root/1.txt
cat: /root/1.txt: 权限不够
[lichao@test-02 ~]$ sudo cat /root/1.txt
aaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeefffffffffffffffffffffggggggggggggg
hhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkkkkkllllllllllllllllll
[lichao@test-02 ~]$ less /root/1.txt
/root/1.txt: 权限不够
[lichao@test-02 ~]$ sudo less /root/1.txt
对不起,用户 lichao 无权以 root 的身份在 test-02 上执行 /bin/less /root/1.txt。
[lichao@test-02 ~]$ sudo more /root/1.txt
aaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeefffffffffffffffffffffggggggggggggg
hhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkkkkkllllllllllllllllll
[lichao@test-02 ~]$ 

限制远程登录root

编辑/etc/ssh/ssh_config文件,将#PermitRootLogin yes ,注释符号删掉,后面的yes改为no ,再重启服务,就限制了远程登录root

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

[root@test-02 ~]# systemctl restart sshd.service重启服务,就行了

限制了远程登录root,而有些普通用户又需要root权限怎么弄呢? 这时候可以visudo,给需要权限的用户授权su ,就可以了

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
AD      ALL=(ALL)       NOPASSWD: /usr/bin/su

猜你喜欢

转载自my.oschina.net/u/3731306/blog/1623155