6.13 3.7-3.9

3.7 su命令

su命令用于切换用户

[root@hyc-01-01 ~]# whoami 查看当前用户

root

[root@hyc-01-01 ~]# su - hyc2

[hyc2@hyc-01-01 ~]$ id 查看当前用户

uid=1006(hyc2) gid=1001(hyc1) =1001(hyc1) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

susu-的区别:

都可以切换用户;

su只会切换登录的用户,切换时不会加载被切换用户的配置文件、环境变量等内容;

su-会直接切换到用户家目录下加载被切换用户的配置文件、环境变量等内容;

 

su - –c 指定以某个用户的身份执行某个命令

[root@hyc-01-01 ~]# whoami

root

[root@hyc-01-01 ~]# su - -c "touch /tmp/ldp" hyc

[root@hyc-01-01 ~]# ls -l /tmp/ldp

-rw-rw-r--. 1 hyc hyc 0 6  13 20:54 /tmp/ldp ldp文件的属主和属组为hyc

[root@hyc-01-01 ~]# tail -7 /etc/passwd

hyc:x:1000:1000::/home/hyc:/bin/bash

hyc1:x:1001:1001::/home/hyc1:/bin/bash

hyc2:x:1006:1001::/home/hyc2:/bin/bash

hyc3:x:1007:1005::/home/hyc111:/sbin/nologin

hyc4:x:1008:1008::/home/hyc4:/bin/bash

hyc5:x:1014:1001::/home/abc:/sbin/nologin

hyc6:x:1015:1015::/home/hyc6:/bin/bash

[root@hyc-01-01 ~]# date

2018 06 13 星期三 21:00:22 CST

root切换到普通用户不需要密码,从一个普通用户切换到另一个普通用户需要密码

[root@hyc-01-01 ~]# whoami

root

[root@hyc-01-01 ~]# su - hyc

上一次登录:三 6 13 21:01:10 CST 2018pts/1

[hyc@hyc-01-01 ~]$ whoami

hyc

[hyc@hyc-01-01 ~]$ su - hyc1

密码:

上一次登录:五 6  8 01:09:00 CST 2018pts/0

最后一次失败的登录:三 6 13 21:01:33 CST 2018pts/1

最有一次成功登录后有 2 次失败的登录尝试。

[root@hyc-01-01 ~]# ls /home home目录不存在用户hyc4的家目录

3.txt-  hyc  hyc1  hyc111  hyc2  hyc5  hyc6  hyc7

[root@hyc-01-01 ~]# tail -7 /etc/passwd

hyc:x:1000:1000::/home/hyc:/bin/bash

hyc1:x:1001:1001::/home/hyc1:/bin/bash

hyc2:x:1006:1001::/home/hyc2:/bin/bash

hyc3:x:1007:1005::/home/hyc111:/sbin/nologin

hyc4:x:1008:1008::/home/hyc4:/bin/bash

hyc5:x:1014:1001::/home/abc:/sbin/nologin

hyc6:x:1015:1015::/home/hyc6:/bin/bash

[root@hyc-01-01 ~]# su - hyc4 导致切换时无法加载用户hyc4的配置文件

su: 警告:无法更改到 /home/hyc4 目录: 没有那个文件或目录

-bash-4.2$

解决办法:

用户缺少家目录

[root@hyc-01-01 ~]# mkdir /home/hyc4 创建hyc4家目录

[root@hyc-01-01 ~]# chown hyc4:hyc4 /home/hyc4 修改目录权限

[root@hyc-01-01 ~]# su - hyc4

上一次登录:三 6 13 21:07:57 CST 2018pts/1

-bash-4.2$ 由于缺少配置文件导致依然无法正常登录

[root@hyc-01-01 ~]# ls -al /etc/skel/ 该目录下为用户家目录配置文件模板

总用量 24

drwxr-xr-x.  2 root root   62 5  25 04:49 .

drwxr-xr-x. 74 root root 8192 6  13 21:07 ..

-rw-r--r--.  1 root root   18 8   3 2017 .bash_logout

-rw-r--r--.  1 root root  193 8   3 2017 .bash_profile

-rw-r--r--.  1 root root  231 8   3 2017 .bashrc

[root@hyc-01-01 ~]# cp /etc/skel/.bash* /home/hyc4 复制模板到hyc4家目录下

[root@hyc-01-01 ~]# chown -R hyc4:hyc4 !$

chown -R hyc4:hyc4 /home/hyc4 连带修改hyc4目录下所有文件的属主和属组

[root@hyc-01-01 ~]# su - hyc4

上一次登录:三 6 13 21:13:04 CST 2018pts/1

[hyc4@hyc-01-01 ~]$ 切换正常

普通用户显示$,root用户显示#

 

3.8 sudo命令

可以让普通用户临时拥有某个用户(包括root)的权限

 

[root@hyc-01-01 ~]# visudo

visudo专门用于编辑/etc/sudoers.tmp文件,可以帮助检测语法错误;

若使用vi命令无法检测语法错误;

     91 ## Allow root to run any commands anywhere

     92 root    ALL=(ALL)       ALL 允许root用户在任何地方以任何用户的身份执行任何命令

     93 hyc     ALL=(root)      ls,mv,cat 允许hyc用户在任何地方以root身份执行lsmvcat       

第二段的ALL通常不修改,这个ALL=后面的括号中写需要获得哪个用户的身份,可以写ALL表示任何用户;

第三段可以写多个或一个命令表示执行的命令,写ALL则为执行任何命令;

正常保存退出出现提示:

>>> /etc/sudoers: 语法错误 near line 93 <<<

现在做什么? 此处回车出现以下信息

选项有:

  重新编辑 sudoers 文件(e)

  退出,不保存对 sudoers 文件的更改(x)

  退出并将更改保存到 sudoers 文件(危险!)(Q)

现在做什么? e重新编辑

     91 ## Allow root to run any commands anywhere

     92 root    ALL=(ALL)       ALL

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

最后一段命令需要写绝对路径;

命令间用逗号和空格分开;

 

[hyc@hyc-01-01 ~]$ ls /root 普通用户无权限查看root家目录

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

[hyc@hyc-01-01 ~]$ sudo /usr/bin/ls /root/ 使用户hycroot身份执行ls命令

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

总结起来无外乎这三点:

    #1) 尊重别人的隐私。

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

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

[sudo] hyc 的密码: 第一次用sudo执行命令时都要输入当前用户的密码

111  222  3.txt  anaconda-ks.cfg.1  hyc2  ls2  test.txt  新建文本文档.txt

[hyc@hyc-01-01 ~]$ sudo /usr/bin/ls /root/

111  222  3.txt  anaconda-ks.cfg.1  hyc2  ls2  test.txt  新建文本文档.txt

[root@hyc-01-01 ~]# visudo

     91 ## Allow root to run any commands anywhere

     92 root    ALL=(ALL)       ALL

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

在最后一段最前面加NOPASSWD:则首次使用sudo时将不提示输入密码

[root@hyc-01-01 ~]# su - hyc

上一次登录:三 6 13 22:11:03 CST 2018pts/1

[hyc@hyc-01-01 ~]$ sudo ls /root 此处未提示密码

111  222  3.txt  anaconda-ks.cfg.1  hyc2  ls2  test.txt  新建文本文档.txt

[root@hyc-01-01 ~]# visudo

# Host_Alias     FILESERVERS = fs1, fs2 为主机做别名

# Host_Alias     MAILSERVERS = smtp, smtp2

# User_Alias ADMINS = jsmith, mikem 为用户做别名

     27 # Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dh        client, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /s        bin/iwconfig, /sbin/mii-tool

     28 Cmnd_Alias HYC_CMD = /usr/bin/ls, /usr/bin/mv, /usr/bin/cat

      91 ## Allow root to run any commands anywhere

     92 root    ALL=(ALL)       ALL

     93 hyc     ALL=(root)      HYC_CMD

此处命令别名需要大写

[root@hyc-01-01 ~]# su - hyc

上一次登录:三 6 13 22:20:46 CST 2018pts/1

[hyc@hyc-01-01 ~]$ sudo ls /root 修改别名成功

[sudo] hyc 的密码:

111  222  3.txt  anaconda-ks.cfg.1  hyc2  ls2  test.txt  新建文本文档.txt

3.9 限制root远程登录

用户无法直接远程到root,但可以执行su命令切换到root

 

设置用户可以以任何用户的身份使用su命令方便从普通用户切换到root

[root@hyc-01-01 ~]# visudo

User_Alias HYCS = hyc, hyc1

## Allow root to run any commands anywhere

root    ALL=(ALL)       ALL

hyc     ALL=(root)      HYC_CMD

HYCS    ALL=(ALL)       NOPASSWD:/usr/bin/su

[root@hyc-01-01 ~]# su - hyc

上一次登录:三 6 13 23:30:41 CST 2018pts/1

[hyc@hyc-01-01 ~]$ sudo su – 在使用su时直接获得root权限执行

上一次登录:三 6 13 23:36:33 CST 2018pts/1

限制root用户远程登录

[root@hyc-01-01 ~]# vi /etc/ssh/sshd_config

     38 #PermitRootLogin no

[root@hyc-01-01 ~]# systemctl restart sshd.service 修改配置后重启服务

此时会发现root用户无法远程登录

[hyc@hyc-01-01 ~]$ su – root 用普通用户登录后切换到root下,发现需要密码但不知道密码,导致无法切换

密码:

su: 鉴定故障

[hyc@hyc-01-01 ~]$

[hyc@hyc-01-01 ~]$ sudo su – root 使用sudo命令以root身份切换root不需要密码

上一次登录:三 6 13 23:56:40 CST 2018tty1

最后一次失败的登录:三 6 13 23:57:48 CST 2018pts/0

最有一次成功登录后有 1 次失败的登录尝试。

用户执行su – root时获得了root权限,所以切换root不需要密码



猜你喜欢

转载自blog.51cto.com/12216458/2129116