[root@localhost ~]# whoami root [root@localhost ~]# su - hanfeng [hanfeng@localhost ~]$ whoami hanfeng [hanfeng@localhost ~]$ pwd /home/hanfeng [hanfeng@localhost ~]$ ls -la 总用量 16 drwx------. 2 hanfeng ha1 83 11月 1 15:07 . drwxr-xr-x. 4 root root 34 10月 31 15:38 .. -rw-------. 1 hanfeng ha1 21 11月 1 15:18 .bash_history -rw-r--r--. 1 hanfeng ha1 18 8月 3 2016 .bash_logout -rw-r--r--. 1 hanfeng ha1 193 8月 3 2016 .bash_profile -rw-r--r--. 1 hanfeng ha1 231 8月 3 2016 .bashrc [hanfeng@localhost ~]$ 登出 [root@localhost ~]#
其中这个 - ,目的就是完全的切换用户,使用本身的配置、环境变量等 .bash开头的文件都是用户的配置文件
[root@localhost ~]# su hanfeng [hanfeng@localhost root]$ whoami hanfeng [hanfeng@localhost root]$ pwd /root [hanfeng@localhost root]$ exit [root@localhost ~]#
[root@localhost ~]# su - -c "touch /tmp/han.111" hanfeng [root@localhost ~]# ls -lt /tmp/ |head 总用量 0 -rw-r--r--. 1 hanfeng ha1 0 11月 1 15:45 han.111 drwx------. 3 root root 17 11月 1 10:21 systemd-private-5909416742444897abc72857986169b4-vmtoolsd.service-klBF7K [root@localhost ~]# id hanfeng uid=1000(hanfeng) gid=1001(ha1) 组=1001(ha1),1002(user3) [root@localhost ~]#
这里会看到文件的属组是ha1,由于hanfeng用户的属组就是ha1
[root@localhost ~]# su - hanfeng 上一次登陆:三 11月 1 16:09:37 CST 2017pts/0 上 [hanfeng@localhost ~]$ su - user5 密码: 最后一次失败的登陆:三 11月 1 16:10:09 CST 2017pts/0 上 最有一次成功登陆后有 1 次失败的登陆尝试。 [user5@localhost ~]$ ls -la 总用量 12 drwx------. 2 user5 user5 62 11月 1 16:07 . drwxr-xr-x. 5 root root 47 11月 1 16:07 .. -rw-r--r--. 1 user5 user5 18 8月 3 2016 .bash_logout -rw-r--r--. 1 user5 user5 193 8月 3 2016 .bash_profile -rw-r--r--. 1 user5 user5 231 8月 3 2016 .bashrc [user5@localhost ~]$ 登出 [hanfeng@localhost ~]$ 登出 [root@localhost ~]#
[root@localhost ~]# useradd -M ll [root@localhost ~]# su - ll su: 警告:没法更改到 /home/ll 目录: 没有那个文件或目录 -bash-4.2$ -bash-4.2$ 登出 [root@localhost ~]# mkdir /home/ll [root@localhost ~]# chown ll:ll /home/ll/ [root@localhost ~]# su - ll 上一次登陆:三 11月 1 16:20:29 CST 2017pts/0 上 -bash-4.2$ pwd /home/ll -bash-4.2$ ls -la 总用量 0 drwxr-xr-x. 2 ll ll 6 11月 1 16:28 . drwxr-xr-x. 6 root root 57 11月 1 16:28 .. -bash-4.2$ 登出 [root@localhost ~]# cp /etc/skel/.bash* /home/ll/ [root@localhost ~]# chown -R ll:ll !$ chown -R ll:ll /home/ll/ [root@localhost ~]# su - ll 上一次登陆:三 11月 1 16:55:28 CST 2017pts/0 上 [ll@localhost ~]$ 登出 [root@localhost ~]#
[root@hf-01 ~]# su - hanfeng 上一次登陆:五 10月 27 05:14:42 CST 2017pts/1 上 [hanfeng@hf-01 ~]$ su - 密码: 上一次登陆:四 11月 2 01:26:08 CST 2017从 192.168.74.1pts/0 上 [root@hf-01 ~]# whoami root [root@hf-01 ~]# 登出 [hanfeng@hf-01 ~]$ 登出 [root@hf-01 ~]# 登出 [root@hf-01 ~]#
[root@hf-01 ~]# visudo 会进入/etc/sudoers.tmp的配置文件中 97 ## Allow root to run any commands anywhere 98 root ALL=(ALL) ALL 99 hanfeng ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/ls [root@hf-01 ~]#
[root@hf-01 ~]# su - hanfeng 上一次登陆:四 11月 2 03:52:44 CST 2017pts/0 上 [hanfeng@hf-01 ~]$ ls /root/ ls: 没法打开目录/root/: 权限不够 [hanfeng@hf-01 ~]$ sudo /usr/bin/ls /root/ 在执行命令后,会提示输入hanfeng用户的密码 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 hanfeng: 11.txt 234 33.txt ha.txt [hanfeng@hf-01 ~]$ ls /root/ 在hanfeng用户下直接去执行会发现没法打开/root/目录 ls: 没法打开目录/root/: 权限不够 [hanfeng@hf-01 ~]$ sudo /usr/bin/ls /root/ 11.txt 234 33.txt ha.txt [hanfeng@hf-01 ~]$ mv /root/ha.txt /root/haha.txt mv: 没法打开目录/root/: 权限不够 [hanfeng@hf-01 ~]$ sudo /usr/bin/mv /root/ha.txt /root/haha.txt [hanfeng@hf-01 ~]$ 登出 [root@hf-01 ~]#
[root@hf-01 ~]# visudo [root@hf-01 ~]# su - user2 上一次登陆:四 11月 2 07:17:04 CST 2017pts/0 上 [user2@hf-01 ~]$ ls /root/ ls: 没法打开目录/root/: 权限不够 [user2@hf-01 ~]$ sudo ls /root/ 11.txt 234 33.txt haha.txt [user2@hf-01 ~]$ sudo /usr/bin/ls /root/ 11.txt 234 33.txt haha.txt [user2@hf-01 ~]$ 登出 [root@hf-01 ~]#
## 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
## 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
例子:html
[root@hf-01 ~]# visudo 进入到配置环境中 而后到 ## Networking 那一段落最后加上 HANFENG_CMD = /usr/bin/ls, /usr/bin/mv, /usr/bin/cat 并将用户名hanfeng后面,去除那些绝对路径命令,修改上HANFENG_CMD,而后保存退出 root ALL=(ALL) ALL hanfeng ALL=(ALL) HANFENG_CMD [root@hf-01 ~]# su - hanfeng 上一次登陆:四 11月 2 05:46:40 CST 2017pts/0 上 [hanfeng@hf-01 ~]$ sudo ls /root/ 这里会发现能够查看/root/目录下的文件 [sudo] password for hanfeng: 11.txt 234 33.txt haha.txt [hanfeng@hf-01 ~]$ sudo ls /root/ 11.txt 234 33.txt haha.txt [hanfeng@hf-01 ~]$ sudo cat /root/haha.txt [hanfeng@hf-01 ~]$ 登出 [root@hf-01 ~]#
## Allows people in group wheel to run all commands 109 %wheel ALL=(ALL) ALL
[root@hf-01 ~]# visudo 在配置环境中设置 在## User Aliases一段末尾处的下一行设置 User_Alias HANS = hanfeng, user2, user4 在 ## Allow root to run any commands anywhere root ALL=(ALL) ALL hanfeng ALL=(ALL) HANFENG_CMD user10 ALL=(ALL) NOPASSWD: /usr/bin/ls, /usr/bin/mv, /usr/bin/cat user2 ALL=(ALL) NOPASSWD: /usr/bin/ls, /usr/bin/mv, /usr/bin/cat 在这里加上一下点,而后保存退出 HANS ALL=(ALL) NOPASSWD: /usr/bin/su 而后执行命令 [root@hf-01 ~]# su - hanfeng 上一次登陆:四 11月 2 07:51:34 CST 2017pts/0 上 [hanfeng@hf-01 ~]$ sudo su - 上一次登陆:四 11月 2 05:38:13 CST 2017从 192.168.74.1pts/1 上 [root@hf-01 ~]# whoami root [root@hf-01 ~]# 登出 [hanfeng@hf-01 ~]$ 登出 [root@hf-01 ~]#
[root@hf-01 ~]# vi /etc/ssh/sshd_config 在这里搜索/Root #LoginGraceTime 2m PermitRootLogin no 将前面的 #号 去除并将yes改成no,表示不容许远程登陆了 #StrictModes yes 而后保存退出,再重启配置服务 [root@hf-01 ~]# systemctl restart sshd.service [root@hf-01 ~]#
sudo与su比较shell
sudo配置文件样例bash