Latest web development tutorials

Linux su命令

Linux 命令大全 Linux命令大全

Linux su命令用於變更為其他使用者的身份,除root 外,需要鍵入該使用者的密碼。

使用權限:所有使用者。

語法

su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]]

參數說明 :

  • -f 或--fast 不必讀啟動檔(如csh.cshrc 等),僅用於csh 或tcsh
  • -m -p 或--preserve-environment 執行su 時不改變環境變數
  • -c command 或--command=command 變更為帳號為USER 的使用者並執行指令(command)後再變回原來使用者
  • -s shell 或--shell=shell 指定要執行的shell (bash csh tcsh 等),預設值為/etc/passwd 內的該使用者(USER) shell
  • --help 顯示說明文件
  • --version 顯示版本資訊
  • - -l 或--login 這個參數加了之後,就好像是重新login 為該使用者一樣,大部份環境變數(HOME SHELL USER等等)都是以該使用者(USER)為主,並且工作目錄也會改變,如果沒有指定USER ,內定是root
  • USER 欲變更的使用者帳號
  • ARG 傳入新的shell 參數

實例

變更帳號為root 並在執行ls 指令後退出變回原使用者

su -c ls root

變更帳號為root 並傳入-f 參數給新執行的shell

su root -f

變更帳號為clsung 並改變工作目錄至clsung 的家目錄(home dir)

su - clsung

切換用戶

[email protected]:~$ whoami //显示当前用户
hnlinux
[email protected]:~$ pwd //显示当前目录
/home/hnlinux
[email protected]:~$ su root //切换到root用户
密码: 
[email protected]:/home/hnlinux# whoami 
root
[email protected]:/home/hnlinux# pwd
/home/hnlinux

切換用戶,改變環境變量

[email protected]:~$ whoami //显示当前用户
hnlinux
[email protected]:~$ pwd //显示当前目录
/home/hnlinux
[email protected]:~$ su - root //切换到root用户
密码: 
[email protected]:/home/hnlinux# whoami 
root
[email protected]:/home/hnlinux# pwd //显示当前目录
/root

Linux 命令大全 Linux命令大全