「Sudo」の版間の差分
細編集の要約なし |
→ヒントとテクニック: 同期 |
||
| (7人の利用者による、間の52版が非表示) | |||
| 1行目: | 1行目: | ||
[[Category:セキュリティ]] |
[[Category:セキュリティ]] |
||
[[Category:コマンド]] |
|||
[[cs:Sudo]] |
|||
[[de:sudo]] |
|||
[[en:Sudo]] |
[[en:Sudo]] |
||
[[es:Sudo]] |
[[es:Sudo]] |
||
[[fr:Sudo]] |
[[fr:Sudo]] |
||
[[ |
[[pt:Sudo]] |
||
[[ru:Sudo]] |
[[ru:Sudo]] |
||
[[sr:Sudo]] |
|||
[[tr:Sudo]] |
|||
[[uk:Sudo]] |
[[uk:Sudo]] |
||
[[zh- |
[[zh-hans:Sudo]] |
||
{{Related articles start}} |
{{Related articles start}} |
||
{{Related|ユーザーとグループ}} |
{{Related|ユーザーとグループ}} |
||
{{ |
{{Related|su}} |
||
{{Related|doas}} |
|||
{{Related articles end}} |
{{Related articles end}} |
||
[ |
[https://www.sudo.ws/sudo/ sudo] を使うことでシステム管理者は権限を委託できるようになり、root や他のユーザーとして複数の(もしくは全ての)コマンドを実行する能力を特定のユーザー(もしくはユーザーのグループ)に与えることができます。 |
||
Sudo は root でコマンドを実行するためのものとして [[su]] に代わるものです。[[su]] が root シェルを起動してそれから全てのコマンドを root として実行できるようにするのに対し、sudo は一つのコマンドだけに特権を与えます。必要な時だけに root 特権を有効にするので、sudo を使うことにより打ち間違いや呼び出されたコマンドのバグによってシステムを破壊してしまう可能性を減らすことができます。 |
|||
== 原理 == |
|||
Sudo は root でコマンドを実行するためのものとして [[su|su]] に代わるものです。[[su|su]] が root シェルを起動してそれから全てのコマンドを root として実行できるようにするのに対し、sudo は一つのコマンドだけに特権を与えます。必要な時だけに root 特権を有効にするので、sudo を使うことにより打ち間違いや呼び出されたコマンドのバグによってシステムを破壊してしまう可能性を減らすことができます。 |
|||
Sudo は他のユーザーとしてコマンドを実行するために使うこともできます; さらに、sudo は全てのコマンドとセキュリティ検査のアクセス失敗を記録します。 |
Sudo は他のユーザーとしてコマンドを実行するために使うこともできます; さらに、sudo は全てのコマンドとセキュリティ検査のアクセス失敗を記録します。 |
||
== インストール == |
== インストール == |
||
{{Pkg|sudo}} パッケージをインストールしてください。あるいは、{{Pkg|sudo-rs}}にsudo-rsというメモリ安全なsudoの実装があります。 |
|||
== 使用方法 == |
|||
# pacman -S sudo |
|||
{{ic|sudo}} を |
ユーザーはコマンドの前に {{ic|sudo}} を付けることで、スーパーユーザー(もしくはその他の)特権がある状態でコマンドを実行できます。 |
||
''sudo'' を使うときはコマンドの前に {{ic|sudo}} と空白を付けてください: |
|||
== 使用方法 == |
|||
$ sudo ''cmd'' |
|||
ユーザーはコマンドの前に {{ic|sudo}} を付けることで、スーパーユーザー(もしくはその他の)特権がある状態でコマンドを実行できます。 |
|||
例えば、pacman を root で使いたいなら: |
例えば、pacman を root で使いたいなら: |
||
| 38行目: | 37行目: | ||
$ sudo pacman -Syu |
$ sudo pacman -Syu |
||
詳しくは |
詳しくは {{man|8|sudo}} を見てください。 |
||
=== ログインシェル === |
|||
すべてのコマンドを他のユーザーとして実行するために単に ''sudo'' を前置するだけでは不十分です。特に、[[Wikipedia:Redirection_(computing)|リダイレクション]]や[[Wikipedia:Command substitution|コマンド置換]]を使用する場合、ログインシェルを使用する必要があります。これは {{ic|sudo -iu ''user''}} で簡単にアクセスできます(目的のユーザーが root である場合は {{ic|-u ''user''}} を省略できます)。 |
|||
次の例では、コマンド置換はフルシェルで動作しますが、''sudo'' を前置すると失敗します: |
|||
{{hc|$ sudo wpa_supplicant -B -i ''interface'' -c '''<('''wpa_passphrase ''MYSSID passphrase''''')'''| |
|||
Successfully initialized wpa_supplicant |
|||
Failed to open config file '/dev/fd/63', error: No such file or directory |
|||
Failed to read or parse configuration '/dev/fd/63' |
|||
}} |
|||
== 設定 == |
== 設定 == |
||
=== デフォルトのスケルトンファイル === |
|||
{{man|5|sudoers|SUDOERS OPTIONS}} は {{ic|/etc/sudoers}} ファイル内の {{ic|Defaults}} コマンドで使用できる全てのオプションをリストアップします。 |
|||
オプションのリスト (バージョン 1.8.7 のソースコードから解析されたもの) を {{ic|sudoers}} 用に最適化した形式で表示するには [https://gist.github.com/AladW/7eca9799b9ea624eca31] を参照してください。 |
|||
パスワードのタイムアウトの設定など、より詳しい情報は {{man|5|sudoers}} を見て下さい。 |
|||
=== 現在の設定を表示 === |
=== 現在の設定を表示 === |
||
{{ic|sudo -ll}} を実行して現在の sudo 設定を表示してください。 |
{{ic|sudo -ll}} を実行して現在の sudo 設定を表示してください。{{ic|sudo -lU ''user''}} で指定したユーザーの sudo 設定も確認できます。 |
||
=== visudo を使う === |
=== visudo を使う === |
||
| 50行目: | 69行目: | ||
sudo の設定ファイルは {{ic|/etc/sudoers}} です。このファイルは'''どんなときでも''' {{ic|visudo}} コマンドを使って編集するべきです。{{ic|visudo}} は {{ic|sudoers}} ファイルをロックし、一時ファイルに編集内容を保存して、それを {{ic|/etc/sudoers}} にコピーする前にファイルの構文をチェックします。 |
sudo の設定ファイルは {{ic|/etc/sudoers}} です。このファイルは'''どんなときでも''' {{ic|visudo}} コマンドを使って編集するべきです。{{ic|visudo}} は {{ic|sudoers}} ファイルをロックし、一時ファイルに編集内容を保存して、それを {{ic|/etc/sudoers}} にコピーする前にファイルの構文をチェックします。 |
||
{{Warning| |
|||
{{Warning|{{ic|sudoers}} に構文エラーの危険性があるということは絶対避けなくてはいけません!どんなエラーでも sudo を使用不可能にしてしまいます。エラーを防ぐために'''いつでも''' {{ic|visudo}} を使って編集するようにしてください。}} |
|||
* {{ic|sudoers}} に構文エラーの危険性があるということは絶対避けなくてはいけません!どんなエラーでも sudo を使用不可能にしてしまいます。エラーを防ぐためにどんなときでも {{ic|visudo}} を使って編集するようにしてください。 |
|||
* {{man|8|visudo}} は、ユーザーが選択したエディターのユーザー環境変数を尊重するように visudo を設定すると、その変数を別の値に設定するだけで、visudo 権限を持つユーザーがログを記録せずに root として任意のコマンドを実行できるようになるため、セキュリティ ホールになる可能性があると警告しています。}} |
|||
visudo のデフォルトエディタは {{ic|vi}} です。 |
visudo のデフォルトエディタは {{ic|vi}} です。core リポジトリの sudo はデフォルトで {{Ic|--with-env-editor}} を有効にしてコンパイルされており、{{Ic|VISUAL}} や {{Ic|EDITOR}} 変数を使用することができます。{{ic|VISUAL}} が設定されている場合 {{ic|EDITOR}} は使われません。 |
||
現在のシェルセッションの間だけ '''visudo''' のエディタとして nano を使いたい場合、'''visudo''' を実行する前に {{Ic|EDITOR}} 変数を設定してください: |
|||
# EDITOR=nano visudo |
# EDITOR=nano visudo |
||
もしくは {{ic|/etc/sudoers}} ファイルのコピーを編集して {{ic|visudo -c -f ''/copy/of/sudoers''}} でチェックすることもできます。visudo でファイルをロックしたくない場合に有用です。 |
|||
ただし既に動作しているシェルには export の効果はありません。 |
|||
エディタを永久に変更するには、[[環境変数#ユーザーごと]]を参照してください。システム全体で ''visudo'' のみのエディタを永久に変更するには、以下を {{ic|/etc/sudoers}} に追加します([[nano]] が好みのエディタであると仮定します): |
|||
# デフォルトのエディタを nano の制限付きバージョンに設定し、visudo が EDITOR/VISUAL を使用しないようにする。 |
|||
# Reset environment by default |
|||
Defaults |
Defaults editor=/usr/bin/rnano, !env_editor |
||
# Set default EDITOR to nano, and do not allow visudo to use EDITOR/VISUAL. |
|||
Defaults editor=/usr/bin/nano, !env_editor |
|||
=== エントリの例 === |
=== エントリの例 === |
||
| 79行目: | 100行目: | ||
%wheel ALL=(ALL) ALL |
%wheel ALL=(ALL) ALL |
||
{{Tip|新しい管理者を作成する際、{{ic|wheel}} グループに sudo アクセスを有効にし、[[ユーザーとグループ#グループ管理|ユーザーを追加する]]ことが望ましいです。デフォルトでは、[[Polkit#Administrator identities|Polkit]] は {{ic|wheel}} グループのメンバーを管理者として扱うためです。ユーザーが {{ic|wheel}} のメンバーでない場合、Polkit を使用するソフトウェアはユーザーのパスワードではなく root のパスワードを使用して認証を求めることがあります。}} |
|||
ユーザー USER_NAME にパスワードを尋ねるのを無効化するには: |
|||
ユーザー {{ic|USER_NAME}} にパスワードを尋ねるのを無効化するには: |
|||
{{Warning|これにより、ユーザー名で実行されている任意のプロセスが、許可を求めずに sudo を使用できるようになります。}} |
|||
Defaults:USER_NAME !authenticate |
Defaults:USER_NAME !authenticate |
||
ユーザー USER_NAME |
ホスト {{ic|HOST_NAME}} 上のユーザー {{ic|USER_NAME}} のみに対して明示的に定義されたコマンドを有効にするには: |
||
USER_NAME HOST_NAME=/usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot,/usr/bin/pacman -Syu |
USER_NAME HOST_NAME=/usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot,/usr/bin/pacman -Syu |
||
{{note|後ろの行の設定が前の設定よりも優先されるので、細かくカスタマイズしたオプションはファイルの最後に置くべきです。特にユーザーがグループに含まれているときは上のような行は {{ic|%wheel}} 行の後にすべきです。}} |
{{note|後ろの行の設定が前の設定よりも優先されるので、細かくカスタマイズしたオプションはファイルの最後に置くべきです。特にユーザーがグループに含まれているときは上のような行は {{ic|%wheel}} 行の後にすべきです。}} |
||
ユーザー USER_NAME が ホスト HOST_NAME を使っているときだけ特定のコマンドに sudo をパスワードなしで使えるようにするには: |
ユーザー USER_NAME が ホスト HOST_NAME を使っているときだけ特定のコマンドに sudo をパスワードなしで使えるようにするには: |
||
USER_NAME HOST_NAME= NOPASSWD: /usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot,/usr/bin/pacman -Syu |
USER_NAME HOST_NAME= NOPASSWD: /usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot,/usr/bin/pacman -Syu |
||
説明のついた sudoers のサンプルは |
説明のついた sudoers のサンプルは {{ic|/usr/share/doc/sudo/examples/sudoers}} にあります。詳しくは {{man|5|sudoers}} を見て下さい。 |
||
=== Sudoers デフォルトファイルパーミッション === |
=== Sudoers デフォルトファイルパーミッション === |
||
| 99行目: | 126行目: | ||
# chmod -c 0440 /etc/sudoers |
# chmod -c 0440 /etc/sudoers |
||
== ヒントとテクニック == |
|||
=== パスワードキャッシュのタイムアウト === |
|||
=== パスワードプロンプトのタイムアウトを無効にする === |
|||
キャッシュされたパスワードの有効期間の前にデフォルトのタイムアウトを変えることができます。{{ic|/etc/sudoers}} の timestamp_timeout でこれができます。単位は分です。 |
|||
タイムアウトを20分に設定: |
|||
よくある悩みは、通常のパーミッションで実行され、必要な時にのみ上昇するバックグラウンドターミナルで実行される長時間実行プロセスです。これにより sudo のパスワード入力が促され、気づかれずにタイムアウトし、その時点でプロセスが終了し、完了した作業は失われるか、せいぜいキャッシュされます。一般的なアドバイスは、パスワードなしの sudo を有効にするか、パスワードを記憶する sudo のタイムアウトを延長することです。これらはいずれもセキュリティに悪影響を及ぼします。 '''プロンプト''' タイムアウトを無効にすることもできますが、これは適切なセキュリティ上の目的を果たすものではありません、そこを理解して設定する必要があります。 |
|||
Defaults:USER_NAME timestamp_timeout=20 |
|||
Defaults passwd_timeout=0 |
|||
{{Tip|sudo が毎回パスワードをきくようにしたいのなら、タイムアウトを 0 に設定してください。パスワードのタイムアウトをなくしたいのなら、0 未満に設定してください。}} |
|||
== |
=== Bash 補完 === |
||
sudo コマンドで完全な Bash 補完機能を利用できるようにするには、{{Pkg|bash-completion}} パッケージをインストールしてください。 |
|||
=== ファイル例 === |
|||
何らかの理由でこのパッケージをインストールできない場合、sudo コマンドの補完機能は限定的になりますが、代替手段として以下の内容を [[.bashrc]] に追加することで利用可能です: |
|||
complete -cf sudo |
|||
この例は screen, tmux, ratpoison などのターミナルマルチプレクサを使っている人や、スクリプトや cron ジョブから sudo を使っている人に役立つと思われます。 |
|||
=== エイリアスを渡す === |
|||
{{hc|/etc/sudoers|<nowiki> |
|||
Cmnd_Alias WHEELER = /usr/bin/lsof, /bin/nice, /bin/ps, /usr/bin/top, /usr/local/bin/nano, /usr/bin/ss, /usr/bin/locate, /usr/bin/find, /usr/bin/rsync |
|||
Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/nice, /usr/bin/ionice, /usr/bin/top, /usr/bin/kill, /usr/bin/killall, /usr/bin/ps, /usr/bin/pkill |
|||
Cmnd_Alias EDITS = /usr/bin/vim, /usr/bin/nano, /usr/bin/cat, /usr/bin/vi |
|||
Cmnd_Alias ARCHLINUX = /usr/bin/gparted, /usr/bin/pacman |
|||
[[Zsh]] と [[Bash]] では、エイリアスは通常、コマンドの最初の単語に対してのみ展開されます。これは、{{ic|sudo}} コマンドを実行するときにエイリアスが通常展開されないことを意味します。次の単語を展開する方法の一つは、スペースで終わる sudo のエイリアスを作成することです。以下を[[コマンドラインシェル#設定ファイル|シェルの設定ファイル]]に追加します: |
|||
root ALL = (ALL) ALL |
|||
USER_NAME ALL = (ALL) ALL, NOPASSWD: WHEELER, NOPASSWD: PROCESSES, NOPASSWD: ARCHLINUX, NOPASSWD: EDITS |
|||
Defaults !requiretty, !tty_tickets, !umask |
|||
Defaults visiblepw, path_info, insults, lecture=always |
|||
Defaults loglinelen = 0, logfile =/var/log/sudo.log, log_year, log_host, syslog=auth |
|||
Defaults mailto=webmaster@foobar.com, mail_badpass, mail_no_user, mail_no_perms |
|||
Defaults passwd_tries = 8, passwd_timeout = 1 |
|||
Defaults env_reset, always_set_home, set_home, set_logname |
|||
Defaults !env_editor, editor="/usr/bin/vim:/usr/bin/vi:/usr/bin/nano" |
|||
Defaults timestamp_timeout=360 |
|||
Defaults passprompt="Sudo invoked by [%u] on [%H] - Cmd run as %U - Password for user %p:" |
|||
</nowiki>}} |
|||
alias sudo='sudo ' |
|||
=== Bash のタブ補完を有効にする === |
|||
{{man|1|zshmisc|ALIASING}} はこれがどのように機能するかを説明しています: |
|||
ユーザーが sudoers ファイルに追加された最初のうちは、デフォルトで、{{ic|Tab}} 補完は動作しないようになっています。例えば、通常 John が入力する必要があるのはこれだけです: |
|||
: 置換テキストがスペースで終わる場合、シェル入力の次の単語は常にエイリアス展開の対象になります。 |
|||
{{man|1|bash|ALIASES}} も同様に説明しています: |
|||
$ fire<{{ic|Tab}}> |
|||
: エイリアス値の最後の文字が空白の場合、エイリアスの次のコマンド単語もエイリアス展開の対象としてチェックされます。 |
|||
=== パスワードプロンプトに端末ベルを鳴らす === |
|||
シェルが彼のためにコマンドを補完してくれるでしょう: |
|||
バックグラウンドの端末で sudo プロンプトに注意を引く為に、[[Wikipedia:Bell character|bell character]] を鳴らす方法もあります。 |
|||
$ firefox |
|||
Defaults passprompt="^G[sudo] password for %p: " |
|||
しかしながら、John が sudoers ファイルに追加されると、同じように入力しても: |
|||
{{ic|^G}} は文字通りベル文字です。 例えば。 [[vim]] で、シーケンスを使用して挿入 {{ic|Ctrl+v}} {{ic|Ctrl+g}} または [[Nano]] {{ic|alt+v}} {{ic|Ctrl+g}} |
|||
$ sudo fire<{{ic|Tab}}> |
|||
もう一つのオプションとして、{{ic|SUDO_PROMPT}} [[環境変数]] を設定する方法があります。例えば、シェルの設定ファイルに以下を追加します: |
|||
シェルは何もしません。 |
|||
export SUDO_PROMPT=$'\a[sudo] please enter a password: ' |
|||
sudo で {{ic|Tab}} 補完を有効にするには、[[公式リポジトリ]]から {{pkg|bash-completion}} パッケージを[[pacman|インストール]]して下さい。詳しくは [[bash#Tab_completion]] を見て下さい。 |
|||
=== ターミナルごとの sudo を無効化 === |
|||
もしくは、以下を {{ic|~/.bashrc}} に追加して下さい: |
|||
{{Warning|以下を行うとすべてのプロセスがあなたの sudo セッションを使うようになります。}} |
|||
complete -cf sudo |
|||
新しいターミナルを開くたびにパスワードを入力するよう求められる sudo のデフォルト設定が煩わしい場合は、{{ic|timestamp_type}} を {{ic|global}} に設定します: |
|||
=== sudo を使って X11 アプリを実行 === |
|||
Defaults timestamp_type=global |
|||
sudo が X11 のグラフィカルアプリケーションを実行できるようにするには visudo に以下を追加する必要があります: |
|||
=== パスワードの入力回数を減らす === |
|||
Defaults env_keep += "HOME" |
|||
5分ごとにパスワードを再入力するのが面倒な場合 (デフォルト)、 {{ic|timestamp_timeout}} に長い値を設定することで変更することができます。(単位:分)。 |
|||
=== ターミナルごとの sudo を無効化 === |
|||
Defaults timestamp_timeout=10 |
|||
{{Warning|以下を行うとすべてのプロセスがあなたの sudo セッションを使うようになります。}} |
|||
多くの sudo コマンドを連続して使用する場合、{{ic|timestamp_timeout}} を増やすよりも、sudo を使用するたびにタイムアウトを更新する方が論理的です。タイムアウトを更新するには、{{ic|sudo -v}} を使用します({{ic|sudo -K}} は即座に取り消します)。 |
|||
次の内容を[[環境変数#シェルの初期化ファイルを使う|シェル初期化ファイル]]に追加することで、自動化することができます: |
|||
alias sudo='sudo -v; sudo ' |
|||
sudo のデフォルト設定では、新しいターミナルを開くたびにパスワードを入力する必要がありますが、これを鬱陶しく感じるなら '''tty_tickets''' を無効化して下さい: |
|||
また、bash 関数を使用することも可能です。詳細は [https://unix.stackexchange.com/questions/584725/expand-certain-aliases-from-within-sudo-bash-function stackexchange] を参照してください。 |
|||
Defaults !tty_tickets |
|||
=== 環境変数 === |
=== 環境変数 === |
||
| 181行目: | 198行目: | ||
</nowiki>}} |
</nowiki>}} |
||
=== |
=== Root パスワード === |
||
ユーザーは、{{ic|/etc/sudoers}} の Defaults 行に {{ic|targetpw}}(ターゲットユーザー、デフォルトは root)または {{ic|rootpw}} を追加することで、ユーザーパスワードの代わりに root パスワードを尋ねるように sudo を設定できます: |
|||
管理コマンド ({{ic|/sbin}} や {{ic|/usr/sbin}} 内のコマンド) をフルパスを使わずに sudo で実行したいなら、以下を {{ic|/etc/sudoers}} に追加して下さい: |
|||
Defaults targetpw |
|||
Defaults secure_path="/bin:/sbin:/usr/bin:/usr/sbin" |
|||
root パスワードをユーザーに公開しないようにするためには、これを特定のグループに制限できます: |
|||
これで、 |
|||
Defaults:%wheel targetpw |
|||
$ sudo /usr/sbin/command |
|||
%wheel ALL=(ALL) ALL |
|||
や |
|||
$ sudo /sbin/command |
|||
などのコマンドを次で実行できるようになります: |
|||
$ sudo command |
|||
=== エイリアスを渡す === |
|||
使っているエイリアスが多すぎて、sudo を使っている時にそれらを root アカウントに持ち越せないことに気づいたかもしれませんね。しかしながら、エイリアスを全て動作させる簡単な方法があります。以下を {{ic|~/.bashrc}} か {{ic|/etc/bash.bashrc}} に追加するだけです: |
|||
alias sudo='sudo ' |
|||
=== Insults === |
|||
間違ったパスワードが入力されたときに、デフォルトの "wrong password" メッセージの代わりに侮辱的なメッセージを表示するように sudo を設定できます。{{ic|/etc/sudoers}} 内の Defaults 行を探して既存のオプションの後に "insults" を加えて下さい。以下のようになります: |
|||
#Defaults specification |
|||
Defaults insults |
|||
テストするために、{{ic|sudo -K}} を入力して現在のセッションを終わらせもう一度 sudo にパスワードを尋ねさせてください。 |
|||
=== root パスワード === |
|||
{{ic|/etc/sudoers}} の Defaults 行に "rootpw" を加えると sudo はユーザーのパスワードではなく root パスワードを尋ねるようになります: |
|||
Defaults timestamp_timeout=0,rootpw |
|||
=== root ログインを無効化 === |
=== root ログインを無効化 === |
||
sudo をインストール・設定したら、root ログインを無効化できます。root を無効化すれば、攻撃者はユーザーのパスワードだけでなく sudoer として設定されたユーザー名もまず考えなくてはならなくなります。[[Secure Shell#root ログインを制限する]]も参照。 |
|||
{{Warning|Arch Linux は root アカウントが無効化された状態でもうまく動くようには作られていません。このメソッドを使うと問題が起こるかもしれません。}} |
|||
sudo をインストール・設定したら、root ログインを無効化したくなりましたか。root がないと、攻撃者はユーザーのパスワードだけでなく sudoer として設定されたユーザー名もまず考えなくてはならなくなります。 |
|||
{{Warning| |
|||
{{Warning|root アカウントを''無効にする前に''ユーザーが sudoer として正しく設定されているか確認して下さい!}} |
|||
* ルートログインを無効にすることで、自分自身をロックアウトしてしまう可能性がありますのでご注意ください。Sudo は自動的にインストールされませんし、そのデフォルトの設定では、パスワードなしの root アクセスも、自分のパスワードによる root アクセスもできません。root アカウントを無効にする前に、ユーザが sudoer として適切に設定されている ことを確認してください! |
|||
* sudoers ファイルをデフォルトで rootpw を使用するように変更した場合、以下のコマンドで root ログインを無効化しないでください! |
|||
* すでにロックアウトされている場合は、[[パスワードリカバリ]] を参照してください。 |
|||
}} |
|||
アカウントをロックするには {{ic|passwd}} を使います: |
アカウントをロックするには {{ic|passwd}} を使います: |
||
| 243行目: | 234行目: | ||
$ sudo passwd root |
$ sudo passwd root |
||
システム緊急時には、リカバリプロンプトが root パスワードを要求するため、リカバリシェルにログインすることができません。緊急時に自動的に root アカウントをアンロックするには、{{ic|rescue.service}} に [[ドロップインファイル]] を使用して {{ic|1=SYSTEMD_SULOGIN_FORCE=1}} 環境変数を追加します: |
|||
{{hc|/etc/systemd/system/rescue.service.d/SYSTEMD_SULOGIN_FORCE.conf|2= |
|||
[Service] |
|||
Environment=SYSTEMD_SULOGIN_FORCE=1 |
|||
}} |
|||
{{Tip|''root'' アカウントを無効化した後もインタラクティブな root プロンプトを表示するには、{{ic|sudo -i}} を使ってください。}} |
|||
==== kdesu ==== |
==== kdesu ==== |
||
KDE では root 権限で GUI アプリケーションを起動するのに kdesu が使われることがあります。root アカウントが無効になっているときでもデフォルトで kdesu は su を使おうとします。幸いに、kdesu では su の代わりに sudo を使うようにすることができます。{{ic|~/.kde4/share/config/kdesurc}} ファイルを作成・編集してください: |
KDE では root 権限で GUI アプリケーションを起動するのに kdesu が使われることがあります。root アカウントが無効になっているときでもデフォルトで kdesu は su を使おうとします。幸いに、kdesu では su の代わりに sudo を使うようにすることができます。{{ic|~/.config/kdesurc}} (kde4 の場合は {{ic|~/.kde4/share/config/kdesurc}}) ファイルを作成・編集してください: |
||
[super-user-command] |
[super-user-command] |
||
super-user-command=sudo |
super-user-command=sudo |
||
もしくは以下のコマンドを使用 (kde4 の場合は ''kwriteconfig'' を使用): |
|||
==== PolicyKit ==== |
|||
$ kwriteconfig5 --file kdesurc --group super-user-command --key super-user-command sudo |
|||
root アカウントを無効にするときは、[[PolicyKit]] の設定を変更する必要があります。デフォルトでは root パスワードを尋ねるので、それを変えなくてはなりません。polkit-1 では、{{ic|/etc/polkit-1/localauthority.conf.d/50-localauthority.conf}} を編集して {{ic|1=AdminIdentities=unix-user:0}} を他のものに置き換えてください。ユーザーやグループのリストにすることができます、例えば: |
|||
=== sudoでハード化する例 === |
|||
AdminIdentities=unix-group:wheel |
|||
例えば、admin, devel, joe の3人のユーザーを作ったとします。''admin'' は journalctl, systemctl, mount, kill, iptables に、''devel'' はパッケージのインストールや設定ファイルの編集に、そして ''joe'' はログインするユーザにそれぞれ使用します。''joe" にリブート、シャットダウン、netctl を使わせるためには、以下のようにします。 |
|||
もしくは |
|||
{{ic|/etc/pam.d/su}} と {{ic|/etc/pam.d/su-l}} を編集してください。 |
|||
AdminIdentities=unix-user:me;unixuser:mom;unix-group:wheel |
|||
ユーザが wheel グループに属していることを要求するが、誰もその中に入れないようにする。 |
|||
#%PAM-1.0 |
|||
auth sufficient pam_rootok.so |
|||
# Uncomment the following line to implicitly trust users in the "wheel" group. |
|||
#auth sufficient pam_wheel.so trust use_uid |
|||
# Uncomment the following line to require a user to be in the "wheel" group. |
|||
auth required pam_wheel.so use_uid |
|||
auth required pam_unix.so |
|||
account required pam_unix.so |
|||
session required pam_unix.so |
|||
SSH ログインを 'ssh' グループに制限します。''joe'' だけがこのグループの一員になります。 |
|||
詳しくは {{ic|man pklocalauthority}} を見て下さい。 |
|||
groupadd -r ssh |
|||
gpasswd -a joe ssh |
|||
echo 'AllowGroups ssh' >> /etc/ssh/sshd_config |
|||
[[リスタート]] {{ic|sshd.service}}. |
|||
==== NetworkManager ==== |
|||
ユーザーを他のグループに追加する。 |
|||
上記の PolicyKit 設定に加えて NetworkManager のポリシーを設定する必要があります。[[NetworkManager#ノーマルユーザーで接続を編集できない|NetworkManager]] のページでまとめられています。 |
|||
for g in power network ;do ;gpasswd -a joe $g ;done |
|||
for g in network power storage ;do ;gpasswd -a admin $g ;done |
|||
デベロッパーが編集できるように、コンフィグにパーミッションを設定します。 |
|||
== トラブルシューティング == |
|||
chown -R devel:root /etc/{http,openvpn,cups,zsh,vim,screenrc} |
|||
Cmnd_Alias POWER = /usr/bin/shutdown -h now, /usr/bin/halt, /usr/bin/poweroff, /usr/bin/reboot |
|||
=== SSH TTY 問題 === |
|||
Cmnd_Alias STORAGE = /usr/bin/mount -o nosuid\,nodev\,noexec, /usr/bin/umount |
|||
Cmnd_Alias SYSTEMD = /usr/bin/journalctl, /usr/bin/systemctl |
|||
Cmnd_Alias KILL = /usr/bin/kill, /usr/bin/killall |
|||
Cmnd_Alias PKGMAN = /usr/bin/pacman |
|||
Cmnd_Alias NETWORK = /usr/bin/netctl |
|||
Cmnd_Alias FIREWALL = /usr/bin/iptables, /usr/bin/ip6tables |
|||
Cmnd_Alias SHELL = /usr/bin/zsh, /usr/bin/bash |
|||
%power ALL = (root) NOPASSWD: POWER |
|||
%network ALL = (root) NETWORK |
|||
%storage ALL = (root) STORAGE |
|||
root ALL = (ALL) ALL |
|||
admin ALL = (root) SYSTEMD, KILL, FIREWALL |
|||
devel ALL = (root) PKGMAN |
|||
joe ALL = (devel) SHELL, (admin) SHELL |
|||
この設定により、root ユーザーでのログインはほとんど必要なくなります。 |
|||
SSH does not allocate a tty by default when running a remote command. Without a tty, sudo cannot disable echo when prompting for a password. You can use ssh's {{ic|-tt}} option to force it to allocate a tty. (use {{ic|-tt}} twice). |
|||
''joe'' は自宅の WiFi に接続できます。 |
|||
The {{ic|Defaults}} option {{ic|requiretty}} only allows the user to run sudo if they have a tty. |
|||
sudo netctl start home |
|||
sudo poweroff |
|||
''joe'' は、他のユーザーとして netctl を使用できません。 |
|||
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear text. You have to run "ssh -t hostname sudo <cmd>". |
|||
sudo -u admin -- netctl start home |
|||
# |
|||
#Defaults requiretty |
|||
''joe'' が journalctl を使ったり、逃亡したプロセスを kill したりする必要があるとき、そのユーザーに切り替えることができます。 |
|||
=== ユーザー特権を表示 === |
|||
sudo -i -u devel |
|||
sudo -i -u admin |
|||
しかし、''joe'' は root ユーザーに切り替えることはできません。 |
|||
You can find out what privileges a particular user has with the following command: |
|||
sudo -i -u root |
|||
''joe'' が管理者として gnu-screen のセッションを開始したい場合は、次のようにします。 |
|||
$ sudo -lU yourusename |
|||
sudo -i -u admin |
|||
admin% chown admin:tty `echo $TTY` |
|||
admin% screen |
|||
=== /etc/sudoers.d のドロップインファイルを使って sudo を設定 === |
|||
Or view your own with: |
|||
''sudo'' は {{ic|/etc/sudoers.d/}} ディレクトリ内のファイルを読み込みます。{{ic|/etc/sudoers}} を編集するかわりに、{{ic|/etc/sudoers.d/}} ディレクトリのファイルで設定を変更することができます。これには2つのメリットがあります: |
|||
{{hc|$ sudo -l|<nowiki> |
|||
* {{ic|sudoers.pacnew}} ファイルを編集する必要がありません。 |
|||
Matching Defaults entries for yourusename on this host: |
|||
* 新しいエントリに問題があった場合、{{ic|/etc/sudoers}} を編集するのではなく問題のあるファイルを削除するだけですみます。 |
|||
loglinelen=0, logfile=/var/log/sudo.log, log_year, syslog=auth, mailto=webmaster@gmail.com, mail_badpass, mail_no_user, |
|||
mail_no_perms, env_reset, always_set_home, tty_tickets, lecture=always, pwfeedback, rootpw, set_home |
|||
ドロップインファイルのエントリの形式は {{ic|/etc/sudoers}} 自体と同じです。直接編集するときは、{{ic|visudo -f /etc/sudoers.d/''somefile''}} を使ってください。詳しくは {{man|5|sudoers}} の "Including other files from within sudoers" セクションを参照。 |
|||
User yourusename may run the following commands on this host: |
|||
{{ic|/etc/sudoers.d/}} ディレクトリのファイルは辞書順に読み込まれます。ファイル名に {{ic|.}} または {{ic|~}} が含まれているとスキップされます。ソート順で問題が起こらないように、ファイル名には2桁の数字を付けてください。例: {{ic|01_foo}}。 |
|||
(ALL) ALL |
|||
(ALL) NOPASSWD: /usr/bin/lsof, /bin/nice, /usr/bin/ss, /usr/bin/su, /usr/bin/locate, /usr/bin/find, /usr/bin/rsync, /usr/bin/strace, |
|||
(ALL) /bin/nice, /bin/kill, /usr/bin/nice, /usr/bin/ionice, /usr/bin/top, /usr/bin/kill, /usr/bin/killall, /usr/bin/ps, /usr/bin/pkill |
|||
(ALL) /usr/bin/gparted, /usr/bin/pacman |
|||
(ALL) /usr/local/bin/synergyc, /usr/local/bin/synergys |
|||
(ALL) /usr/bin/vim, /usr/bin/nano, /usr/bin/cat |
|||
(root) NOPASSWD: /usr/local/bin/synergyc</nowiki>}} |
|||
{{Note|ドロップインファイルのエントリの順番は重要です: 間違って上書きしてしまわないように注意してください。}} |
|||
=== Permissive Umask === |
|||
{{Warning|{{ic|/etc/sudoers.d/}} のファイルは {{ic|/etc/sudoers}} と同じように注意して扱う必要があります: フォーマットを間違えてしまうと {{ic|sudo}} が機能不全に陥ります。{{ic|visudo}} を使うことを強く推奨します。}} |
|||
Sudo will union the user's [[umask]] value with its own umask (which defaults to 0022). This prevents sudo from creating files with more open permissions than the user's umask allows. While this is a sane default if no custom umask is in use, this can lead to situations where a utility run by sudo may create files with different permissions than if run by root directly. If errors arise from this, sudo provides a means to fix the umask, even if the desired umask is more permissive than the umask that the user has specified. Adding this (using {{ic|visudo}}) will override sudo's default behavior: |
|||
=== ファイルの編集 === |
|||
Defaults umask = 0022 |
|||
Defaults umask_override |
|||
{{ic|sudo -e}} または {{ic|sudoedit}} を使うことで、ファイルを他のユーザーとして編集することができます。テキストエディタを動かすユーザーは変わりません。 |
|||
This sets sudo's umask to root's default umask (0022) and overrides the default behavior, always using the indicated umask regardless of what umask the user as set. |
|||
テキストエディタの権限を昇格させることなく root でファイルを編集することが可能です。詳しくは {{man|8|sudo|e}} を読んでください。 |
|||
=== デフォルトスケルトン === |
|||
エディタは他のプログラムに設定できます。例えば {{Pkg|meld}} を使って [[Pacnew と Pacsave ファイル|pacnew]] ファイルを管理する場合: |
|||
The authors site has a [http://www.gratisoft.us/sudo/sudoers.man.html#sudoers_options list of all the options] that can be used with the {{ic|Defaults}} command in the {{ic|/etc/sudoers}} file. |
|||
$ SUDO_EDITOR=meld sudo -e /etc/''file''{,.pacnew''} |
|||
The full list of options ''(parsed from the version 1.8.7 source code)'' is below in a format optimized for copying and pasting into your sudoers files for quick editing. |
|||
=== 侮辱を有効にする === |
|||
{{bc|<nowiki>#Defaults always_set_home |
|||
# If enabled, sudo will set the HOME environment variable to the home directory of the target user |
|||
# (which is root unless the -u option is used). This effectively means that the -H option |
|||
# always_set_home is only effective for configurations where either env_reset is disabled or HOME is present |
|||
# in the env_keep list. Default: OFF |
|||
visudo を使用して sudoers ファイルに次の行を追加することにより、sudo でイースターエッグ (侮辱) することができます。 |
|||
#Defaults authenticate |
|||
# If set, users must authenticate themselves via a password (or other means of authentication) |
|||
# before they may run commands. This default may be overridden via the PASSWD and NOPASSWD |
|||
不正なパスワードを入力すると、{{ic|Sorry, try again.}} のメッセージがユーモラスな侮辱に置き換えられます。 |
|||
#Defaults closefrom_override |
|||
# If set, the user may use sudo's -C option which overrides the default starting |
|||
# point at which sudo begins closing open file descriptors. Default: OFF |
|||
{{hc|1=/etc/sudoers|2= |
|||
#Defaults compress_io |
|||
Defaults insults |
|||
# If set, and sudo is configured to log a command's input or output, the I/O logs will be |
|||
}} |
|||
# compressed using zlib. This flag is on by default when sudo is compiled with zlib support. |
|||
=== パスワード入力のフィードバックを有効にする === |
|||
#Defaults env_editor |
|||
# If set, visudo will use the value of the EDITOR or VISUAL environment variables before falling back on the default |
|||
# editor list. Note that this may create a security hole as it allows th separated list of editors in the editor |
|||
# variable. visudo will then only use the EDITOR or VISUAL if they match a value specified in editor. On by default. |
|||
デフォルトでは、パスワードを入力する際に視覚的なフィードバックはありません。これは、セキュリティを高めるために意図的に行われています。しかし、視覚的な入力が必要な場合は、この行を追加することで有効にすることができます: |
|||
#Defaults env_reset |
|||
# If set, sudo will run the command in a minimal environment containing the TERM, PATH, HOME, MAIL, SHELL, LOGNAME, |
|||
# USER, USERNAME and SUDO_* variables. Any variables in the caller's env in the file specified by the env_file |
|||
# option (if any). The default contents of the env_keep and env_check lists are displayed when sudo is run by |
|||
# root with the -V option. |
|||
{{hc|/etc/sudoers|2= |
|||
#Defaults fast_glob |
|||
Defaults pwfeedback |
|||
# Normally, sudo uses the glob(3) function to do shell-style globbing when matching path names. |
|||
}} |
|||
# However, since it accesses the file system, glob(3) can take a long time to complete for som (automounted). |
|||
# The fast_glob option causes sudo to use the fnmatch(3) function, which does not access the file system to do |
|||
# its matching. The disadvantage of fast_glob is that it is unable to ma names that include globbing characters |
|||
# are used with the negation operator, '!', as such rules can be trivially bypassed. As such, this option should |
|||
# not be used when sudoers contains rules that |
|||
=== カラーパスワードプロンプト === |
|||
#Defaults fqdn |
|||
# Set this flag if you want to put fully qualified host names in the sudoers file. I.e., instead of myhost you |
|||
# would use myhost.mydomain.edu. You may still use the short form if you wish (and sudo unusable if DNS stops |
|||
# working (for example if the machine is not plugged into the network). Also note that you must use the |
|||
# host's official name as DNS knows it. That is, you may not use a all aliases from DNS. If your machine's |
|||
# host name (as returned by the hostname command) is already fully qualified you should not need to set fqdn. |
|||
# Default: OFF |
|||
パスワードプロンプトをカラーや太字フォントでカスタマイズするには、シェル初期化ファイルに {{ic|SUDO_PROMPT}} [[環境変数]] を設定し、{{man|1|tput}} を使用します。 |
|||
#Defaults ignore_dot |
|||
# If set, sudo will ignore '.' or '' (current dir) in the PATH environment variable; the PATH |
|||
# itself is not modified. Default: OFF |
|||
例えば、パスワードプロンプトを太字の赤で表示するには、以下のように設定します: |
|||
#Defaults ignore_local_sudoers |
|||
# If set via LDAP, parsing of /etc/sudoers will be skipped. This is intended for Enterprises that wish to prevent |
|||
# the usage of local sudoers files so that only LDAP is used. /etc/sudoers does not even need to exist. |
|||
# Since this option tells sudo how to behave when no specific LDAP entries have been matched, this sudo |
|||
# Option is only meaningful for the cn=default |
|||
export SUDO_PROMPT="$(tput setaf 1 bold)Password:$(tput sgr0) " |
|||
#Defaults insults |
|||
# If set, sudo will insult users when they enter an incorrect password. Default: OFF |
|||
また、デフォルトメッセージに異なる色を使用するには、以下のようにします: |
|||
#Defaults log_host |
|||
# If set, the host name will be logged in the (non-syslog) sudo log file. Default: OFF |
|||
export SUDO_PROMPT="$(tput setab 1 setaf 7 bold)[sudo]$(tput sgr0) $(tput setaf 6)password for$(tput sgr0) $(tput setaf 5)%p$(tput sgr0): " |
|||
#Defaults log_input |
|||
# If set, sudo will run the command in a pseudo tty and log all user input. If the standard |
|||
# input is not connected to the user's tty, due to I/O redirection or because the command is part Input is logged |
|||
# to the directory specified by the iolog_dir option (/var/log/sudo-io by default) using a unique session ID that |
|||
# is included in the normal sudo log line, prefixed with TSID=. Note that user input may contain sensitive |
|||
# information such as passwords (even if they are not echoed to the screen), which will be stored in the log file |
|||
# unencrypted. |
|||
{| |
|||
#Defaults log_output |
|||
|- |
|||
# If set, sudo will run the command in a pseudo tty and log all output that is sent to the |
|||
| |
|||
# screen, similar to the script(1) command. If the standard output or standard error is not connec is also captured and |
|||
# stored in separate log files. Output is logged to the directory specified by the iolog_dir option (/var/log/sudo-io |
|||
# by default) using a unique session ID that is included in the normal sudo log line, prefixed with TSID=. The Output |
|||
# logs may be viewed with the sudoreplay(8) utility, which can also be used to list or search the available logs. |
|||
{| class="wikitable" |
|||
#Defaults log_year |
|||
|+ tput コマンドラインオプション チートシート |
|||
# If set, the four-digit year will be logged in the (non-syslog) sudo log file. Default: OFF |
|||
|- |
|||
! tput コマンド !! 説明 |
|||
|- |
|||
| {{ic|tput bold}} || 太字テキスト |
|||
|- |
|||
| {{ic|tput setaf [CODE]}} || 前景色を設定 |
|||
|- |
|||
| {{ic|tput setab [CODE]}} || 背景色を設定 |
|||
|- |
|||
| {{ic|tput rev}} || 色を反転 |
|||
|- |
|||
| {{ic|tput sgr0}} || すべてをリセット |
|||
|} |
|||
<br> |
|||
<br> |
|||
<br> |
|||
|| || |
|||
#Defaults long_otp_prompt |
|||
# When validating with a One Time Password (OTP) scheme such as S/Key or OPIE, a two-line |
|||
# prompt is used to make it easier to cut and paste the challenge to a local window |
|||
{| class="wikitable sortable" |
|||
#Defaults mail_always |
|||
|+ tput カラーコード |
|||
# Send mail to the mailto user every time a users runs sudo. Default: OFF |
|||
|- |
|||
! tput カラー [CODE] !! 色 |
|||
|- |
|||
| 0 || 黒 |
|||
|- |
|||
| 1 || 赤 |
|||
|- |
|||
| 2 || 緑 |
|||
|- |
|||
| 3 || 黄 |
|||
|- |
|||
| 4 || 青 |
|||
|- |
|||
| 5 || マゼンタ |
|||
|- |
|||
| 6 || シアン |
|||
|- |
|||
| 7 || 白 |
|||
|} |
|||
|} |
|||
#Defaults mail_badpass |
|||
# Send mail to the mailto user if the user running sudo does not enter the correct password. |
|||
# Default: OFF |
|||
=== U2F の使用 === |
|||
#Defaults mail_no_host |
|||
# If set, mail will be sent to the mailto user if the invoking user exists in the sudoers |
|||
# file, but is not allowed to run commands on the current host. Default: OFF |
|||
U2F は sudo と一緒に使用するのに最適です。これは、公共の場所での[[Wikipedia:Shoulder surfing (computer security)|ショルダーハック]]のリスクを効果的に排除しながら、シンプルな物理的なタッチでプロンプトを承認する意識的なコントロールを与えてくれます。 |
|||
#Defaults mail_no_perms |
|||
# If set, mail will be sent to the mailto user if the invoking user is allowed to use sudo |
|||
# but the command they are trying is not listed in their sudoers file entry or is explicitly denied |
|||
[[Universal 2nd Factor#パスワードなしの sudo]] を参照してください。 |
|||
#Defaults mail_no_user |
|||
# If set, mail will be sent to the mailto user if the invoking user is not in the sudoers file. |
|||
# Default: ON |
|||
=== 保護されたファイルへの書き込み === |
|||
#Defaults noexec |
|||
# If set, all commands run via sudo will behave as if the NOEXEC tag has been set, unless overridden |
|||
# by a EXEC tag. See the description of NOEXEC and EXEC below as well as the "PREVENTING SHE |
|||
sudo を使用する際、保護されたファイルに書き込みたい場合、[[tee]] を使うことで、このような分離が可能になります: |
|||
#Defaults path_info |
|||
# Normally, sudo will tell the user when a command could not be found in their PATH environment |
|||
# variable. Some sites may wish to disable this as it could be used to gather information on t the executable is |
|||
# simply not in the user's PATH, sudo will tell the user that they are not allowed to run it, which can be confusing. |
|||
# Default: ON |
|||
$ ''input stream'' | sudo tee ''--option'' ''protected_file_1 protected_file_2...'' |
|||
#Defaults passprompt_override |
|||
# The password prompt specified by passprompt will normally only be used if the password prompt provided by |
|||
# systems such as PAM matches the string "Password:" |
|||
これは、単純な {{ic|>}}/{{ic|>>}} が機能しない場合に有効です。 |
|||
#Defaults preserve_groups |
|||
# By default, sudo will initialize the group vector to the list of groups the target |
|||
# user is in. When preserve_groups is set, the user's existing group vector is left unaltered. |
|||
==== Vim の場合 ==== |
|||
#Defaults pwfeedback |
|||
# By default, sudo reads the password like most other Unix programs, by turning off echo |
|||
# until the user hits the return (or enter) key. Some users become confused by this as it appears to the user |
|||
# presses a key. Note that this does have a security impact as an onlooker may be able to determine the length of |
|||
# the password being entered. Default: OFF |
|||
他のユーザーが所有するファイルを編集している際、sudo 付きで [[Vim]] を起動するのを忘れた場合にも、同様の方法が役立ちます。 |
|||
#Defaults requiretty |
|||
そのようなときには、[[Vim]] 内部で以下を実行することでファイルを保存できます: |
|||
# If set, sudo will only run when the user is logged in to a real tty. When this flag is set, |
|||
# sudo can only be run from a login session and not via other means such as cron(8) or cgi-bin |
|||
:w !sudo tee % |
|||
#Defaults root_sudo |
|||
# If set, root is allowed to run sudo too. Disabling this prevents users from "chaining" sudo |
|||
# commands to get a root shell by doing something like "sudo sudo /bin/sh". Note, however, that real additional |
|||
# security; it exists purely for historical reasons. Default: ON |
|||
この操作を簡単に使えるように、以下を {{ic|~/.vimrc}} に追加すると、コマンドモードで {{ic|:w!!}} によるマッピングが使えるようになります: |
|||
#Defaults rootpw |
|||
# If set, sudo will prompt for the root password instead of the password of the invoking user. |
|||
# Default: OFF |
|||
{{hc|~/.vimrc|2= |
|||
#Defaults runaspw |
|||
" Allow saving of files as sudo when I forgot to start vim using sudo |
|||
# If set, sudo will prompt for the password of the user defined by the runas_default option |
|||
cmap w!! w !sudo tee > /dev/null % |
|||
# (defaults to root) instead of the password of the invoking user. Default: OFF |
|||
}} |
|||
{{ic|> /dev/null}} の部分は、標準出力を明示的に破棄するためのもので、他のパイプコマンドに何も渡す必要がないためです。 |
|||
#Defaults set_home |
|||
# If enabled and sudo is invoked with the -s option the HOME environment variable will be set |
|||
# to the home directory of the target user (which is root unless the -u option is used). So set_home is only |
|||
# effective for configs where either env_reset is disabled or HOME is present in the env_keep list. Default: OFF |
|||
この仕組みがどのように動作し、なぜ機能するのかについての詳細な説明は、StackOverflow の記事 |
|||
#Defaults set_logname |
|||
[https://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work How does the vim “write with sudo” trick work?] を参照してください。 |
|||
# Normally, sudo will set the LOGNAME, USER and USERNAME environment variables to the name |
|||
# of the target user (usually root unless the -u option is given). However, since some programs ( may be desirable |
|||
# to change this behavior. This can be done by negating the set_logname option. Note that if the env_reset option |
|||
# has not been disabled, entries in the env_keep list will override |
|||
== トラブルシューティング == |
|||
#Defaults set_utmp |
|||
# When enabled, sudo will create an entry in the utmp (or utmpx) file when a pseudo-tty is |
|||
# allocated. A pseudo-tty is allocated by sudo when the log_input, log_output or use_pty flags are e the tty, time, |
|||
# type and pid fields updated. Default: ON |
|||
=== SSH TTY 問題 === |
|||
#Defaults setenv |
|||
# Allow the user to disable the env_reset option from the command line via the -E option. |
|||
# Additionally, environment variables set via the command line are not subject to the restrictions impo variables |
|||
# in this manner. Default: OFF |
|||
リモートコマンドを実行するとき SSH はデフォルトでは tty を割り当てません。tty がないと、パスワードを要求するときに sudo は echo を無効化することができません。ssh の {{ic|-tt}} オプションを使うことで強制的に tty を割り当てることができます ({{ic|-tt}} を2回使ってください)。 |
|||
#Defaults shell_noargs |
|||
# If set and sudo is invoked with no arguments it acts as if the -s option had been given. |
|||
# That is, it runs a shell as root (the shell is determined by the SHELL environment variable if is off by default. |
|||
{{ic|Defaults}} オプションの {{ic|requiretty}} は tty が存在するときだけ sudo を実行することを許可します。 |
|||
#Defaults stay_setuid |
|||
# Normally, when sudo executes a command the real and effective UIDs are set to the target |
|||
# user (root by default). This option changes that behaviour such that the real UID is left as the systems that |
|||
# disable some potentially dangerous functionality when a program is run setuid. This option is only effective on |
|||
# systems with either the setreuid() or setresuid() function. This flag |
|||
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear text. You have to run "ssh -t hostname sudo <cmd>". |
|||
#Defaults targetpw |
|||
# |
|||
# If set, sudo will prompt for the password of the user specified by the -u option (defaults to root) instead |
|||
#Defaults requiretty |
|||
# of the password of the invoking user. In addition, the timestamp file name will passwd database as an argument |
|||
# to the -u option. Default: OFF |
|||
=== Permissive Umask === |
|||
#Defaults tty_tickets |
|||
# If set, users must authenticate on a per-tty basis. With this flag enabled, sudo will |
|||
# use a file named for the tty the user is logged in on in the user's time stamp directory. |
|||
Sudo はユーザーの [[umask]] の値を sudo の umask (デフォルトは 0022) と結合します。これによってユーザーの umask が許可している以上のパーミッションでファイルを作成できないようにしています。umask をカスタマイズしていない場合はデフォルトで問題ありませんが、sudo によって実行されたユーティリティが作成したファイルのパーミッションが root で直接実行した場合と異なってしまう可能性があります。エラーが発生する場合、たとえユーザーが指定した umask よりもパーミッションを甘くしたいときでも、sudo には umask を修正する手段があります。以下を追加 ({{ic|visudo}} を使用) することで sudo のデフォルトの挙動が上書きされます: |
|||
#Defaults umask_override |
|||
# If set, sudo will set the umask as specified by sudoers without modification. This makes |
|||
# it possible to specify a more permissive umask in sudoers than the user's own umask and match user's umask and what |
|||
# is specified in sudoers. Default: OFF |
|||
Defaults umask = 0022 |
|||
Defaults umask_override |
|||
# If set, sudo will run the command in a pseudo-pty even if no I/O logging is being gone. |
|||
# A malicious program run under sudo could conceivably fork a background process that retains to the u that impossible. |
|||
# Default: OFF |
|||
#Defaults utmp_runas |
|||
# If set, sudo will store the name of the runas user when updating the utmp (or utmpx) file. |
|||
# By default, sudo stores the name of the invoking user. Default: OFF |
|||
#Defaults visiblepw |
|||
# By default, sudo will refuse to run if the user must enter a password but it is not possible to disable echo on the |
|||
# terminal. If the visiblepw flag is set, sudo will prompt for a password even when it would be visible on the screen. |
|||
# This makes it possible to run things like "rsh somehost sudo ls" since rsh(1) does not allocate a tty. Default: OFF |
|||
#Defaults closefrom |
|||
# Before it executes a command, sudo will close all open file descriptors other than standard |
|||
# input, standard output and standard error (ie: file descriptors 0-2). |
|||
#Defaults passwd_tries |
|||
# The number of tries a user gets to enter his/her password before sudo logs the failure |
|||
# and exits. The default is 3. |
|||
#Defaults loglinelen |
|||
# Number of characters per line for the file log. This value is used to decide when to wrap |
|||
# lines for nicer log files. This has no effect on the syslog log file, only the file log. |
|||
#Defaults passwd_timeout |
|||
# Number of minutes before the sudo password prompt times out, or 0 for no timeout. |
|||
# The timeout may include a fractional component if minute granularity is insufficient, for example 2 |
|||
#Defaults timestamp_timeout |
|||
# Number of minutes that can elapse before sudo will ask for a passwd again. The timeout |
|||
# may include a fractional component if minute granularity is insufficient, for example 2.5. timestamp will never expire. |
|||
# This can be used to allow users to create or delete their own timestamps via sudo -v and sudo -k respectively. |
|||
#Defaults umask |
|||
# Umask to use when running the command. Negate this option or set it to 0777 to preserve |
|||
# the user's umask. The actual umask that is used will be the union of the user's umask and the value o running |
|||
# a command. Note on systems that use PAM, the default PAM configuration may specify its own umask which will |
|||
# override the value set in sudoers. |
|||
#Defaults badpass_message |
|||
# Message that is displayed if a user enters an incorrect password. The default is Sorry, |
|||
# try again. unless insults are enabled. |
|||
#Defaults editor |
|||
# A colon (':') separated list of editors allowed to be used with visudo. visudo will choose |
|||
# the editor that matches the user's EDITOR environment variable if possible, or the first editor in |
|||
#Defaults iolog_dir |
|||
# The top-level directory to use when constructing the path name for the input/output log |
|||
# directory. Only used if the log_input or log_output options are enabled or when the LOG_INPUT or L directory. |
|||
# The default is "/var/log/sudo-io". The following percent (%) escape sequences are supported: |
|||
# %{seq} - expanded to base-36 sequence number, such as 0100A5, to form a new directory, e.g. 01/00/A5 |
|||
# %{user} - expanded to the invoking user's login name |
|||
# %{group} - expanded to the name of the invoking user's real group ID |
|||
# %{runas_user} - expanded to the login name of the user the command will be run as (e.g. root) |
|||
# %{runas_group} - expanded to the group name of the user the command will be run as (e.g. wheel) |
|||
# %{hostname} - expanded to the local host name without the domain name |
|||
# %{command} - expanded to the base name of the command being run In addition, any escape sequences supported by |
|||
# strftime() function will be expanded. To include a literal % character, the string %% should be used. |
|||
#Defaults iolog_file |
|||
# The path name, relative to iolog_dir, in which to store input/output logs when the log_input or log_output options are |
|||
# enabled or when the LOG_INPUT or LOG_OUTPUT tags are present for a See the iolog_dir option above for a list of |
|||
# supported percent (%) escape sequences. In addition to the escape sequences, path names that end in six or more Xs will |
|||
# have the Xs replaced with a unique combination of digits and letters, similar to the mktemp() function. |
|||
#Defaults mailsub |
|||
# Subject of the mail sent to the mailto user. The escape %h will expand to the host name of |
|||
# the machine. Default is *** SECURITY information for %h ***. |
|||
#Defaults noexec_file |
|||
# This option is no longer supported. The path to the noexec file should now be set in the /etc/sudo.conf file. |
|||
#Defaults passprompt |
|||
# The default prompt to use when asking for a password; can be overridden via the -p option or the SUDO_PROMPT |
|||
# environment variable. The following percent (%) escape sequences are supported: |
|||
# %H - expanded to the local host name including the domain (only if the host name is fqdn or fqdn option is set) |
|||
# %h - expanded to the local host name without the domain name |
|||
# %p - expanded to the user whose password is being asked for (respects the rootpw, targetpw and runaspw flags) |
|||
# %U - expanded to the login name of the user the command will be run as (defaults to root) |
|||
# %u - expanded to the invoking user's login name |
|||
# %% - two consecutive % characters are collapsed into a single % character |
|||
# The default value is Password:. |
|||
#Defaults runas_default |
|||
# The default user to run commands as if the -u option is not specified on the command line. This defaults to root. |
|||
#Defaults syslog_badpri |
|||
# Syslog priority to use when user authenticates unsuccessfully. Defaults to alert. |
|||
# alert, crit, debug, emerg, err, info, notice, and warning. |
|||
#Defaults syslog_goodpri |
|||
# Syslog priority to use when user authenticates successfully. Defaults to notice. See syslog_badpri for the priorities. |
|||
#Defaults sudoers_locale |
|||
# Locale to use when parsing the sudoers file, logging commands, and sending email. |
|||
# Note that changing the locale may affect how sudoers is interpreted. Defaults to "C". |
|||
#Defaults timestampdir |
|||
# The directory in which sudo stores its timestamp files. The default is /var/db/sudo. |
|||
#Defaults timestampowner |
|||
# The owner of the timestamp directory and the timestamps stored therein. The default is root. |
|||
#Defaults env_file |
|||
# The env_file option specifies the fully qualified path to a file containing variables to |
|||
# be set in the environment of the program being run. Entries in this file should either be of the f quotes. |
|||
# Variables in this file are subject to other sudo environment settings such as env_keep and env_check. |
|||
#Defaults exempt_group |
|||
# Users in this group are exempt from password and PATH requirements. The group name specified should not include |
|||
# a % prefix. This is not set by default. |
|||
#Defaults group_plugin |
|||
# A string containing a sudoers group plugin with optional arguments. This can be used to implement support for the |
|||
# nonunix_group syntax described earlier. The string should consist of configuration arguments the plugin requires. |
|||
# These arguments (if any) will be passed to the plugin's initialization function. If arguments are present, the |
|||
# string must be enclosed in double quote For example, given /etc/sudo-group, a group file in Unix group format, |
|||
# the sample group plugin can be used: Defaults group_plugin="sample_group.so /etc/sudo-group" |
|||
# For more information see sudo_plugin(5). |
|||
#Defaults lecture |
|||
# This option controls when a short lecture will be printed along with the password prompt. The default value is once. |
|||
# It has the following possible values: |
|||
# always - Always lecture the user. |
|||
# never - Never lecture the user. |
|||
# once - Only lecture the user the first time they run sudo. |
|||
# If no value is specified, a value of once is implied. Negating the option results in a value of never being used. |
|||
#Defaults lecture_file |
|||
# Path to a file containing an alternate sudo lecture that will be used in place of the standard lecture if the named |
|||
# file exists. By default, sudo uses a built-in lecture. |
|||
#Defaults listpw |
|||
# This option controls when a password will be required when a user runs sudo with the -l option. |
|||
# It has the following possible values: |
|||
# all - All the user's sudoers entries for the current host must have the NOPASSWD set to avoid entering a pass. |
|||
# always - The user must always enter a password to use the -l option. |
|||
# any - At least one of the user's sudoers entries for the current host must have the NOPASSWD set to avoid pass. |
|||
# never - The user need never enter a password to use the -l option. |
|||
# If no value is specified, a value of any is implied. The default value is any. |
|||
#Defaults logfile |
|||
# Path to the sudo log file (not the syslog log file). Setting a path turns on logging to a file; |
|||
# negating this option turns it off. By default, sudo logs via syslog. |
|||
#Defaults mailerflags |
|||
# Flags to use when invoking mailer. Defaults to -t. |
|||
#Defaults mailerpath |
|||
# Path to mail program used to send warning mail. Defaults to the path to sendmail found at configure time. |
|||
#Defaults mailfrom |
|||
# Address to use for the "from" address when sending warning and error mail. The address should |
|||
# be enclosed in double quotes (") to protect against sudo interpreting the @ sign. |
|||
#Defaults mailto |
|||
# Address to send warning and error mail to. The address should be enclosed in double quotes |
|||
# (") to protect against sudo interpreting the @ sign. Defaults to root. |
|||
#Defaults secure_path |
|||
# Path used for every command run from sudo. If you do not trust the people running sudo to have a sane PATH environment |
|||
# variable you may want to use this. Another use is if you want to option are not affected by secure_path. |
|||
# This option is not set by default. |
|||
#Defaults syslog |
|||
# Syslog facility if syslog is being used for logging (negate to disable syslog logging). Defaults to auth. |
|||
# authpriv (if OS supports it), auth, daemon, user, local0, local1, local2, local3, local4, local5, local6, and local7. |
|||
#Defaults verifypw |
|||
# This option controls when a password will be required when a user runs sudo with the -v option. |
|||
# It has the following possible values: |
|||
# all - All the user's sudoers entries for the current host must have the NOPASSWD flag to avoid pw. |
|||
# always - The user must always enter a password to use the -v option. |
|||
# any - At least one of the user's sudoers entries for the current host must have NOPASSWD to avoid pw. |
|||
# never - The user need never enter a password to use the -v option |
|||
# If no value is specified, a value of all is implied. Negating the option results in a value of never being used. |
|||
# The default value is all. |
|||
#Defaults env_check |
|||
# Environment variables to be removed from the user's environment if the variable's value contains % or / characters. |
|||
# This can be used to guard against printf-style format vulnerabilities value without double-quotes. The list can be |
|||
# replaced, added to, deleted from, or disabled by using the =, +=, -=, and ! operators respectively. Regardless of |
|||
# whether the env_reset option is ena they pass the aforementioned check. The default list of environment variables |
|||
# to check is displayed when sudo is run by root with the -V option. |
|||
sudo の umask が root のデフォルト umask (0022) に設定されデフォルトの挙動が上書きされます。ユーザーによって設定された umask を無視して指定された umask を常時使うようになります。 |
|||
#Defaults env_delete |
|||
# Environment variables to be removed from the user's environment when the env_reset option is not in effect. The |
|||
# argument may be a double-quoted, space-separated list or a single value w +=, -=, and ! operators respectively. |
|||
# The default list of environment variables to remove is displayed when sudo is run by root with the -V option. |
|||
{{TranslationStatus|Sudo|2024-07-31|811791}} |
|||
#Defaults env_keep |
|||
# Environment variables to be preserved in the user's environment when the env_reset option is in effect. This |
|||
# allows fine-grained control over the environment sudo-spawned processes will r quotes. The list can be replaced, |
|||
# added to, deleted from, or disabled by using the =, +=, -=, and ! operators respectively.</nowiki>}} |
|||
2025年5月7日 (水) 17:53時点における最新版
sudo を使うことでシステム管理者は権限を委託できるようになり、root や他のユーザーとして複数の(もしくは全ての)コマンドを実行する能力を特定のユーザー(もしくはユーザーのグループ)に与えることができます。
Sudo は root でコマンドを実行するためのものとして su に代わるものです。su が root シェルを起動してそれから全てのコマンドを root として実行できるようにするのに対し、sudo は一つのコマンドだけに特権を与えます。必要な時だけに root 特権を有効にするので、sudo を使うことにより打ち間違いや呼び出されたコマンドのバグによってシステムを破壊してしまう可能性を減らすことができます。
Sudo は他のユーザーとしてコマンドを実行するために使うこともできます; さらに、sudo は全てのコマンドとセキュリティ検査のアクセス失敗を記録します。
インストール
sudo パッケージをインストールしてください。あるいは、sudo-rsにsudo-rsというメモリ安全なsudoの実装があります。
使用方法
ユーザーはコマンドの前に sudo を付けることで、スーパーユーザー(もしくはその他の)特権がある状態でコマンドを実行できます。
sudo を使うときはコマンドの前に sudo と空白を付けてください:
$ sudo cmd
例えば、pacman を root で使いたいなら:
$ sudo pacman -Syu
詳しくは sudo(8) を見てください。
ログインシェル
すべてのコマンドを他のユーザーとして実行するために単に sudo を前置するだけでは不十分です。特に、リダイレクションやコマンド置換を使用する場合、ログインシェルを使用する必要があります。これは sudo -iu user で簡単にアクセスできます(目的のユーザーが root である場合は -u user を省略できます)。
次の例では、コマンド置換はフルシェルで動作しますが、sudo を前置すると失敗します:
$ sudo wpa_supplicant -B -i interface -c <(wpa_passphrase MYSSID passphrase)
Successfully initialized wpa_supplicant Failed to open config file '/dev/fd/63', error: No such file or directory Failed to read or parse configuration '/dev/fd/63'
設定
デフォルトのスケルトンファイル
sudoers(5) § SUDOERS OPTIONS は /etc/sudoers ファイル内の Defaults コマンドで使用できる全てのオプションをリストアップします。
オプションのリスト (バージョン 1.8.7 のソースコードから解析されたもの) を sudoers 用に最適化した形式で表示するには [1] を参照してください。
パスワードのタイムアウトの設定など、より詳しい情報は sudoers(5) を見て下さい。
現在の設定を表示
sudo -ll を実行して現在の sudo 設定を表示してください。sudo -lU user で指定したユーザーの sudo 設定も確認できます。
visudo を使う
sudo の設定ファイルは /etc/sudoers です。このファイルはどんなときでも visudo コマンドを使って編集するべきです。visudo は sudoers ファイルをロックし、一時ファイルに編集内容を保存して、それを /etc/sudoers にコピーする前にファイルの構文をチェックします。
sudoersに構文エラーの危険性があるということは絶対避けなくてはいけません!どんなエラーでも sudo を使用不可能にしてしまいます。エラーを防ぐためにどんなときでもvisudoを使って編集するようにしてください。- visudo(8) は、ユーザーが選択したエディターのユーザー環境変数を尊重するように visudo を設定すると、その変数を別の値に設定するだけで、visudo 権限を持つユーザーがログを記録せずに root として任意のコマンドを実行できるようになるため、セキュリティ ホールになる可能性があると警告しています。
visudo のデフォルトエディタは vi です。core リポジトリの sudo はデフォルトで --with-env-editor を有効にしてコンパイルされており、VISUAL や EDITOR 変数を使用することができます。VISUAL が設定されている場合 EDITOR は使われません。
現在のシェルセッションの間だけ visudo のエディタとして nano を使いたい場合、visudo を実行する前に EDITOR 変数を設定してください:
# EDITOR=nano visudo
もしくは /etc/sudoers ファイルのコピーを編集して visudo -c -f /copy/of/sudoers でチェックすることもできます。visudo でファイルをロックしたくない場合に有用です。
エディタを永久に変更するには、環境変数#ユーザーごとを参照してください。システム全体で visudo のみのエディタを永久に変更するには、以下を /etc/sudoers に追加します(nano が好みのエディタであると仮定します):
# デフォルトのエディタを nano の制限付きバージョンに設定し、visudo が EDITOR/VISUAL を使用しないようにする。 Defaults editor=/usr/bin/rnano, !env_editor
エントリの例
ユーザー USER_NAME が sudo を使った時に、完全な root 権限を与えたい場合は、次の行を加えて下さい:
USER_NAME ALL=(ALL) ALL
マシンのホストネームが HOST_NAME のときだけ、ユーザー USER_NAME に他のユーザーとしてコマンドを実行する権限を与えるには:
USER_NAME HOST_NAME=(ALL) ALL
wheel グループに追加されている全てのユーザーが sudo を使えるようにするには:
%wheel ALL=(ALL) ALL
wheel グループに sudo アクセスを有効にし、ユーザーを追加することが望ましいです。デフォルトでは、Polkit は wheel グループのメンバーを管理者として扱うためです。ユーザーが wheel のメンバーでない場合、Polkit を使用するソフトウェアはユーザーのパスワードではなく root のパスワードを使用して認証を求めることがあります。ユーザー USER_NAME にパスワードを尋ねるのを無効化するには:
Defaults:USER_NAME !authenticate
ホスト HOST_NAME 上のユーザー USER_NAME のみに対して明示的に定義されたコマンドを有効にするには:
USER_NAME HOST_NAME=/usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot,/usr/bin/pacman -Syu
%wheel 行の後にすべきです。ユーザー USER_NAME が ホスト HOST_NAME を使っているときだけ特定のコマンドに sudo をパスワードなしで使えるようにするには:
USER_NAME HOST_NAME= NOPASSWD: /usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot,/usr/bin/pacman -Syu
説明のついた sudoers のサンプルは /usr/share/doc/sudo/examples/sudoers にあります。詳しくは sudoers(5) を見て下さい。
Sudoers デフォルトファイルパーミッション
sudoers ファイルの所有者とグループはゼロにしてください、つまり所有者が居てはいけません。ファイルパーミッションは 0440 に設定する必要があります。このパーミッションはデフォルトでセットされますが、パーミッションを変える事故を起こしてしまった場合、すぐにパーミッションを元に戻すか sudo を消して下さい。
# chown -c root:root /etc/sudoers # chmod -c 0440 /etc/sudoers
ヒントとテクニック
パスワードプロンプトのタイムアウトを無効にする
よくある悩みは、通常のパーミッションで実行され、必要な時にのみ上昇するバックグラウンドターミナルで実行される長時間実行プロセスです。これにより sudo のパスワード入力が促され、気づかれずにタイムアウトし、その時点でプロセスが終了し、完了した作業は失われるか、せいぜいキャッシュされます。一般的なアドバイスは、パスワードなしの sudo を有効にするか、パスワードを記憶する sudo のタイムアウトを延長することです。これらはいずれもセキュリティに悪影響を及ぼします。 プロンプト タイムアウトを無効にすることもできますが、これは適切なセキュリティ上の目的を果たすものではありません、そこを理解して設定する必要があります。
Defaults passwd_timeout=0
Bash 補完
sudo コマンドで完全な Bash 補完機能を利用できるようにするには、bash-completion パッケージをインストールしてください。 何らかの理由でこのパッケージをインストールできない場合、sudo コマンドの補完機能は限定的になりますが、代替手段として以下の内容を .bashrc に追加することで利用可能です:
complete -cf sudo
エイリアスを渡す
Zsh と Bash では、エイリアスは通常、コマンドの最初の単語に対してのみ展開されます。これは、sudo コマンドを実行するときにエイリアスが通常展開されないことを意味します。次の単語を展開する方法の一つは、スペースで終わる sudo のエイリアスを作成することです。以下をシェルの設定ファイルに追加します:
alias sudo='sudo '
zshmisc(1) § ALIASING はこれがどのように機能するかを説明しています:
- 置換テキストがスペースで終わる場合、シェル入力の次の単語は常にエイリアス展開の対象になります。
bash(1) § ALIASES も同様に説明しています:
- エイリアス値の最後の文字が空白の場合、エイリアスの次のコマンド単語もエイリアス展開の対象としてチェックされます。
パスワードプロンプトに端末ベルを鳴らす
バックグラウンドの端末で sudo プロンプトに注意を引く為に、bell character を鳴らす方法もあります。
Defaults passprompt="^G[sudo] password for %p: "
^G は文字通りベル文字です。 例えば。 vim で、シーケンスを使用して挿入 Ctrl+v Ctrl+g または Nano alt+v Ctrl+g
もう一つのオプションとして、SUDO_PROMPT 環境変数 を設定する方法があります。例えば、シェルの設定ファイルに以下を追加します:
export SUDO_PROMPT=$'\a[sudo] please enter a password: '
ターミナルごとの sudo を無効化
新しいターミナルを開くたびにパスワードを入力するよう求められる sudo のデフォルト設定が煩わしい場合は、timestamp_type を global に設定します:
Defaults timestamp_type=global
パスワードの入力回数を減らす
5分ごとにパスワードを再入力するのが面倒な場合 (デフォルト)、 timestamp_timeout に長い値を設定することで変更することができます。(単位:分)。
Defaults timestamp_timeout=10
多くの sudo コマンドを連続して使用する場合、timestamp_timeout を増やすよりも、sudo を使用するたびにタイムアウトを更新する方が論理的です。タイムアウトを更新するには、sudo -v を使用します(sudo -K は即座に取り消します)。
次の内容をシェル初期化ファイルに追加することで、自動化することができます:
alias sudo='sudo -v; sudo '
また、bash 関数を使用することも可能です。詳細は stackexchange を参照してください。
環境変数
環境変数をたくさん使っている場合や、export http_proxy="..." でプロクシー設定を export している場合、sudo を使うときに、sudo に -E オプションを付けない限りこれらの環境変数が root アカウントに渡されません。
$ sudo -E pacman -Syu
環境変数を維持するために env_keep にそれら環境変数を追加するのを推奨します:
/etc/sudoers
Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"
Root パスワード
ユーザーは、/etc/sudoers の Defaults 行に targetpw(ターゲットユーザー、デフォルトは root)または rootpw を追加することで、ユーザーパスワードの代わりに root パスワードを尋ねるように sudo を設定できます:
Defaults targetpw
root パスワードをユーザーに公開しないようにするためには、これを特定のグループに制限できます:
Defaults:%wheel targetpw %wheel ALL=(ALL) ALL
root ログインを無効化
sudo をインストール・設定したら、root ログインを無効化できます。root を無効化すれば、攻撃者はユーザーのパスワードだけでなく sudoer として設定されたユーザー名もまず考えなくてはならなくなります。Secure Shell#root ログインを制限するも参照。
- ルートログインを無効にすることで、自分自身をロックアウトしてしまう可能性がありますのでご注意ください。Sudo は自動的にインストールされませんし、そのデフォルトの設定では、パスワードなしの root アクセスも、自分のパスワードによる root アクセスもできません。root アカウントを無効にする前に、ユーザが sudoer として適切に設定されている ことを確認してください!
- sudoers ファイルをデフォルトで rootpw を使用するように変更した場合、以下のコマンドで root ログインを無効化しないでください!
- すでにロックアウトされている場合は、パスワードリカバリ を参照してください。
アカウントをロックするには passwd を使います:
# passwd -l root
同じようなコマンドで root をアンロックできます。
$ sudo passwd -u root
もしくは、/etc/shadow を編集して root の暗号化されたパスワードを "!" で置き換えてください:
root:!:12345::::::
もう一度 root ログインを有効にするには:
$ sudo passwd root
システム緊急時には、リカバリプロンプトが root パスワードを要求するため、リカバリシェルにログインすることができません。緊急時に自動的に root アカウントをアンロックするには、rescue.service に ドロップインファイル を使用して SYSTEMD_SULOGIN_FORCE=1 環境変数を追加します:
/etc/systemd/system/rescue.service.d/SYSTEMD_SULOGIN_FORCE.conf
[Service] Environment=SYSTEMD_SULOGIN_FORCE=1
sudo -i を使ってください。kdesu
KDE では root 権限で GUI アプリケーションを起動するのに kdesu が使われることがあります。root アカウントが無効になっているときでもデフォルトで kdesu は su を使おうとします。幸いに、kdesu では su の代わりに sudo を使うようにすることができます。~/.config/kdesurc (kde4 の場合は ~/.kde4/share/config/kdesurc) ファイルを作成・編集してください:
[super-user-command] super-user-command=sudo
もしくは以下のコマンドを使用 (kde4 の場合は kwriteconfig を使用):
$ kwriteconfig5 --file kdesurc --group super-user-command --key super-user-command sudo
sudoでハード化する例
例えば、admin, devel, joe の3人のユーザーを作ったとします。admin は journalctl, systemctl, mount, kill, iptables に、devel はパッケージのインストールや設定ファイルの編集に、そして joe はログインするユーザにそれぞれ使用します。joe" にリブート、シャットダウン、netctl を使わせるためには、以下のようにします。
/etc/pam.d/su と /etc/pam.d/su-l を編集してください。
ユーザが wheel グループに属していることを要求するが、誰もその中に入れないようにする。
#%PAM-1.0 auth sufficient pam_rootok.so # Uncomment the following line to implicitly trust users in the "wheel" group. #auth sufficient pam_wheel.so trust use_uid # Uncomment the following line to require a user to be in the "wheel" group. auth required pam_wheel.so use_uid auth required pam_unix.so account required pam_unix.so session required pam_unix.so
SSH ログインを 'ssh' グループに制限します。joe だけがこのグループの一員になります。
groupadd -r ssh gpasswd -a joe ssh echo 'AllowGroups ssh' >> /etc/ssh/sshd_config
リスタート sshd.service.
ユーザーを他のグループに追加する。
for g in power network ;do ;gpasswd -a joe $g ;done for g in network power storage ;do ;gpasswd -a admin $g ;done
デベロッパーが編集できるように、コンフィグにパーミッションを設定します。
chown -R devel:root /etc/{http,openvpn,cups,zsh,vim,screenrc}
Cmnd_Alias POWER = /usr/bin/shutdown -h now, /usr/bin/halt, /usr/bin/poweroff, /usr/bin/reboot Cmnd_Alias STORAGE = /usr/bin/mount -o nosuid\,nodev\,noexec, /usr/bin/umount Cmnd_Alias SYSTEMD = /usr/bin/journalctl, /usr/bin/systemctl Cmnd_Alias KILL = /usr/bin/kill, /usr/bin/killall Cmnd_Alias PKGMAN = /usr/bin/pacman Cmnd_Alias NETWORK = /usr/bin/netctl Cmnd_Alias FIREWALL = /usr/bin/iptables, /usr/bin/ip6tables Cmnd_Alias SHELL = /usr/bin/zsh, /usr/bin/bash %power ALL = (root) NOPASSWD: POWER %network ALL = (root) NETWORK %storage ALL = (root) STORAGE root ALL = (ALL) ALL admin ALL = (root) SYSTEMD, KILL, FIREWALL devel ALL = (root) PKGMAN joe ALL = (devel) SHELL, (admin) SHELL
この設定により、root ユーザーでのログインはほとんど必要なくなります。
joe は自宅の WiFi に接続できます。
sudo netctl start home sudo poweroff
joe は、他のユーザーとして netctl を使用できません。
sudo -u admin -- netctl start home
joe が journalctl を使ったり、逃亡したプロセスを kill したりする必要があるとき、そのユーザーに切り替えることができます。
sudo -i -u devel sudo -i -u admin
しかし、joe は root ユーザーに切り替えることはできません。
sudo -i -u root
joe が管理者として gnu-screen のセッションを開始したい場合は、次のようにします。
sudo -i -u admin admin% chown admin:tty `echo $TTY` admin% screen
/etc/sudoers.d のドロップインファイルを使って sudo を設定
sudo は /etc/sudoers.d/ ディレクトリ内のファイルを読み込みます。/etc/sudoers を編集するかわりに、/etc/sudoers.d/ ディレクトリのファイルで設定を変更することができます。これには2つのメリットがあります:
sudoers.pacnewファイルを編集する必要がありません。- 新しいエントリに問題があった場合、
/etc/sudoersを編集するのではなく問題のあるファイルを削除するだけですみます。
ドロップインファイルのエントリの形式は /etc/sudoers 自体と同じです。直接編集するときは、visudo -f /etc/sudoers.d/somefile を使ってください。詳しくは sudoers(5) の "Including other files from within sudoers" セクションを参照。
/etc/sudoers.d/ ディレクトリのファイルは辞書順に読み込まれます。ファイル名に . または ~ が含まれているとスキップされます。ソート順で問題が起こらないように、ファイル名には2桁の数字を付けてください。例: 01_foo。
/etc/sudoers.d/ のファイルは /etc/sudoers と同じように注意して扱う必要があります: フォーマットを間違えてしまうと sudo が機能不全に陥ります。visudo を使うことを強く推奨します。ファイルの編集
sudo -e または sudoedit を使うことで、ファイルを他のユーザーとして編集することができます。テキストエディタを動かすユーザーは変わりません。
テキストエディタの権限を昇格させることなく root でファイルを編集することが可能です。詳しくは sudo(8) § e を読んでください。
エディタは他のプログラムに設定できます。例えば meld を使って pacnew ファイルを管理する場合:
$ SUDO_EDITOR=meld sudo -e /etc/file{,.pacnew}
侮辱を有効にする
visudo を使用して sudoers ファイルに次の行を追加することにより、sudo でイースターエッグ (侮辱) することができます。
不正なパスワードを入力すると、Sorry, try again. のメッセージがユーモラスな侮辱に置き換えられます。
/etc/sudoers
Defaults insults
パスワード入力のフィードバックを有効にする
デフォルトでは、パスワードを入力する際に視覚的なフィードバックはありません。これは、セキュリティを高めるために意図的に行われています。しかし、視覚的な入力が必要な場合は、この行を追加することで有効にすることができます:
/etc/sudoers
Defaults pwfeedback
カラーパスワードプロンプト
パスワードプロンプトをカラーや太字フォントでカスタマイズするには、シェル初期化ファイルに SUDO_PROMPT 環境変数 を設定し、tput(1) を使用します。
例えば、パスワードプロンプトを太字の赤で表示するには、以下のように設定します:
export SUDO_PROMPT="$(tput setaf 1 bold)Password:$(tput sgr0) "
また、デフォルトメッセージに異なる色を使用するには、以下のようにします:
export SUDO_PROMPT="$(tput setab 1 setaf 7 bold)[sudo]$(tput sgr0) $(tput setaf 6)password for$(tput sgr0) $(tput setaf 5)%p$(tput sgr0): "
|
|
U2F の使用
U2F は sudo と一緒に使用するのに最適です。これは、公共の場所でのショルダーハックのリスクを効果的に排除しながら、シンプルな物理的なタッチでプロンプトを承認する意識的なコントロールを与えてくれます。
Universal 2nd Factor#パスワードなしの sudo を参照してください。
保護されたファイルへの書き込み
sudo を使用する際、保護されたファイルに書き込みたい場合、tee を使うことで、このような分離が可能になります:
$ input stream | sudo tee --option protected_file_1 protected_file_2...
これは、単純な >/>> が機能しない場合に有効です。
Vim の場合
他のユーザーが所有するファイルを編集している際、sudo 付きで Vim を起動するのを忘れた場合にも、同様の方法が役立ちます。 そのようなときには、Vim 内部で以下を実行することでファイルを保存できます:
:w !sudo tee %
この操作を簡単に使えるように、以下を ~/.vimrc に追加すると、コマンドモードで :w!! によるマッピングが使えるようになります:
~/.vimrc
" Allow saving of files as sudo when I forgot to start vim using sudo cmap w!! w !sudo tee > /dev/null %
> /dev/null の部分は、標準出力を明示的に破棄するためのもので、他のパイプコマンドに何も渡す必要がないためです。
この仕組みがどのように動作し、なぜ機能するのかについての詳細な説明は、StackOverflow の記事 How does the vim “write with sudo” trick work? を参照してください。
トラブルシューティング
SSH TTY 問題
リモートコマンドを実行するとき SSH はデフォルトでは tty を割り当てません。tty がないと、パスワードを要求するときに sudo は echo を無効化することができません。ssh の -tt オプションを使うことで強制的に tty を割り当てることができます (-tt を2回使ってください)。
Defaults オプションの requiretty は tty が存在するときだけ sudo を実行することを許可します。
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear text. You have to run "ssh -t hostname sudo <cmd>". # #Defaults requiretty
Permissive Umask
Sudo はユーザーの umask の値を sudo の umask (デフォルトは 0022) と結合します。これによってユーザーの umask が許可している以上のパーミッションでファイルを作成できないようにしています。umask をカスタマイズしていない場合はデフォルトで問題ありませんが、sudo によって実行されたユーティリティが作成したファイルのパーミッションが root で直接実行した場合と異なってしまう可能性があります。エラーが発生する場合、たとえユーザーが指定した umask よりもパーミッションを甘くしたいときでも、sudo には umask を修正する手段があります。以下を追加 (visudo を使用) することで sudo のデフォルトの挙動が上書きされます:
Defaults umask = 0022 Defaults umask_override
sudo の umask が root のデフォルト umask (0022) に設定されデフォルトの挙動が上書きされます。ユーザーによって設定された umask を無視して指定された umask を常時使うようになります。