sudo的使用和限制root直接远程登录

3.7 su命令

su - 用户名 切换用户 - 的含义是完全意义上的把用户身份切换过去
使用su切换用户身份来执行命令su - -c "touch /tmp/user.txt" user,如下:

[root@localhost user]# su - -c "touch /tmp/user.txt" user
[root@localhost user]# ll /tmp/user.txt 
-rw-rw-r-- 1 user user 0 6月 11 22:32 /tmp/user.txt

su 切换用户时会出现bash环境不完全的情况,这里删除了user的用户bash环境文件
用户家目录下包含.bash_logout、.bash_profile、.bashrc的系统环境文件,这些文件定义了用户登录系统后再什么样的特定环境中运行,如果这些文件缺失或被删除,用户登录系统后会在命令输入行前会有跟正常显示不同的登录提示

[root@localhost user]# su user
bash-4.2$                                            登录提示非正常
bash-4.2$ su - root                          切换回root用户来对user用户操作
密码:
[root@localhost user]# 
[root@localhost user]# ls -la /etc/skel/           /etc/skel目录下的bash用户变量模板可拷贝到用户家目录下来恢复
总用量 24
drwxr-xr-x. 3 root root 78 5月 28 22:17 . 
drwxr-xr-x. 82 root root 8192 6月 11 22:05 ..
-rw-r--r--. 1 root root 18 8月 3 2016 .bash_logout               查看.bash的文件
-rw-r--r--. 1 root root 193 8月 3 2016 .bash_profile
-rw-r--r--. 1 root root 231 8月 3 2016 .bashrc
drwxr-xr-x. 4 root root 39 5月 28 22:17 .mozilla
[root@localhost user]# cp /etc/skel/.bash* /home/user                  拷贝.bash文件到user家目录下
[root@localhost user]# chmod -R user:user /home/user                重新为这些文件指定所属主所属组
[root@localhost user]# su - user                                             root下切换到user用户,恢复正常的命令行前缀提示
上一次登录:一 6月 11 22:42:03 CST 2018pts/0 上

3.8 sudo 命令

普通用户授权的命令,让普通用户临时以root权限执行命令
set nu 在vi中查看行号

[root@localhost user]# visudo
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin        
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin        
##Next comes the main part: which users can run what software on
##which machines (the sudoers file can be shared between multiple
##systems).
##Syntax: 
#user MACHINE=COMMANDS
                       #给指定的用户给予权限,权限需要写绝对路径
#The COMMANDS section may have other options added to it.           
#Allow root to run any commands anywhere
root ALL=(ALL) ALL
user ALL=(ALL) /usr/bin/ls,/usr/bin/mv,/usr/bin/cat          以root权限执行ls\mv\cat命令
xiangchen ALL=(ALL)    NOPASSWD: /usr/bin/mv,/usr/bin/cat    NOPASSWD:   执行sudo时不输入密码
保存退出即可生效

这样user用户就可以以root身份执行sudo使用ls\mv\cat命令,第一次使用时会验证user自己的用户密码
让user以root权限查看root下的文件

[user@localhost ~]$ ls /root
ls: 无法打开目录/root: 权限不够
[user@localhost ~]$ sudo ls /root             使用sudo能查看,需要输入当前普通用户密码验证
[sudo] password for user: 
anaconda-ks.cfg

visudo的命令别名,用户组别名

[root@localhost ~]# visudo
##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
User_Alias YUNWEI = user, xiangchen  指定一个用户组的别名,这个用户组别名里可以添加多个用户来使用

##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
Cmnd_Alias XIANG = /usr/bin/ls, /usr/bin/mv, /usr/bin/cat       自定义命令组别名
.......................................................................
##Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
YUNWEI    ALL=(ALL)       XIANG         将用户修改为用户组,将命令修改为命令组
xiangchen ALL=(ALL)    NOPASSWD: /usr/bin/ls,/usr/bin/mv,/usr/bin/cat     
##Allows members of the 'sys' group to run networking, software,
##service management apps and more.
#%sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

##Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL             指定多个用户的用户组,在这个组内可以添加多个用户来分配权限

保存退出即生效

测试用户组和命令组是否生效(ls命令):

[root@localhost ~]# su - user
上一次登录:一 6月 11 23:15:39 CST 2018pts/0 上
[user@localhost ~]$ ls /root
ls: 无法打开目录/root: 权限不够                    不使用sudo执行命令会提示权限不够
[user@localhost ~]$ sudo ls /root
[sudo] password for user:                        使用sudo能以root身份来查看不能访问的内容
anaconda-ks.cfg

3.9 限制root用户远程登录

限制root用户远程登录后,需要使用普通用户登录系统然后再su到root用户,但是,这里有个情景,普通用户不知道root用户密码,su到root时却需要验证密码,这时我们把su这个命令加入到sudo中,这样就可以以root权限的身份su到root用户下,root权限su到root是不需要密码验证的

修改ssh配置文件,限制root用户远程登录

[root@localhost ~]# vim /etc/ssh/sshd_config
PermitRootLogin no                                 找到PermitRootLogin yes这项去掉注释并修改为PermitRootLogin no
[root@localhost ~]# systemctl restart sshd          保存并重启sshd服务

测试,重启ssh服务后使用密码拒绝root用户登录验证:
sudo的使用和限制root直接远程登录
我们再次修改ssh配置文件解除root用户远程登录的限制

[root@localhost ~]# vim /etc/ssh/sshd_config
#PermitRootLogin no                                            vi编辑中按下/输入Root即可锁定到该行
[root@localhost ~]# systemctl restart sshd          重启sshd服务

sudo的使用和限制root直接远程登录

猜你喜欢

转载自blog.51cto.com/8844414/2128984