su命令介紹
- sum命令,它是用來切換用戶的
- 快捷鍵ctrl+d,退出當前用戶
su命令用法
[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開頭的文件都是用戶的配置文件
- su username 看到用戶已經切換,但不是完全的切換過來,由於還在/root用戶下
[root@localhost ~]# su hanfeng
[hanfeng@localhost root]$ whoami
hanfeng
[hanfeng@localhost root]$ pwd
/root
[hanfeng@localhost root]$ exit
[root@localhost ~]#
su - -c用法
- su - -c 「命令」 用戶名:是不進入這個帳戶,可是以這個帳戶的身份執行命令
[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 ~]#
/etc/skel文件
- useradd -M 不要自動創建用戶的登入目錄,就是說建立的用戶沒有配置文件
- /etc/skel是存放模板配置文件
[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 ~]#
- 普通用戶也能夠 su - 到root用戶下,只須要root用戶的密碼
[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 ~]#