SUDO(8) MAINTENANCE COMMANDS SUDO(8) 名前 sudo, sudoedit - コマンドを別のユーザとして実行する 書式 sudo -h | -K | -k | -L | -V sudo -v [-AknS] [-p prompt] sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U username] [-u username|#uid] [command] sudo [-AbEHnPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u username|#uid] [VAR=value] [-i | -s] [command] sudoedit [-AnS] [-C fd] [-g groupname|#gid] [-p prompt] [-u username|#uid] file ... 説明 sudo を使用すると、許可されたユーザが、スーパーユーザを始め自分以外のユ ーザに変身して、コマンドを実行することが可能になる。許可の範囲について は、sudoers ファイルで指定する。実 uid と gid 、実効 uid と gid は、変 身の対象になるユーザの、passwd ファイルに明記されているものと同一になる ようにセットされる。所属するグループについても (-P オプションが指定され ていないかぎり)、group ファイルに基づいて初期化される。sudo を起動する ユーザが root だったり、変身の対象になるユーザが sudo を起動するユーザ と同一だったりする場合は、パスワードは要求されない。それ以外の場合は、 パスワードを要求して、本人であることを証明するように求めるのが、sudo の デフォルトの動作である (注意: デフォルトの設定では、求められるのは sudo を実行するユーザのパスワードであって、 root のパスワードではない)。ユー ザの認証がすむと、タイムスタンプが更新され、ユーザはしばらくの間 (sudoers で設定が変更されていないかぎり 5 分間) パスワードなしで sudo を使用することができる。 sudoedit というコマンド名で起動するのは、sudo に -e オプション (下記参 照) を付けて実行するのと同じである。 sudo は /etc/sudoers ファイルを参照して、誰に sudo を使用する権限がある かを判断する。sudo に -v オプションを付けて動かすと、ユーザはコマンドを 実行せずに、タイムスタンプを更新することができる。ユーザが 5 分以内にパ スワードを入力しないと、パスワードプロンプトそのものも (sudoers で設定 が変更されていないかぎり)、タイムアウトする。 sudoers ファイルに名前の載っていないユーザが sudo を使ってコマンドを実 行しようとすると、しかるべき権威者にメールが送付される。誰にメールを送 るかは、プログラムを configure するときに決めることができるが、sudoers ファイルで指定してもよい (デフォルトの宛先は root である)。sudo を使用 する権限のないユーザが、-l や -v オプションを付けて sudo を実行しようと しても、メールは送付されないことに注意してほしい。これによって、自分が sudo を使用できるかどうか、ユーザが自分で判断できるようになっているので ある。 sudo が root によって実行されたとき、環境変数 SUDO_USER がセットされて いると、sudo は実際のユーザが誰かを決めるのに、その変数の値を使用するこ とになる。ユーザはこの動作を利用することで、すでにルート・シェルを呼び 出しているときでも、自分がそこからさらに sudo 経由で使用したコマンドの ログを取ることができる。またこのことによって、sudo で実行したスクリプト やプログラムから呼び出される場合でも、-e オプションが相変わらず便利なも のになっている。とは言え、こうした場合でも、sudoers の参照は、やはり root に対してなされるのであって、 SUDO_USER が指定するユーザに対してで はないことに注意してほしい。 sudo は、実行の試みが成功した場合も失敗した場合も、そのことを (エラー内 容とともに) syslog(3) なり独自のログファイルなり、あるいはその両方なり に書き出すことができる。デフォルトでは、sudo は syslog(3) 経由でログを 記録するが、この動作は configure 時に、あるいは sudoers ファイルによっ て、変更可能である。 オプション sudo では以下のコマンドライン・オプションが使用できる。 -A 通常 sudo がパスワードを要求するとき、パスワードは現在使用 しているターミナルから読み込まれる。-A (askpass) を指定した 場合は、ヘルパー・プログラム (グラフィカルなものでもよい) が実行されて、ユーザのパスワードを読み込み、それを標準出力 に書き出す。環境変数 SUDO_ASKPASS が設定されているときは、 それがヘルパー・プログラムのパスになる。それ以外の場合は、 sudoers(5) の askpass オプションが指定している値が使用され る。 -b -b (background) オプションを付けると、sudo は指定されたコマ ンドをバックグラウンドで実行する。-b オプションを使用すると 、シェルのジョブ制御を使ってプロセスを操作できなくなるので 注意してほしい。 -C fd 通常 sudo は、標準入力、標準出力、標準エラーを除いて、オー プンしたすべてのファイル・ディスクリプタをクローズする。こ れに対し、 -C (close from) オプションを使用すると、標準エラ ーより番号の大きい (すなわち、ファイル・ディスクリプタ 3 以 上の) どのファイル・ディスクリプタからクローズして行くかを 、ユーザが指定できるようになる。3 未満の値は許可されない。 このオプションが使用できるのは、管理者が sudoers(5) 中で closefrom_override オプションを有効にしたときだけである。 -E -E (preserve environment) オプションは sudoers(5) ファイル の env_reset オプションを無効にする。これが使用できるのは、 マッチするコマンドに SETENV タグが付いているときか、 sudoers(5) で setenv オプションがセットされているときだけで ある。 -e -e (edit) オプションを指定するのは、ユーザーがコマンドの実 行ではなく、一つ以上のファイルの編集を望んでいるということ である。 sudoers ファイルの参照では、コマンドとして文字列 "sudoedit" が使用される。 sudoers ファイルによってユーザに 権限があることが認められると、次のようなことが順番に行われ る。 1. 編集対象のファイルのコピーをテンポラリファイルとして作 成する。テンポラリファイルのオーナーは sudo を起動した ユーザである。 2. 環境変数 SUDO_EDITOR, VISUAL, EDITOR のいづれかで指定さ れたエディタを起動して、テンポラリファイルを編集する。 SUDO_EDITOR, VISUAL, EDITOR のいづれも設定されていない ときは、 sudoers ファイルの editor 変数にリストされた最 初のプログラムが使用される。 3. 編集作業がすむと、テンポラリファイルをオリジナルのファ イルに書き戻して、テンポラリファイルを消去する。 指定されたファイルが存在しない場合は作成する。ここで注意す べきは、 sudo によって実行されるコマンドの大部分と違って、 -e でエディタが実行されるときは、sudo を起動したユーザの環 境が、変更を受けずにそのまま使われるということである。何ら かの理由で sudo が編集した内容でファイルを更新できないとき は、ユーザに警告を発し、編集した内容をテンポラリファイルに 保持することになる。 -g group 通常 sudo はプライマリ・グループを、passwd データベースで変 身対象ユーザ (デフォルトでは root) のプライマリ・グループと して指定されているグループに設定する。これに対し、-g (group) オプションを使用すると、sudo はプライマリ・グループ を group に設定して、指定のコマンドを実行することになる。グ ループ名の代わりに gid を指定するときは、#gid という書き方 をする。多くのシェルでは、gid としてコマンドを実行するとき は、’#’ をバックスラッシュ (’\’) でエスケープしなければなら ない。なお、 -u オプションを同時に指定しない場合、コマンド は (root としてではなく) sudo を起動するユーザの資格で実行 されることになる。いづれにしろ、プライマリ・グループが group に設定されることに変わりはない。 (訳注: -g オプション を使用するには、sudoers ファイルのユーザ設定で変身対象とな るグループを設定しておく必要がある。詳細については、 sudoers(5) のマニュアルの該当個所を参照してほしい。) -H -H (HOME) オプションを指定すると、環境変数 HOME が、変身の 対象となるユーザ (デフォルトでは root) の、 passwd(5) ファ イルに明記されているホームディレクトリになる。 sudo はデフ ォルトでは、環境変数 HOME を変更しない (sudoers(5) の set_home や always_set_home フラグを参照すること)。 -h -h (help) オプションを指定すると、sudo は使用法を表示して終 了する。 -i [command] -i (simulate initial login) オプションを指定すると、 passwd(5) 中の変身対象ユーザのエントリで指定されているシェ ルがログイン・シェルとして実行される。すなわち、.profile や .login といったログイン用のリソースファイルがシェルによって 読み込まれるわけだ。コマンドを指定すると、それがシェルに渡 されて、実行される。それ以外の場合は、対話的シェルが実行さ れることになる。sudo は、シェルを実行する前に、変身対象ユー ザのホームディレクトリに移動しようとする。また sudo は環境 を初期化する。変数 DISPLAY と TERM は変更しないが、 HOME, SHELL, USER, LOGNAME, PATH をセットし、さらに Linux や AIX システムでは /etc/environment に書いてある環境変数も設定す る。それ以外の環境変数はすべて消去する。 -K -K (sure kill) オプションは -k オプションに似ているが、ユー ザのタイムスタンプを完全に消去してしまうという点と、コマン ドや他のオプションと組み合わせて使用できないという点で異な っている。このオプションはパスワードを要求しない。 -k -k (kill) オプションを単独で使うと、sudo はタイムスタンプの 日付を UNIX 紀元 (1970 年 1 月 1 日) に設定することで、ユー ザのタイムスタンプを失効させる。次回 sudo を実行するときに は、パスワードが要求されるようになるわけだ。このオプション 自身はパスワードを必要としない。なお、このオプションが追加 されたのは、ユーザが .logout ファイルで、パスワードなしでの sudo の実行を無効にすることができるようにするためである。 -k オプションをコマンドや、パスワードを必要とするような他の オプションと組み合わせて使用すると、sudo はユーザのタイムス タンプ・ファイルを無視するようになる。その結果、sudo は (sudoers の設定でパスワードを要求するようになっているなら ば) プロンプトを出してパスワードを求めるものの、ユーザのタ イムスタンプ・ファイルの日付を更新することはない。 -L -L (list defaults) オプションは、sudoers ファイルの Defaults 行で設定できるパラメーターを列挙し、それぞれについ て簡単に説明する。このオプションは grep(1) と一緒に使うと、 便利である。 -l[l] [command] command を指定しない場合、-l (list) オプションは、 sudo を 実行したユーザ (あるいは、-U で指定したユーザ) が現在ログイ ンしているホストで許可されている (及び、禁じられている) コ マンドを列挙する。command を指定した場合は、sudoers で許可 されているコマンドならば、その絶対パスを表示する (訳注: sudo-1.7.2 では、 sudoers でコマンドの引き数を明示的に指定 している場合は、コマンドラインからも -l とコマンドに続けて 、引き数まで正しく指定しなければならない)。指定したコマンド が許可されていない場合は、sudo が返り値 1 で終了する。-l オ プションに l 引数を付けた場合や (すなわち -ll)、-l を複数回 指定した場合は、長い方のリスト形式が使用される。 -n -n (non-interactive) オプションがあると、sudo はユーザにパ スワードを要求するプロンプトを出さない。実行するコマンドに パスワードが必要な場合、sudo はエラーメッセージを表示して、 終了する。 -P -P (preserve group vector) オプションを指定すると、 sudo は 、sudo を実行するユーザが所属するグループのリストを、変更せ ずにそのまま使用する。 デフォルトでは、sudo は所属グループ の初期値として、変身対象となるユーザが所属するグループのリ ストを設定するのだ。とは言え、実 gid や 実効 gid が変身対象 ユーザと同一になるようにセットされる点には、変わりがない。 -p prompt -p (prompt) オプションを使うと、デフォルトのパスワードプロ ンプトを変更して、好きな文句にすることができる。以下のパー セント (‘%’) エスケープが使用できる。 %H ドメイン名を含むローカルホスト名に展開される (マシンの ホスト名が完全修飾名であるか、sudoers ファイルで fqdn オプションがセットされている場合に有効) %h ドメイン名なしのローカルホスト名に展開 %p パスワードを要求されるユーザ名に展開 (sudoers ファイル のフラグ rootpw、targetpw、runaspw を尊重する) %U 変身対象になる (デフォルトでは root) ユーザのログイン名 に展開される %u sudo を起動するユーザのログイン名に展開される %% 連続した二つの % は、一個の % 文字そのものを意味する -p で指定したプロンプトが、PAM をサポートしているシステムに おいてシステムのパスワードプロンプトを上書きするのは、 sudoers で passprompt_override が有効になっている場合である (訳注: sudoers(5) の passprompt_override の項も参照してほし い)。 -S -S (stdin) オプションを指定すると、sudo はパスワードをター ミナルデバイスからではなく、標準入力から読み込む。 -s [command] -s (shell) は、環境変数 SHELL が設定されていれば、そのシェ ルを、さもなければ、passwd(5) で指定されているシェルを実行 する。コマンドが指定された場合は、それをシェルに渡して実行 させる。コマンドが指定されていなければ、対話的シェルを開く 。 -U user -U (other user) オプションは、-l と組み合わせて使用し、誰の 権限の一覧を表示するかを指定する。root のほかには、現在のホ ストでコマンドに ALL を許されているユーザのみがこのオプショ ンを使用できる。 -u user -u (user) オプションを指定すると、sudo は root 以外のユーザ として指定されたコマンドを実行する。ユーザ名の代わりに uid を指定するときは、#uid という書き方をする。多くのシェルでは 、uid の資格でコマンドを実行するときは、’#’ をバックスラッ シュ (’\’) でエスケープしなければならない。sudoers ファイル の Defaults 行で targetpw オプションが設定されているときは (sudoers(5) 参照)、パスワード・データベースに記載されていな い uid ではコマンドを実行できないことに注意してほしい。 -V -V (version) オプションを指定すると、sudo はバージョンナン バーを表示して、終了する。sudo -V を実行するユーザが root だった場合は、sudo がコンパイルされたときのデフォルト値のリ ストを、マシンのローカル IP アドレスとともに表示する。 -v -v (validate) オプションを指定すると、sudo はユーザのタイム スタンプを更新する。このとき、もし必要なら、パスワードの入 力を促す。このオプションによって sudo のタイムアウト時間が もう 5 分間 (あるいは、何分であれ、sudoers で設定されたタイ ムアウト時間) 伸びるが、このオプションがコマンドを実行する ことはない。 -- -- オプションがあると、sudo はそこでコマンドライン引き数の 処理をやめる。これは -s オプションと組み合わせて使うと、大 変便利である。 さらに、コマンドのためにセットしたい環境変数も、 VAR=value、たとえば LD_LIBRARY_PATH=/usr/local/pkg/lib といった形でコマンドラインから渡すこ とができる。コマンドラインから渡される変数は、通常の環境変数と同じ制限 の対象になるが、一つだけ重要な違いがある。 sudoers ファイルで setenv オ プションが設定されているか、実行されるコマンドに SETENV タグがついてい るか、あるいは、マッチするコマンドが ALL である場合は、ユーザがほかの状 況でなら禁じられているような変数をセットすることができるのだ。詳細につ いては sudoers(5) を参照してほしい。 返り値 プログラムの実行に成功した場合、sudo が返す終了ステータスは、実行したプ ログラムの終了ステータスそのものである。 そうでない場合、設定やパーミッションに問題があったり、sudo が指定された コマンドを実行できなかったりしたときは、sudo は返り値 1 で終了する。後 者の場合は、エラーメッセージが標準エラーに表示される。また、 sudo がユ ーザの PATH にある一つ以上のエントリを stat(2) できなかったときも、エラ ーが標準エラーに出力される (ただし、そのディレクトリが存在しなかったり 、実際にはディレクトリでなかった場合は、そのエントリは無視され、エラー は表示されない)。そうしたことは、正常な環境では起きるはずのないことであ る。 stat(2) が "permission denied" を返す理由で一番よくあるのは、ユー ザーがオートマウントを使用していて、 PATH にあるディレクトリの一つが目 下到達不可能なマシンにある場合だ。 セキュリティに関する注意点 sudo は外部のコマンドをできるだけ安全に実行しようとする。 環境変数を扱うにあたって、二つの異なる行き方がある。デフォルトでは、 sudoers の env_reset オプションが有効になっている。この場合は、最小限の 環境でコマンドが実行されることになるが、その環境とは、 TERM, PATH, HOME, SHELL, LOGNAME, USER, USERNAME、それに sudo という呼び出し側のプ ロセスから来た変数で、sudoers の env_check や env_keep オプションによっ て許可されたものである。言わば、環境変数のホワイトリストが存在すること になるわけだ。 他方、sudoers ファイルで env_reset オプションが無効になっている場合は、 いかなる変数も、env_check や env_delete オプションで明示的に拒否されて いないかぎり、呼び出し側のプロセスから継承される。この場合、env_check や env_delete はブラックリストのように振る舞うわけだ。危険性のある環境 変数のすべてをブラックリストに入れることは不可能なので、env_reset のデ フォルトの動作を使用する方をお勧めする。 どんな場合でも、() で始まる値を持つ環境変数は除去されるが、それは bash の関数と解釈されかねないからだ。 sudo が許可、または拒否する環境変数の リストは、 sudo -V を root として実行したときの出力中に表示される。 たいていのオペレーティングシステムのダイナミック・リンカは、ダイナミッ ク・リンキングを制御する働きのある環境変数を、sudo もその一つである setuid プログラムの環境から除去するものだということに注意してほしい。オ ペレーティングシステムが何であるかにもよるが、そうした環境変数には、 _RLD*, DYLD_*, LD_*, LDR_*, LIBPATH, SHLIB_PATH などが含まれるだろう。 この種の変数は、sudo の実行が始まるよりも前に、環境から除去される。それ 故、sudo がそうした変数を保持することは不可能である。 偽コマンドの実行 (command spoofing) を防止するため、sudo はコマンドを捜 してユーザの PATH を検索する際に、"." と "" (どちらもカレントディレクト リを意味する) を最後に調べる (そのどちらか、あるいは両方が PATH 中に存 在すればだが)。とは言え、環境変数 PATH そのものは変更されずに、そのまま sudo が実行するプログラムに渡されることに注意してほしい。 sudo はタイムスタンプ・ディレクトリ (デフォルトでは /var/run/sudo) の所 有者を調べて、所有者が root でなかったり、 root 以外のユーザにも書き込 みが可能だったりした場合は、そのディレクトリの中身を無視する。root 以外 のユーザでも chown(2) を利用してファイルの所有者を変えられるシステムで は、タイムスタンプ・ディレクトリが (たとえば /tmp といった) 誰にでも書 き込めるディレクトリにあると、一般ユーザが、sudo を実行する前にタイムス タンプ・ディレクトリを作成することが可能になる。もっとも、sudo はタイム スタンプ・ディレクトリとその中身の、所有者とモードをチェックしているの で、起こりえる唯一の被害は、ファイルを「隠匿」する目的でそのディレクト リに入れて置かれることぐらいだ。これはあまりありそうにないことだ。なぜ なら、タイムスタンプ・ディレクトリが root の所有になり、ほかのユーザに よるアクセスが禁じられてしまうと、ファイルをそこに置いたユーザはそれを 回収できなくなるからである。この問題を回避したかったら、誰でも書き込め るわけではないディレクトリ (たとえば /var/adm/sudo) をタイムスタンプ用 に使用するなり、適切な所有者 (root) とパーミッション (0700) を持った /var/run/sudo をシステムのスタートアップ・ファイルで作成するなりすれば よい。 sudo はあまりにも未来に設定したタイムスタンプを認めない。タイムスタンプ が「現在時 + 2 * TIMEOUT」より新しい日時だった場合、sudo はそれを無視し 、ログに記録して警告を発する。このようになっているのは、一般ユーザがフ ァイルの所有者を変えられるシステムで、ユーザがいい加減な日付を付けて勝 手に自分のタイムスタンプを作れないようにするためである。 sudo は通常、自分が明示的に実行したコマンドしかログに記録しないことに気 を付けてほしい。ユーザが sudo su とか sudo sh とかいったコマンドを実行 した場合、そのシェルから続いて実行されるコマンドは記録されないし、sudo によるアクセス制御もそういったコマンドに及ばないことになる。シェル・エ スケープを提供するコマンドについても (たいていのエディタがそれに含まれ る) 同じことが言える。それ故、ユーザに sudo 経由でコマンドの使用を許す ときは、気がつかないうちにそのコマンドがユーザに事実上ルート・シェルを 与えていないかを、念には念を入れて確認しなければならない。もっと詳しい ことが知りたかったら、sudoers(5) の「シェル・エスケープを防止する」のセ クションを御覧になってほしい。 環境変数 sudo は以下の環境変数を利用する。 EDITOR 変数 SUDO_EDITOR や VISUAL が設定されていないとき、-e (sudoedit) モードで使用するデフォルトのエディタ HOME -i や -H モードでは (あるいは、sudo が configure された とき --enable-shell-sets-home オプションが与えられてい れば)、変身対象ユーザのホームディレクトリにセットされる PATH sudoers の secure_path オプションが設定されていれば、無 難な値にセットされる SHELL -s オプションで実行されるシェルを決めるのに使用する SUDO_ASKPASS ターミナルが利用できない場合や、-A オプションが指定され ている場合に、パスワードを読み込むのに使用するヘルパー プログラムのパスを指定する SUDO_COMMAND sudo が実行するコマンドにセットされる SUDO_EDITOR -e (sudoedit) モードで使用するデフォルトのエディタ SUDO_GID sudo を起動したユーザのグループ ID にセットされる SUDO_PROMPT デフォルトのパスワード・プロンプトとして使用する SUDO_PS1 設定すると、実行されるプログラムの PS1 がこの変数の値に セットされる SUDO_UID sudo を起動したユーザのユーザ ID にセットされる SUDO_USER sudo を起動したユーザのログイン名にセットされる USER 変身対象ユーザにセットされる (オプション -u が指定され ていなければ、 root になる) VISUAL 変数 SUDO_EDITOR が設定されていない場合に、 -e (sudoedit) モードで使用するデフォルトのエディタ ファイル /etc/sudoers 誰が何を実行できるかのリスト /var/run/sudo タイムスタンプが存在するディレクトリ /etc/environment Linux や AIX で -i モードを使うときの初期環境 用例 注意: 以下の例は、sudoers(5) に適切な記載があることを前提にしている。 読み取り不可のディレクトリのファイル一覧を取得する。 $ sudo ls /usr/local/protected ユーザ yaz のホームディレクトリのファイル一覧を取得したいのだが、 ~yaz を含むファイルシステムが別のマシンにあって、root でアクセスできるように エクスポートされていない場合。 $ sudo -u yaz ls ~yaz ユーザ www として index.html ファイルを編集する。 $ sudo -u www vi ~www/htdocs/index.html root と adm グループのユーザだけがアクセスできるシステムログを閲覧する 。 $ sudo -g adm view /var/log/syslog jim に変身してエディタを実行する。プライマリグループには別のグループを 指定する。 $ sudo -u jim -g audio vi ~jim/sound.txt マシンをリブートする。 $ sudo shutdown -r +15 "quick reboot" /home パーティションに存在するディレクトリのディスク使用量リストを作成 する。cd やファイル・リダイレクションがきちんと動作するように、コマンド をサブシェルで実行していることに注目してほしい。 $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE" 関連項目 grep(1), su(1), stat(2), passwd(5), sudoers(5), visudo(8) 作者 多数の人々が長年に渡って sudo の製作に取り組んできた。当バージョンは主 として次の者が書いたコードからできている。 Todd C. Miller sudo の簡単な履歴については、配布物中の HISTORY ファイルか、 http://www.sudo.ws/sudo/history.html を御覧いただきたい。 警告 ユーザが sudo 経由で任意のコマンドの実行を許されているならば、そのユー ザがルート・シェルを獲得するのを防止する簡単な方法はない。また、(エディ タを含む) 多くのプログラムが、シェル・エスケープを通してユーザがコマン ドを実行できるようにしており、この方法でユーザは sudo のチェックを回避 することができる。とは言え、たいていのシステムでは、 sudo の noexec 機 能を用いてシェル・エスケープを阻止することが可能だ。詳細については、 sudoers(5) のマニュアルを参照してほしい。 下記のように sudo を通して直接 cd コマンドを実行しても意味がない。 $ sudo cd /usr/local/protected なぜなら、コマンドが終了したとき、その親プロセス (あなたのシェルだ) は 相変わらず元の状態のままだからだ。より詳しく知りたかったら、「用例」セ クションを御覧になってほしい。 もし sudoers ファイルでユーザに許可するコマンドが ALL になっているなら ば、ユーザ設定でどんな項目に ’!’ を付けようとも、ユーザが自分でプログ ラムを作って、ルート・シェルを獲得するのを防ぐすべはまったくない。 sudo を介してシェルスクリプトを実行すると、ある種のオペレーティングシス テムで setuid シェルスクリプトを危険なものにしているのと同一のカーネル のバグが表面化するおそれがある (使用している OS に /dev/fd/ ディレクト リがあれば、setuid シェルスクリプトはたいてい安全である)。 バグ sudo にバクを発見したと思ったら、下記のページにアクセスして、バグレポー トを提出していただきたい。 http://www.sudo.ws/sudo/bugs/ サポート ある程度の無料サポートが sudo-users メーリングリストを通して利用できる 。購読やアーカイブの検索には下記 URL を御覧になること。 http://www.sudo.ws/mailman/listinfo/sudo-users 免責 sudo は「現状のまま」提供される。明示的な、あるいは黙示的ないかなる保証 も、商品性や特定目的への適合性についての黙示的な保証を含め、またそれの みに止まらず、これを否認する。詳細な全文については、 sudo と一緒に配布 されている LICENSE ファイルや下記 Web ページを御覧いただきたい。 http://www.sudo.ws/sudo/license.html 1.7.2p1 June 15, 2009 SUDO(8)
SUDO(8) BSD System Manager’s Manual SUDO(8) NAME sudo, sudoedit - execute a command as another user SYNOPSIS sudo -h | -K | -k | -V sudo -v [-AknS] [-g group name | #gid] [-p prompt] [-u user name | #uid] sudo -l[l] [-AknS] [-g group name | #gid] [-p prompt] [-U user name] [-u user name | #uid] [command] sudo [-AbEHnPS] [-C fd] [-g group name | #gid] [-p prompt] [-r role] [-t type] [-u user name | #uid] [VAR=value] -i | -s [command] sudoedit [-AnS] [-C fd] [-g group name | #gid] [-p prompt] [-u user name | #uid] file ... DESCRIPTION sudo allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. sudo supports a plugin architecture for security policies and input/out- put logging. Third parties can develop and distribute their own policy and I/O logging plugins to work seamlessly with the sudo front end. The default security policy is sudoers, which is configured via the file /etc/sudoers, or via LDAP. See the PLUGINS section for more information. The security policy determines what privileges, if any, a user has to run sudo. The policy may require that users authenticate themselves with a password or another authentication mechanism. If authentication is required, sudo will exit if the user’s password is not entered within a configurable time limit. This limit is policy-specific; the default password prompt timeout for the sudoers security policy is 5 minutes. Security policies may support credential caching to allow the user to run sudo again for a period of time without requiring authentication. The sudoers policy caches credentials for 5 minutes, unless overridden in sudoers(5). By running sudo with the -v option, a user can update the cached credentials without running a command. When invoked as sudoedit, the -e option (described below), is implied. Security policies may log successful and failed attempts to use sudo. If an I/O plugin is configured, the running command’s input and output may be logged as well. The options are as follows: -A Normally, if sudo requires a password, it will read it from the user’s terminal. If the -A (askpass) option is speci- fied, a (possibly graphical) helper program is executed to read the user’s password and output the password to the stan- dard output. If the SUDO_ASKPASS environment variable is set, it specifies the path to the helper program. Otherwise, if /etc/sudo.conf contains a line specifying the askpass pro- gram, that value will be used. For example: # Path to askpass helper program Path askpass /usr/X11R6/bin/ssh-askpass If no askpass program is available, sudo will exit with an error. -b The -b (background) option tells sudo to run the given com- mand in the background. Note that if you use the -b option you cannot use shell job control to manipulate the process. Most interactive commands will fail to work properly in back- ground mode. -C fd Normally, sudo will close all open file descriptors other than standard input, standard output and standard error. The -C (close from) option allows the user to specify a starting point above the standard error (file descriptor three). Val- ues less than three are not permitted. The security policy may restrict the user’s ability to use the -C option. The sudoers policy only permits use of the -C option when the administrator has enabled the closefrom_override option. -E The -E (preserve environment) option indicates to the secu- rity policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment. -e The -e (edit) option indicates that, instead of running a command, the user wishes to edit one or more files. In lieu of a command, the string "sudoedit" is used when consulting the security policy. If the user is authorized by the pol- icy, the following steps are taken: 1. Temporary copies are made of the files to be edited with the owner set to the invoking user. 2. The editor specified by the policy is run to edit the temporary files. The sudoers policy uses the SUDO_EDITOR, VISUAL and EDITOR environment variables (in that order). If none of SUDO_EDITOR, VISUAL or EDITOR are set, the first program listed in the editor sudoers(5) option is used. 3. If they have been modified, the temporary files are copied back to their original location and the temporary versions are removed. If the specified file does not exist, it will be created. Note that unlike most commands run by sudo, the editor is run with the invoking user’s environment unmodified. If, for some reason, sudo is unable to update a file with its edited version, the user will receive a warning and the edited copy will remain in a temporary file. -g group Normally, sudo runs a command with the primary group set to the one specified by the password database for the user the command is being run as (by default, root). The -g (group) option causes sudo to run the command with the primary group set to group instead. To specify a gid instead of a group name, use #gid. When running commands as a gid, many shells require that the ‘#’ be escaped with a backslash (‘\’). If no -u option is specified, the command will be run as the invoking user (not root). In either case, the primary group will be set to group. -H The -H (HOME) option requests that the security policy set the HOME environment variable to the home directory of the target user (root by default) as specified by the password database. Depending on the policy, this may be the default behavior. -h The -h (help) option causes sudo to print a short help mes- sage to the standard output and exit. -i [command] The -i (simulate initial login) option runs the shell speci- fied by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution via the shell’s -c option. If no command is specified, an interactive shell is executed. sudo attempts to change to that user’s home directory before running the shell. The security policy shall initialize the environment to a minimal set of variables, similar to what is present when a user logs in. The Command Environment section in the sudoers(5) manual documents how the -i option affects the environment in which a command is run when the sudoers policy is in use. -K The -K (sure kill) option is like -k except that it removes the user’s cached credentials entirely and may not be used in conjunction with a command or other option. This option does not require a password. Not all security policies support credential caching. -k [command] When used alone, the -k (kill) option to sudo invalidates the user’s cached credentials. The next time sudo is run a pass- word will be required. This option does not require a pass- word and was added to allow a user to revoke sudo permissions from a .logout file. Not all security policies support cre- dential caching. When used in conjunction with a command or an option that may require a password, the -k option will cause sudo to ignore the user’s cached credentials. As a result, sudo will prompt for a password (if one is required by the security policy) and will not update the user’s cached credentials. -l[l] [command] If no command is specified, the -l (list) option will list the allowed (and forbidden) commands for the invoking user (or the user specified by the -U option) on the current host. If a command is specified and is permitted by the security policy, the fully-qualified path to the command is displayed along with any command line arguments. If command is speci- fied but not allowed, sudo will exit with a status value of 1. If the -l option is specified with an l argument (i.e. -ll), or if -l is specified multiple times, a longer list format is used. -n The -n (non-interactive) option prevents sudo from prompting the user for a password. If a password is required for the command to run, sudo will display an error message and exit. -P The -P (preserve group vector) option causes sudo to preserve the invoking user’s group vector unaltered. By default, the sudoers policy will initialize the group vector to the list of groups the target user is in. The real and effective group IDs, however, are still set to match the target user. -p prompt The -p (prompt) option allows you to override the default password prompt and use a custom one. The following percent (‘%’) escapes are supported by the sudoers policy: %H expanded to the host name including the domain name (on if the machine’s host name is fully qualified or the fqdn option is set in sudoers(5)) %h expanded to the local host name without the domain name %p expanded to the name of the user whose password is being requested (respects the rootpw, targetpw, and runaspw flags in sudoers(5)) %U expanded to the login name of the user the command will be run as (defaults to root unless the -u option is also specified) %u expanded to the invoking user’s login name %% two consecutive ‘%’ characters are collapsed into a sin- gle ‘%’ character The prompt specified by the -p option will override the sys- tem password prompt on systems that support PAM unless the passprompt_override flag is disabled in sudoers. -r role The -r (role) option causes the new (SELinux) security con- text to have the role specified by role. -S The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The pass- word must be followed by a newline character. -s [command] The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in the password database. If a command is specified, it is passed to the shell for execution via the shell’s -c option. If no command is specified, an interactive shell is executed. -t type The -t (type) option causes the new (SELinux) security con- text to have the type specified by type. If no type is spec- ified, the default type is derived from the specified role. -U user The -U (other user) option is used in conjunction with the -l option to specify the user whose privileges should be listed. The security policy may restrict listing other users’ privi- leges. The sudoers policy only allows root or a user with the ALL privilege on the current host to use this option. -u user The -u (user) option causes sudo to run the specified command as a user other than root. To specify a uid instead of a user name, #uid. When running commands as a uid, many shells require that the ‘#’ be escaped with a backslash (‘\’). Security policies may restrict uids to those listed in the password database. The sudoers policy allows uids that are not in the password database as long as the targetpw option is not set. Other security policies may not support this. -V The -V (version) option causes sudo to print its version string and the version string of the security policy plugin and any I/O plugins. If the invoking user is already root the -V option will display the arguments passed to configure when sudo was built and plugins may display more verbose information such as default options. -v When given the -v (validate) option, sudo will update the user’s cached credentials, authenticating the user’s password if necessary. For the sudoers plugin, this extends the sudo timeout for another 5 minutes (or whatever the timeout is set to by the security policy) but does not run a command. Not all security policies support cached credentials. -- The -- option indicates that sudo should stop processing com- mand line arguments. Environment variables to be set for the command may also be passed on the command line in the form of VAR=value, e.g. LD_LIBRARY_PATH=/usr/local/pkg/lib. Variables passed on the command line are subject to the same restrictions as normal environment variables with one important exception. If the setenv option is set in sudoers, the command to be run has the SETENV tag set or the command matched is ALL, the user may set variables that would otherwise be forbidden. See sudoers(5) for more information. COMMAND EXECUTION When sudo executes a command, the security policy specifies the execution envionment for the command. Typically, the real and effective uid and gid are set to match those of the target user, as specified in the pass- word database, and the group vector is initialized based on the group database (unless the -P option was specified). The following parameters may be specified by security policy: · real and effective user ID · real and effective group ID · supplementary group IDs · the environment list · current working directory · file creation mode mask (umask) · SELinux role and type · scheduling priority (aka nice value) Process model When sudo runs a command, it calls fork(2), sets up the execution envi- ronment as described above, and calls the execve system call in the child process. The main sudo process waits until the command has completed, then passes the command’s exit status to the security policy’s close method and exits. If an I/O logging plugin is configured, a new pseudo- terminal (“pty”) is created and a second sudo process is used to relay job control signals between the user’s existing pty and the new pty the command is being run in. This extra process makes it possible to, for example, suspend and resume the command. Without it, the command would be in what POSIX terms an “orphaned process group” and it would not receive any job control signals. Signal handling Because the command is run as a child of the sudo process, sudo will relay signals it receives to the command. Unless the command is being run in a new pty, the SIGHUP, SIGINT and SIGQUIT signals are not relayed unless they are sent by a user process, not the kernel. Otherwise, the command would receive SIGINT twice every time the user entered control-C. Some signals, such as SIGSTOP and SIGKILL, cannot be caught and thus will not be relayed to the command. As a general rule, SIGTSTP should be used instead of SIGSTOP when you wish to suspend a command being run by sudo. As a special case, sudo will not relay signals that were sent by the com- mand it is running. This prevents the command from accidentally killing itself. On some systems, the reboot(8) command sends SIGTERM to all non- system processes other than itself before rebooting the systyem. This prevents sudo from relaying the SIGTERM signal it received back to reboot(8), which might then exit before the system was actually rebooted, leaving it in a half-dead state similar to single user mode. Note, how- ever, that this check only applies to the command run by sudo and not any other processes that the command may create. As a result, running a script that calls reboot(8) or shutdown(8) via sudo may cause the system to end up in this undefined state unless the reboot(8) or shutdown(8) are run using the exec() family of functions instead of system() (which interposes a shell between the command and the calling process). PLUGINS Plugins are dynamically loaded based on the contents of the /etc/sudo.conf file. If no /etc/sudo.conf file is present, or it con- tains no Plugin lines, sudo will use the traditional sudoers security policy and I/O logging, which corresponds to the following /etc/sudo.conf file. # # Default /etc/sudo.conf file # # Format: # Plugin plugin_name plugin_path plugin_options ... # Path askpass /path/to/askpass # Path noexec /path/to/sudo_noexec.so # Debug sudo /var/log/sudo_debug all@warn # Set disable_coredump true # # The plugin_path is relative to /usr/libexec unless # fully qualified. # The plugin_name corresponds to a global symbol in the plugin # that contains the plugin interface structure. # The plugin_options are optional. # Plugin policy_plugin sudoers.so Plugin io_plugin sudoers.so A Plugin line consists of the Plugin keyword, followed by the symbol_name and the path to the shared object containing the plugin. The symbol_name is the name of the struct policy_plugin or struct io_plugin in the plugin shared object. The path may be fully qualified or relative. If not fully qualified it is relative to the /usr/libexec directory. Any addi- tional parameters after the path are passed as arguments to the plugin’s open function. Lines that don’t begin with Plugin, Path, Debug, or Set are silently ignored. For more information, see the sudo_plugin(8) manual. PATHS A Path line consists of the Path keyword, followed by the name of the path to set and its value. E.g. Path noexec /usr/libexec/sudo_noexec.so Path askpass /usr/X11R6/bin/ssh-askpass The following plugin-agnostic paths may be set in the /etc/sudo.conf file: askpass The fully qualified path to a helper program used to read the user’s password when no terminal is available. This may be the case when sudo is executed from a graphical (as opposed to text-based) application. The program specified by askpass should display the argument passed to it as the prompt and write the user’s password to the standard output. The value of askpass may be overridden by the SUDO_ASKPASS environment vari- able. noexec The fully-qualified path to a shared library containing dummy versions of the execv(), execve() and fexecve() library func- tions that just return an error. This is used to implement the noexec functionality on systems that support LD_PRELOAD or its equivalent. Defaults to /usr/libexec/sudo_noexec.so. DEBUG FLAGS sudo versions 1.8.4 and higher support a flexible debugging framework that can help track down what sudo is doing internally if there is a problem. A Debug line consists of the Debug keyword, followed by the name of the program to debug (sudo, visudo, sudoreplay), the debug file name and a comma-separated list of debug flags. The debug flag syntax used by sudo and the sudoers plugin is subsystem@priority but the plugin is free to use a different format so long as it does not include a comma (‘,’). For instance: Debug sudo /var/log/sudo_debug all@warn,plugin@info would log all debugging statements at the warn level and higher in addi- tion to those at the info level for the plugin subsystem. Currently, only one Debug entry per program is supported. The sudo Debug entry is shared by the sudo front end, sudoedit and the plugins. A future release may add support for per-plugin Debug lines and/or support for multiple debugging files for a single program. The priorities used by the sudo front end, in order of decreasing sever- ity, are: crit, err, warn, notice, diag, info, trace and debug. Each priority, when specified, also includes all priorities higher than it. For example, a priority of notice would include debug messages logged at notice and higher. The following subsystems are used by the sudo front-end: all matches every subsystem args command line argument processing conv user conversation edit sudoedit exec command execution main sudo main function netif network interface handling pcomm communication with the plugin plugin plugin configuration pty pseudo-tty related code selinux SELinux-specific handling util utility functions utmp utmp handling EXIT VALUE Upon successful execution of a program, the exit status from sudo will simply be the exit status of the program that was executed. Otherwise, sudo exits with a value of 1 if there is a configuration/per- mission problem or if sudo cannot execute the given command. In the lat- ter case the error string is printed to the standard error. If sudo can- not stat(2) one or more entries in the user’s PATH, an error is printed on stderr. (If the directory does not exist or if it is not really a directory, the entry is ignored and no error is printed.) This should not happen under normal circumstances. The most common reason for stat(2) to return “permission denied” is if you are running an auto- mounter and one of the directories in your PATH is on a machine that is currently unreachable. SECURITY NOTES sudo tries to be safe when executing external commands. To prevent command spoofing, sudo checks "." and "" (both denoting cur- rent directory) last when searching for a command in the user’s PATH (if one or both are in the PATH). Note, however, that the actual PATH envi- ronment variable is not modified and is passed unchanged to the program that sudo executes. Please note that sudo will normally only log the command it explicitly runs. If a user runs a command such as sudo su or sudo sh, subsequent commands run from that shell are not subject to sudo’s security policy. The same is true for commands that offer shell escapes (including most editors). If I/O logging is enabled, subsequent commands will have their input and/or output logged, but there will not be traditional logs for those commands. Because of this, care must be taken when giving users access to commands via sudo to verify that the command does not inadver- tently give the user an effective root shell. For more information, please see the PREVENTING SHELL ESCAPES section in sudoers(5). To prevent the disclosure of potentially sensitive information, sudo dis- ables core dumps by default while it is executing (they are re-enabled for the command that is run). To aid in debugging sudo crashes, you may wish to re-enable core dumps by setting “disable_coredump” to false in the /etc/sudo.conf file as follows: Set disable_coredump false Note that by default, most operating systems disable core dumps from setuid programs, which includes sudo. To actually get a sudo core file you may need to enable core dumps for setuid processes. On BSD and Linux systems this is accomplished via the sysctl command, on Solaris the core- adm command can be used. ENVIRONMENT sudo utilizes the following environment variables. The security policy has control over the actual content of the command’s environment. EDITOR Default editor to use in -e (sudoedit) mode if neither SUDO_EDITOR nor VISUAL is set. MAIL In -i mode or when env_reset is enabled in sudoers, set to the mail spool of the target user. HOME Set to the home directory of the target user if -i or -H are specified, env_reset or always_set_home are set in sudoers, or when the -s option is specified and set_home is set in sudoers. PATH May be overridden by the security policy. SHELL Used to determine shell to run with -s option. SUDO_ASKPASS Specifies the path to a helper program used to read the password if no terminal is available or if the -A option is specified. SUDO_COMMAND Set to the command run by sudo. SUDO_EDITOR Default editor to use in -e (sudoedit) mode. SUDO_GID Set to the group ID of the user who invoked sudo. SUDO_PROMPT Used as the default password prompt. SUDO_PS1 If set, PS1 will be set to its value for the program being run. SUDO_UID Set to the user ID of the user who invoked sudo. SUDO_USER Set to the login name of the user who invoked sudo. USER Set to the target user (root unless the -u option is specified). VISUAL Default editor to use in -e (sudoedit) mode if SUDO_EDITOR is not set. FILES /etc/sudo.conf sudo front end configuration EXAMPLES Note: the following examples assume a properly configured security pol- icy. To get a file listing of an unreadable directory: $ sudo ls /usr/local/protected To list the home directory of user yaz on a machine where the file system holding ~yaz is not exported as root: $ sudo -u yaz ls ~yaz To edit the index.html file as user www: $ sudo -u www vi ~www/htdocs/index.html To view system logs only accessible to root and users in the adm group: $ sudo -g adm view /var/log/syslog To run an editor as jim with a different primary group: $ sudo -u jim -g audio vi ~jim/sound.txt To shut down a machine: $ sudo shutdown -r +15 "quick reboot" To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redi- rection work. $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE" SEE ALSO grep(1), su(1), stat(2), passwd(5), sudoers(5), sudo_plugin(8), sudoreplay(8), visudo(8) HISTORY See the HISTORY file in the sudo distribution (http://www.sudo.ws/sudo/history.html) for a brief history of sudo. AUTHORS Many people have worked on sudo over the years; this version consists of code written primarily by: Todd C. Miller See the CONTRIBUTORS file in the sudo distribution (http://www.sudo.ws/sudo/contributors.html) for an exhaustive list of people who have contributed to sudo. CAVEATS There is no easy way to prevent a user from gaining a root shell if that user is allowed to run arbitrary commands via sudo. Also, many programs (such as editors) allow the user to run commands via shell escapes, thus avoiding sudo’s checks. However, on most systems it is possible to pre- vent shell escapes with the sudoers(5) plugin’s noexec functionality. It is not meaningful to run the cd command directly via sudo, e.g., $ sudo cd /usr/local/protected since when the command exits the parent process (your shell) will still be the same. Please see the EXAMPLES section for more information. Running shell scripts via sudo can expose the same kernel bugs that make setuid shell scripts unsafe on some operating systems (if your OS has a /dev/fd/ directory, setuid shell scripts are generally safe). BUGS If you feel you have found a bug in sudo, please submit a bug report at http://www.sudo.ws/sudo/bugs/ SUPPORT Limited free support is available via the sudo-users mailing list, see http://www.sudo.ws/mailman/listinfo/sudo-users to subscribe or search the archives. DISCLAIMER sudo is provided “AS IS” and any express or implied warranties, includ- ing, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. See the LICENSE file distributed with sudo or http://www.sudo.ws/sudo/license.html for com- plete details. Sudo 1.8.6p3 July 10, 2012 Sudo 1.8.6p3
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa