2017-11-1 3周3次课

本文介绍了Linux系统中su和sudo命令的使用方法及其常见语法,包括如何切换用户身份及获取临时root权限。同时,文章详细说明了如何通过编辑sudoers文件赋予特定用户sudo权限,并演示了如何限制root用户的远程登录。

三周第三次课(11月1日)

3.7 su命令
3.8 sudo命令
3.9 限制root远程登录

命令su

语法 : su [-] username

su命令用于变更为其他使用者的身份。
    常用语法:su – 用户名 ;加‘-’表示切换用户的同时切换到该用户的环境变量下。如:
[root@node79 ~]# su aaa
[aaa@node79 root]$ pwd
/root
[aaa@node79 root]$ exit
exit
[root@node79 ~]# su - aaa
Last login: Thu Nov  2 08:28:16 CST 2017 on pts/0
[aaa@node79 ~]$ pwd
/home/aaa
[aaa@node79 ~]$ 
如果不加 ‘-‘ 切换到root账户下时,当前目录没有变化,而加上 ‘-‘ 切换到root账户后,当前目录为root账户的家目录,这跟直接登陆root账户是一样的。当用root切换普通用户时,是不需要输入密码的。这也体现了root用户至高无上的权利。

命令 : sudo

sudo 表示获取临时的root权限命令
    由于sudo的真正配置文件/etc/sudoers比较重要,不建议直接用vi来编辑内容,如果有语法错误则无法检测。具体使用命令visudo来操作:如果没有visudo这个命令,请使用 yum install -y sudo 安装。

默认root能够sudo是因为这个文件中有一行 “root ALL=(ALL) ALL” 使用 “visudo” 命令编辑/etc/sudoers配置文件,

配置文件中最核心的配置语句为:

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
表示允许root用户在任何时候都可以使用所有的命令。
默认root能够sudo是因为这个文件中有一行 “root ALL=(ALL) ALL” 在该行下面加入 “aa ALL=(ALL) ALL” 就可以让test用户拥有了sudo的权利。
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
aaa     ALL=(ALL)       ALL
测试:
[root@node79 ~]# su - aaa
Last login: Thu Nov  2 08:28:28 CST 2017 on pts/0
[aaa@node79 ~]$ 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 aaa:

#第一次使用时需要输入aaa的密码
如果不想普通用户在使用sudo时输入密码,则可以在配置文件中加入NOPASSWD,
root    ALL=(ALL)       ALL
aaa     ALL=(ALL)       NOPASSWD: /usr/bin/ls, /usr/bin/mv, /usr/bin/cat
[root@node79 ~]# su - aaa
Last login: Thu Nov  2 08:45:16 CST 2017 on pts/0
[aaa@node79 ~]$ ls /root/
ls: 无法打开目录/root/: 权限不够
[aaa@node79 ~]$ sudo ls /root/
1.txt  22.txt  234  2.txt  anaconda-ks.cfg1  test
[aaa@node79 ~]$ sudo /usr/bin/ls /root/
1.txt  22.txt  234  2.txt  anaconda-ks.cfg1  test

限制root远程登录
    禁止root用户远程登录的方法是修改ssh的配置文件/etc/ssh/sshd_config
[root@node79 ~]# vi /etc/ssh/sshd_config 
#PermitRootLogin yes
PermitRootLogin no 
[root@node79 ~]# systemctl restart sshd.service






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值