su命令
1.su命令: (1)彻底切换用户
[root@weix01 ~]# su - weixing01 #加 - 彻底切换用户
[weixing01@weix01 ~]$ whoami
weixing01
[weixing01@weix01 ~]$ id
uid=1000(weixing01) gid=1000(weixing01) 组=1000(weixing01),1002(grp2),1006(user5) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
(2)不彻底切换用户
[root@weix01 ~]# su weixing01 #不加 - 配置文件等未变动
[weixing01@weix01 root]$ pwd
/root
[weixing01@weix01 root]$ ls
ls: 无法打开目录.: 权限不够
2.以指定用户的身份执行一条命令,而不切换命令:
[root@weix01 ~]# su - -c "touch /tmp/weix.1111" weixing01 su - -c “命令” 用户
[root@weix01 ~]# ls -lt /tmp/ |head
总用量 0
-rw-rw-r--. 1 weixing01 weixing01 0 12月 27 23:39 weix.1111
3.从普通用户切换到另一个普通用户:
[weixing01@weix01 ~]$ su - user5 #输入密码即可,但是由于该用户没有家目录,所以不正常
密码:
最后一次失败的登录:三 12月 27 23:43:30 CST 2017pts/0 上
最有一次成功登录后有 2 次失败的登录尝试。
su: 警告:无法更改到 /home/user5 目录: 没有那个文件或目录
创建家目录,并更改所有者与所属组
[root@weix01 ~]# mkdir /home/user5
[root@weix01 ~]# chown user5:user5 /home/user5
再次切换还是不正常
[root@weix01 ~]# su - user5
上一次登录:三 12月 27 23:43:39 CST 2017pts/0 上
-bash-4.2$
主要是由于没有相应配置文件
-bash-4.2$ ls -la #缺少配置文件
总用量 0
drwxr-xr-x. 2 user5 user5 6 12月 27 23:45 .
drwxr-xr-x. 6 root root 68 12月 27 23:45 ..
-bash-4.2$ 登出
[root@weix01 ~]# ls /etc/skel/
[root@weix01 ~]# ls -la /etc/skel/ #配置文件模板
总用量 24
drwxr-xr-x. 2 root root 62 11月 15 02:01 .
drwxr-xr-x. 74 root root 8192 12月 27 23:43 ..
-rw-r--r--. 1 root root 18 8月 3 05:11 .bash_logout
-rw-r--r--. 1 root root 193 8月 3 05:11 .bash_profile
-rw-r--r--. 1 root root 231 8月 3 05:11 .bashrc
将配置文件拷贝到家目录下面:
[root@weix01 ~]# cp /etc/skel/.bash* /home/user5/ # .表示隐藏文件,*是通配符
更改所有者与所属组
[root@weix01 ~]# chown -R user5:user5 !$
chown -R user5:user5 /home/user5/
状态正常:
[root@weix01 ~]# su - user5
上一次登录:三 12月 27 23:46:29 CST 2017pts/0 上
4.普通用户切换到root用户下面:需要输入root用户密码
[weixing01@weix01 ~]$ su -
密码:
上一次登录:三 12月 27 23:33:07 CST 2017从 192.168.188.1pts/0 上
[root@weix01 ~]# pwd
/root
[root@weix01 ~]# whoami
root
sudo命令
1.sudo:让普通用户临时以指定用户的身份去执行一条命令,通常情况都是指定root用户身份 2.sudo配置文件: (1)使用visudo命令打开,有语法错误,可以检测到
[root@weix01 ~]# visudo
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command.
## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
# Host_Alias FILESERVERS = fs1, fs2
# Host_Alias MAILSERVERS = smtp, smtp2
## 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
## Command Aliases
## These are groups of related commands...
"/etc/sudoers.tmp" 112L, 3938C #实际配置文件
(2)不建议使用vi命令打开,如果有语法错误,无法识别 (3)配置文件含义
## Allow root to run any commands anywhere #允许root用户在任何地方执行任何命令
root ALL=(ALL) ALL #第一个all代表在哪里(主机),括号里面的all代表授权用户 最后一个all代表命令
(4)当更改文件有语法错误时,按回车选择:
[root@weix01 ~]# visudo
>>> /etc/sudoers: 语法错误 near line 93 <<<
现在做什么?
选项有:
重新编辑 sudoers 文件(e)
退出,不保存对 sudoers 文件的更改(x)
退出并将更改保存到
```
(5)使用:set nu查看行数
97 PROCESSES, LOCATE, DRIVERS 98 ## Allows people in group wheel to run all commands 99 %wheel ALL=(ALL) ALL 100 101 ## Same thing without a password :set nu
(6)命令要写成绝对路径,且要有空格分割:
92 root ALL=(ALL) ALL 93 weixing01 ALL=(ALL) /usr/bin/ls, usr/bin/mv, usr/bin/cat
(7)使用sudo来执行命令:
[weixing01@weix01 ~]$ ls /root/ #正常情况无法使用 ls: 无法打开目录/root/: 权限不够 [weixing01@weix01 ~]$ sudo /usr/bin/ls /root/ #通过sudo 来执行
我们信任您已经从系统管理员那里了解了日常注意事项。 总结起来无外乎这三点:
#1) 尊重别人的隐私。
#2) 输入前要先考虑(后果和风险)。
#3) 权力越大,责任越大。
[sudo] weixing01 的密码: 2.txt anaconda-ks.cfg BugReport.txt
[weixing01@weix01 ~]$ sudo /usr/bin/cat /root/2.txt
[weixing01@weix01 ~]$ cat /root/2.txt
cat: /root/2.txt: 权限不够
(8)配置另一个用户不用密码登录:
93 weixing01 ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/cat
94 user5 ALL=(ALL) NOPASSWD: /usr/bin/ls, /usr/bin/cat #NOPASSWD 不用密码登录
[root@weix01 ~]# su - user5 上一次登录:三 12月 27 23:52:26 CST 2017pts/0 上 [user5@weix01 ~]$ ls /root/ ls: 无法打开目录/root/: 权限不够 [user5@weix01 ~]$ sudo ls /root 2.txt anaconda-ks.cfg BugReport.txt
(9)别名用法:
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 WEIXING_CMD = /usr/bin/ls, /usr/bin/mv, /usr/bin/cat #命令别名用法
[root@weix01 ~]# su - weixing01 上一次登录:四 12月 28 00:10:36 CST 2017pts/0 上 [weixing01@weix01 ~]$ sudo ls /root/ [sudo] weixing01 的密码: 2.txt anaconda-ks.cfg BugReport.txt [weixing01@weix01 ~]$ sudo cat /root/2.txt
(10)用户组用法:
Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL
# 限制root用户远程登录
1.禁止root远程登录后,普通用户可以使用sudo su命令来切换到root用户下
(1)首先创建个用户组的别名
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 WEIXINGS = weixing01, user5, weixing02 #创建别名
(2)编写该组的规则
Allow root to run any commands anywhere
root ALL=(ALL) ALL weixing01 ALL=(ALL) WEIXING_CMD user5 ALL=(ALL) NOPASSWD: /usr/bin/ls, /usr/bin/cat WEIXINGS ALL=(ALL) NOPASSWD: /usr/bin/su #该组用户使用su时不用密码
(3)使用sudo su命令来切换
[root@weix01 ~]# su - weixing01
上一次登录:四 12月 28 00:33:21 CST 2017pts/0 上
[weixing01@weix01 ~]$ sudo su - #不用输入密码即可切换
上一次登录:四 12月 28 00:43:11 CST 2017从 192.168.188.1pts/1 上
2.禁止root用户远程登录:
(1)找到配置文件
[root@weix01 ~]# vi /etc/ssh/sshd_config
Ciphers and keying
#RekeyLimit default none
Logging
#SyslogFacility AUTH SyslogFacility AUTHPRIV #LogLevel INFO
Authentication:
#LoginGraceTime 2m #PermitRootLogin yes #将此行#去掉,并改成no
(2)更改配置
#LoginGraceTime 2m PermitRootLogin no
(3)重启服务
[root@weix01 ~]# systemctl restart sshd.service
(4)查看是否还能远程
![](http://i2.51cto.com/images/blog/201712/28/834da0da81b6aa1d9b5315192cceaa38.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![](http://i2.51cto.com/images/blog/201712/28/3ed2a9b8c9ed90a5e6c548e819c167a5.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
密码与秘钥都无法登录
(5)使用远程工具先登录普通用户,再切换到root下面
[weixing01@weix01 ~]$ ls /root ls: 无法打开目录/root: 权限不够 [weixing01@weix01 ~]$ su - root 密码: [weixing01@weix01 ~]$ sudo su - root 上一次登录:四 12月 28 00:48:26 CST 2017pts/0 上 最后一次失败的登录:四 12月 28 00:56:30 CST 2017从 192.168.188.1ssh:notty 上 最有一次成功登录后有 1 次失败的登录尝试。 [root@weix01 ~]# ls 2.txt anaconda-ks.cfg BugReport.txt