Linux su命令詳解 Linux su命令怎麼用

來源:互聯網
上載者:User

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

切換使用者

hnlinux@w3cschool.cc:~$ whoami //顯示目前使用者

hnlinux

hnlinux@w3cschool.cc:~$ pwd //顯示目前的目錄

/home/hnlinux

hnlinux@w3cschool.cc:~$ su root //切換到root使用者

密碼:

root@w3cschool.cc:/home/hnlinux# whoami

root

root@w3cschool.cc:/home/hnlinux# pwd

/home/hnlinux

切換使用者,改變環境變數

hnlinux@w3cschool.cc:~$ whoami //顯示目前使用者

hnlinux

hnlinux@w3cschool.cc:~$ pwd //顯示目前的目錄

/home/hnlinux

hnlinux@w3cschool.cc:~$ su - root //切換到root使用者

密碼:

root@w3cschool.cc:/home/hnlinux# whoami

root

root@w3cschool.cc:/home/hnlinux# pwd//顯示目前的目錄

/root

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.