Sudo

From ArchWiki
Revision as of 19:58, 6 June 2011 by Svenstaro (Talk | contribs) (Tips and tricks: Added trick for running graphical applications with sudo)

Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary text Template:Article summary end

Sudo (su "do") allows a system administrator to delegate authority to give certain users (or groups of users) the ability to run some (or all) commands as root or another user while providing an audit trail of the commands and their arguments.[1]

Rationale

Sudo is an alternative to su for running commands as root. Unlike su, which launches a root shell that allows all further commands root access, sudo instead grants temporary privilege escalation to a single command. By enabling root privileges only when needed, sudo usage reduces the likelyhood that a typo or a bug in an invoked command will ruin the system. Sudo can also be used to run commands as other users; additionally, sudo logs all commands and failed access attempts for security auditing.

Installation

To install sudo:

# pacman -Syu sudo

Use of sudo will be unavailable to users until configured.

Usage

With sudo, users can prefix commands with Template:Codeline to run them with superuser (or other) privileges. For example:

$ sudo pacman -Syu

See the sudo manual for more information.

Configuration

The configuration file for sudo is Template:Filename. This file should not be edited directly! Instead, run

# EDITOR=nano visudo

Using visudo

The Template:Filename file should always be edited with the Template:Codeline command. Template:Codeline locks the Template:Filename file, saves edits to a temporary file, and checks that file's grammar before copying it to Template:Filename. It is imperative that Template:Filename be free of syntax errors since Template:Codeline will not run otherwise.

The default editor is Template:Codeline, which will be used if you do not preface the command with EDITOR=nano. You can use other editors, for example, Gedit:

# EDITOR=gedit visudo

You can permanently change the setting system-wide to e.g. Template:Codeline by appending

export EDITOR=vim

to your Template:Filename file. This won't take effect for already-running shells.

Or, change it permanently for just Template:Codeline by adding the following line to Template:Filename where vim is your prefered editor:

# Defaults specification
# Reset environment by default
Defaults      env_reset
# Set default EDITOR to vim, and do not allow visudo to use EDITOR/VISUAL.
Defaults      editor=/usr/bin/vim, !env_editor

Note you must still run the command Template:Codeline as root even if using a different editor.

To allow a user to gain full root privileges when he/she precedes a command with "sudo", add the following line:

USER_NAME   ALL=(ALL) ALL

and/or to allow a user sudo access from the local machine only:

USER_NAME   HOSTNAME=(ALL) ALL

and/or to allow members of group wheel sudo access requiring no password:

%wheel      ALL=(ALL) NOPASSWD: ALL

where USER_NAME is the user name of the individual.

A detailed Template:Filename example can be found here. Otherwise, see the sudoers manual for detailed information.

sudoers default file permissions

The owner and group for the sudoers file must both be 0. The file permissions should be set to 0440. These permissions are set by default, but if you accidentally change them, they should be changed back immediately.

# chown -c root:root /etc/sudoers
# chmod -c 0440 /etc/sudoers

Password cache timeout

Users may wish to change the default timeout before the cached password expires. This is accomplished by adding following to Template:Filename (Template:Codeline) for example:

Defaults:USER_NAME timestamp_timeout=20

where the password expires for user USER_NAME if unused for over 20 minutes. Values between 0 and 1 are also allowed.

Tip: To ensure sudo always asks for a password, set the timeout to zero.

Tips and tricks

Enabling tab-completion in bash

Tab-completion, by default, will not work when a user is initially added to the sudoers file. For example, normally john only needs to type:

fire<TAB>

and the shell will complete the command for him as:

firefox

If, however, john is added to the sudoers file and he types:

sudo fire<TAB>

the shell will do nothing.

To enable tab-completion with sudo, add the following to your Template:Filename:

complete -cf sudo

Alternatively, you could also install and enable bash-completion to get smarter tab-completion for commands like sudo, see bash#Auto-completion for more information.

Run X11 apps using sudo

To allow sudo to start graphical application in X11, you need to add

Defaults env_keep += "HOME"

to visudo.

Disable per-terminal sudo

Warning: This will let any process use your sudo session

If you are annoyed by sudo's defaults that require you to enter your password every time you open a new terminal, disable tty_tickets:

Defaults !tty_tickets

Environment variables (Outdated?)

If you have a lot of environment variables, or you export your proxy settings via export http_proxy="...", when using sudo these variables do not get passed to the root account unless you run sudo with the Template:Codeline option.

$ sudo -E pacman -Syu

Because of this you may wish to add an alias in Template:Filename:

alias sudo="sudo -E"

Another way of fixing this would be to add in Template:Filename:

Defaults !env_reset

If you want to just pass *_proxy variables, add the following:

Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"

Add /sbin and /usr/sbin to root's PATH

If you want to run administrative commands (those in /sbin or /usr/sbin) with sudo without using their full path, add:

Defaults secure_path="/bin:/sbin:/usr/bin:/usr/sbin" 

in Template:Filename.

This allows you to do:

$ sudo command

instead of:

$ sudo /sbin/command

or:

$ sudo /usr/sbin/command

Passing aliases

If you use a lot of aliases, you might have noticed that they do not carry over to the root account when using sudo. However, there is an easy way to make them work. Simply add the following to your Template:Filename or Template:Filename:

alias sudo='sudo '

Insults

Users can configure sudo to display clever insults when an incorrect password is entered instead of printing the default "wrong password" message. Find the Defaults line in Template:Filename and append "insults" after a comma to existing options. The final result might look like this:

#Defaults specification
Defaults insults

To test, type Template:Codeline to end the current session a let sudo ask for the password again.

Root password

Users can configure sudo to ask for the root password instead of the user password by adding "rootpw" to the Defaults line in Template:Filename:

Defaults timestamp_timeout=0,rootpw

Disable root login

Warning: Arch Linux is not fine-tuned to run with a disabled root account. Users may encounter problems with this method.

With sudo installed and configured, users may wish to disable the root login. Without root, attackers must first guess a user name configured as a sudoer as well as the user password.

Warning: Ensure a user is properly configured as a sudoer before disabling the root account!

The account can be locked via Template:Codeline:

# passwd -l root

A similar command unlocks root.

$ sudo passwd -u root

Alternatively, edit Template:Filename and replace the root's encrypted password with "!":

root:!:12345::::::

To enable root login again:

$ sudo passwd root

kdesu

kdesu may be used under KDE to launch GUI applications with root privileges. It is possible that by default kdesu will try to use su even if the root account is disabled. Fortunately one can tell kdesu to use sudo instead of su. Create/edit the file Template:Filename:

[super-user-command]
super-user-command=sudo

Policykit

When disabling the root account, it is nessecary to change the policykit configuration for local authorification to reflect that. The default is to ask for root password, so that must be changed. With polkit-1, this can be achieved by editing /etc/polkit-1/localauthority.conf.d/50-localauthority.conf so that

AdminIdentities=unix-user:0

is replaced by something else, depending on the system configuration. It can be a list of users and groups, for example

AdminIdentities=unix-group:wheel

or

AdminIdentities=unix-user:me;unixuser:mom;unix-group:wheel

For more information, see man pklocalauthority

Debugging Sudo

SSH TTY Issues

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 "-tt" option to force it to allocate a tty. (use -tt twice).

The Defaults option requiretty only allows the user to run sudo if they have a tty

# Disable "ssh hostname sudo <cmd>", because it will show the password in clear. You have to run "ssh -t hostname sudo <cmd>".
#
#Defaults    requiretty

Display User Privileges

You can find out what privileges a particular user has with the following command:

 sudo -lU askapache

Or view your own with

 sudo -l
Matching Defaults entries for askapache on this host:
    loglinelen=0, logfile=/var/log/sudo.log, log_year, syslog=auth, mailto=sqpt.webmaster@gmail.com, mail_badpass, mail_no_user, mail_no_perms, env_reset, always_set_home, tty_tickets, lecture=always, pwfeedback, rootpw, set_home

User askapache may run the following commands on this host:
    (ALL) ALL, 
    (ALL) NOPASSWD: /usr/sbin/lsof, /bin/nice, /bin/netstat, /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/sbin/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

Example Sudoers

This is especially helpful for those using terminal multiplexers like screen, tmux, or ratpoison, and those using sudo from scripts/cronjobs.


Cmnd_Alias WHEELER = /usr/sbin/lsof, /bin/nice, /bin/ps, /usr/bin/top, /usr/local/bin/nano, /bin/netstat, /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/sbin/gparted, /usr/bin/pacman, /usr/bin/pacman-color

root ALL = (ALL) ALL
askapache 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@askapache.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:"