Sudo

From Gentoo Wiki
Jump to: navigation, search
This page is a translated version of the page Sudo and the translation is 10% complete.

Outdated translations are marked like this.
Other languages:
English • ‎español • ‎français • ‎italiano • ‎русский • ‎中文(中国大陆)‎ • ‎日本語 • ‎한국어

Warning: Display title "sudo/ja" overrides earlier display title "Sudo".

sudoコマンドは特権の昇格をシンプル・安全に管理します。たとえば一般ユーザーが特定(もしくはすべて)のコマンドをrootや他のユーザーで(パスワードなしも含め)実行できるようにします。

ユーザーに特権の必要な操作を許可したいがrootは渡したくない、という時にはsudoを使うとよいでしょう。sudoは誰が何をすることができるかを制御できます。それではこの素敵なツールについて簡単に紹介します。

This article is meant as a quick introduction. The app-admin/sudo package is a lot more powerful than what is described here. It has special features for editing files as a different user (sudoedit), running from within a script (so it can background, read the password from standard in instead of the keyboard, ...), etc.

Please read the sudo and sudoers manual pages for more information.

インストール

USE フラグ

USE flags for app-admin/sudo Allows users or groups to run commands as other users

gcrypt Use message digest functions from dev-libs/libgcrypt instead of sudo's
ldap Add LDAP support (Lightweight Directory Access Protocol)
libressl Use message digest functions from dev-libs/libressl instead of sudo's
offensive Let sudo print insults when the user types the wrong password
secure-path Replace PATH variable with compile time secure paths
sendmail Allow sudo to send emails with sendmail
skey Enable S/Key (Single use password) authentication support
sssd Add System Security Services Daemon support
system-digest Use message digest functions from dev-libs/libgcrypt, dev-libs/libressl or dev-libs/openssl instead of sudo's internal SHA2 implementation

Emerge

root #emerge --ask app-admin/sudo

設定

Logging activity

One additional advantage of sudo is that it can log any attempt (successful or not) to run an application. This is very useful when tracking who made that one fatal mistake that took 10 hours to fix :)

Granting permissions

The app-admin/sudo package allows the system administrator to grant permission to other users to execute one or more applications they would normally have no right to. Unlike using the setuid bit on these applications sudo gives a more fine-grained control on who can execute a certain command and when.

With sudo a clear list can be made of who can execute a certain application. If the setuid bit is set on an executable, any user would be able to run the application (or any user of a certain group, depending on the permissions used). With sudo the user can (and probably should) be required to provide a password in order to execute the application.

sudoの設定は/etc/sudoersに書かれています。このファイルをnano /etc/sudoersvim /etc/sudoersやその他のエディタで編集してはいけません。代わりにvisudoを使うようにしてください。

基本的な構文

sudoの最も難解な部分が/etc/sudoersの構文です。基本的な構文を示します。

CODE /etc/sudoersの基本的な構文
user  host = commands

この構文では、"host"からログインしている"user"というユーザーは"commands"に上げられたコマンドをrootとして実行してもよい、ということをsudoに示します。"swift"がlocalhostでログインしていたらemergeの実行を許可する、といった現実的な例を考えるとわかりやすいでしょう。

A more real-life example might make this more clear: To allow the user larry to execute emerge when he is logged in on localhost:

CODE /etc/sudoersの現実的な例
swift  localhost = /usr/bin/emerge
注意
ホスト名はhostnameの出力と一致させます。
Note
In the simplest case, commands must be fully qualified paths to executables: hence /usr/bin/emerge not just emerge. Paths can also contain wildcards and may refer to entire directories. See the manpage for details.
警告
他人に対して特権の昇格を許可できるアプリケーションの実行を許可しないでください。例えば、あるユーザーにemergeをrootで実行させるというのは完全なroot権限を与えるのと同義です。なぜならemergeはユーザーの意のままにファイルシステムを変更することができるからです。信用できないsudoユーザーに特権を与えてはいけません。

ユーザー名の代わりにグループ名を使うこともできます。この場合はグループ名の頭に%をつけてください。wheelグループのメンバーにemergeの実行を許可する場合はこのように書きます。

CODE wheelグループのメンバーにemergeの実行を許可する
%wheel  localhost = /usr/bin/emerge

コマンドごとに行を増やす代わりに、一行に複数のコマンドを書くこともできます。例えば先ほどのユーザーにemergeだけでなくebuildemerge-webrsyncの実行も許可する場合はこう書きます。

CODE 複数のコマンドを指定する
swift  localhost = /usr/bin/emerge, /usr/bin/ebuild, /usr/sbin/emerge-webrsync

The precise command line can also be specified (including parameters and arguments) not just the name of the executable. This is useful to restrict the use of a certain tool to a specified set of command options. The sudo tool allows shell-style wildcards (AKA meta or glob characters) to be used in path names as well as command-line arguments in the sudoers file. Note that these are not regular expressions.

Here is an example of sudo from the perspective of a first-time user of the tool who has been granted access to the full power of emerge:

user $sudo emerge -uDN world
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
  
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
  
Password: ## (rootではなくユーザーのパスワードを入力します!)

sudoが要求するパスワードはユーザー自身のパスワードです。この手順を踏むことで、うっかりログインしたまま席を離れてしまった時に不正利用されないようにします。

なおsudo${PATH}を変更しません。sudoで実行するコマンドはあなたの環境から実行されます。このため、/sbinなどに置かれたツールを実行したい時は次のように完全なパスをsudoに渡す必要があります。

Basic syntax with LDAP

The ldap and pam USE flags are needed for the LDAP support.

When using sudo with LDAP, sudo will read configuration from LDAP Server as well. So two files will need to edited.

FILE /etc/ldap.conf.sudoPlease chmod 400 when done
# See ldap.conf(5) and README.LDAP for details
# This file should only be readable by root
 
# supported directives: host, port, ssl, ldap_version
# uri, binddn, bindpw, sudoers_base, sudoers_debug
# tls_{checkpeer,cacertfile,cacertdir,randfile,ciphers,cert,key}
 
host ldap.example.com
port 389
 
base dc=example,dc=com
 
uri ldap://ldap.example.com/
#uri ldapi://%2fvar%2frun%2fopenldap%2fslapd.sock
 
ldap_version 3
#ssl start_tls
 
sudoers_base ou=SUDOers,dc=example,dc=com
#sudoers_debug 2
 
bind_policy soft
FILE /etc/nsswitch.confPlease add the sudoers line
sudoers:     ldap files

The following LDAP entry will need to added for sudo.

Note
It was design so that the sudoers branch are on top of the tree for security reason. This allows a different access right from LDAP to read/write to this branch
CODE LDAP entry for sudo
version: 1
DN: ou=SUDOers,dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
objectClass: domainRelatedObject
associatedDomain: example.com
ou: SUDOers
 
DN: cn=defaults,ou=SUDOers,dc=example,dc=com
objectClass: top
objectClass: sudoRole
cn: defaults
description: Default sudoOption's go here
sudoOption: env_reset
 
DN: cn=root,ou=SUDOers,dc=example,dc=com
objectClass: top
objectClass: sudoRole
cn: root
sudoCommand: ALL
sudoHost: ALL
sudoUser: root
 
DN: cn=%wheel,ou=SUDOers,dc=example,dc=com
objectClass: top
objectClass: sudoRole
cn: %wheel
sudoCommand: ALL
sudoHost: ALL
sudoOption: !authenticate
sudoUser: %wheel
CODE LDAP entry for wheel group
version: 1
DN: cn=wheel,ou=Group,dc=example,dc=com
objectClass: top
objectClass: posixGroup
cn: wheel
description: Wheel Group
gidNumber: 10
memberUid: useradmin1
memberUid: root

The configuration on the sudoer on LDAP are similar to files with some different. Please read more about sudo with LDAP on the link below.[1]

エイリアスを用いる

In larger environments having to enter all users over and over again (or hosts, or commands) can be a daunting task. To ease the administration of /etc/sudoers aliases can defined. The format to declare aliases is quite simple:

CODE Declaring aliases in /etc/sudoers
Host_Alias hostalias = hostname1, hostname2, ...
User_Alias useralias = user1, user2, ...
Cmnd_Alias cmndalias = command1, command2, ...

One alias that always works, for any position, is the ALL alias (to make a good distinction between aliases and non-aliases it is recommended to use capital letters for aliases). The ALL alias is an alias to all possible settings.

A sample use of the ALL alias to allow any user to execute the shutdown command if he is logged on locally is:

CODE Allowing any user to execute shutdown
ALL  localhost = /sbin/shutdown

Another example is to allow the user larry to execute the emerge command as root, regardless of where he is logged in from:

CODE Allowing a user to run an application regardless of his location
larry   ALL = /usr/bin/emerge

More interesting is to define a set of users who can run software administrative applications (such as emerge and ebuild) on the system and a group of administrators who can change the password of any user, except root!

CODE Using aliases for users and commands
User_Alias  SOFTWAREMAINTAINERS = larry, john, danny
User_Alias  PASSWORDMAINTAINERS = larry, sysop
Cmnd_Alias  SOFTWARECOMMANDS    = /usr/bin/emerge, /usr/bin/ebuild
Cmnd_Alias  PASSWORDCOMMANDS    = /usr/bin/passwd [a-zA-Z0-9_-]*, !/usr/bin/passwd root
 
SOFTWAREMAINTAINERS  localhost = SOFTWARECOMMANDS
PASSWORDMAINTAINERS  localhost = PASSWORDCOMMANDS

Non-root execution

It is also possible to have a user run an application as a different, non-root user. This can be very interesting when running applications as a different user (for instance apache for the web server) and want to allow certain users to perform administrative steps as that user (like killing zombie processes).

Inside /etc/sudoers list the user(s) in between ( and ) before the command listing:

CODE Non-root execution syntax
users  hosts = (run-as) commands

For instance, to allow larry to run the kill tool as the apache or gorg user:

CODE Non-root execution example
Cmnd_Alias KILL = /bin/kill, /usr/bin/pkill
 
larry   ALL = (apache, gorg) KILL

With this set, the user can run sudo -u to select the user he wants to run the application as:

user $sudo -u apache pkill apache

An alias can set for the user to run an application as using the Runas_Alias directive. Its use is identical to the other _Alias directives we have seen before.

Passwords and default settings

By default, sudo asks the user to identify himself using his own password. Once a password is entered, sudo remembers it for 5 minutes, allowing the user to focus on his tasks and not repeatedly re-entering his password.

Of course, this behavior can be changed: set the Defaults: directive in /etc/sudoers to change the default behavior for a user.

For instance, to change the default 5 minutes to 0 (never remember):

CODE Changing the timeout value
Defaults:larry  timestamp_timeout=0

A setting of -1 would remember the password indefinitely (until the system reboots).

A different setting would be to require the password of the user that the command should be run as and not the users' personal password. This is accomplished using runaspw. In the following example we also set the number of retries (how many times the user can re-enter a password before sudo fails) to 2 instead of the default 3:

CODE Requiring the root password instead of the user's password
Defaults:john   runaspw, passwd_tries=2

Another interesting feature is to keep the DISPLAY variable set so that graphical tools can be executed:

CODE Keeping the DISPLAY variable alive
Defaults:john env_keep=DISPLAY

Dozens of default settings can changed using the Defaults: directive. Fire up the sudoers manual page and search for Defaults.

To allow a user to run a certain set of commands without providing any password whatsoever, start the commands with NOPASSWD:, like so:

CODE Allowing emerge to be ran as root without asking for a password
larry     localhost = NOPASSWD: /usr/bin/emerge

Bash completion

Users that want bash completion with sudo need to run this once.

user $sudo echo "complete -cf sudo" >> $HOME/.bashrc

Zshell completion

Users that want zsh completion for sudo can set the following in .zprofile and .zshrc respectively

FILE .zprofileAdding zshell completion
if [[ $EUID != 0 ]]; then
    typeset -xT SUDO_PATH sudo_path
    typeset -U sudo_path
    sudo_path=({,/usr/local,/usr}/sbin(N-/))
    alias sudo="sudo env PATH=\"SUDO_PATH:$PATH\""
fi
FILE .zshrcAdding zshell completion
zstyle ':completion:*:sudo:*' environ PATH="$SUDO_PATH:$PATH"

With the above change, all commands in the /sbin, /usr/sbin and /usr/local/sbin locations will be available to the shell for completion when the command is prefaced with 'sudo'.

使い方

Listing privileges

To list the current user's capabilities, run sudo -l :

user $sudo -l
User swift may run the following commands on this host:
    (root)   /usr/libexec/xfsm-shutdown-helper
    (root)   /usr/bin/emerge
    (root)   /usr/bin/passwd [a-zA-Z0-9_-]*
    (root)   !/usr/bin/passwd root
    (apache) /usr/bin/pkill
    (apache) /bin/kill

Any command in /etc/sudoers that does not require a password to be entered, a password will not be required to list the entries either. Otherwise sudo will ask for a password if it isn't remembered.

Prolonging password timeout

By default, if a user has entered their password to authenticate their self to sudo, it is remembered for 5 minutes. If the user wants to prolong this period, he can run sudo -v to reset the time stamp so that it will take another 5 minutes before sudo asks for the password again.

user $sudo -v

The inverse is to kill the time stamp using sudo -k.

References