sudo

パーミッション

書き方

sudo [cmd]

命令一つを別の利用者、既定では root として実行します。sudo cmd > file の > は自分のシェルで開かれるので 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 の > は自分のシェルで開かれるので root 所有のファイルでは失敗し、そこでは tee を使います。

Q. どう打ちますか。

sudo [cmd] — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは7個です。全部は man sudo にあります。このコマンドはパーミッションの仲間です。

関連するコマンド

man ページ: man sudo