首頁·終端機命令

sudo

權限與擁有者

用法

sudo [cmd]

以別的使用者(預設 root)身分跑一條命令;sudo cmd > file 裡的 > 是你自己的 shell 開啟的,所以對 root 擁有的檔案會失敗,那時要用 tee。

常用選項

選項含義
-uRun as another user instead of root
-iStart a login shell with that user environment
-sStart a shell but keep your own environment
-EKeep your environment variables, if the policy allows it
-kForget the cached password right now
-lList what you are allowed to run
-vRefresh the timestamp without running anything

範例

sudo -u postgres psql

Runs the client as the postgres user.

sudo -i

A root login shell, with root PATH and environment.

echo text | sudo tee -a /etc/hosts

Redirection needs tee, because > runs as you.

數字模式和符號模式說的是同一件事:755 和 u=rwx,go=rx 是同一個權限。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. sudo 是做什麼的?

以別的使用者(預設 root)身分跑一條命令;sudo cmd > file 裡的 > 是你自己的 shell 開啟的,所以對 root 擁有的檔案會失敗,那時要用 tee。

Q. 怎麼寫?

sudo [cmd] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man sudo。這條命令屬於權限與擁有者。

相關命令

man 手冊: man sudo