Difference between revisions of "Sudo"

From ArchWiki
Jump to: navigation, search
m (Password timeout)
m (Password cache timeout)
Line 67: Line 67:
  Defaults:USER_NAME timestamp_timeout=20
  Defaults:USER_NAME timestamp_timeout=20
where the password expires for user USER_NAME if unused for over 20 minutes.
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.}}
{{Tip|To ensure sudo always asks for a password, set the timeout to zero.}}

Revision as of 04:24, 14 September 2010

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 wiki 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]


Sudo is a secure alternative to the traditional su command. Many times the user utilizes su (substitute user) to gain root priviledges. Generally, it is considered unwise to login as root -- the superuser -- for extended periods of time. The root user enjoys complete and absolute control over the entire system, but at great risk! Simple typos can easily render a system unusable, and any applications run as root share this unfettered access.

Rather, sudo grants temporary privilege escalation for a single command (whether as root or another user); returning to the unprivileged state after completion, and rendering the system safe from unintended consequences. Additionally, sudo logs all commands and failed access attempts for security auditing.


To install sudo:

# pacman -S sudo

By default, users will not be allowed to run sudo. See #Configuration for instructions.


With sudo installed and configured, users are able to prefix commands with Template:Codeline to run said command with superuser (or other) privileges. For example:

$ sudo pacman -Syu

See the sudo manual for more information.


The configuration file for sudo is Template:Filename. This file should not be edited directly! Instead, users must run the command Template:Codeline as root, which opens a temporary copy of the configuration file in $EDITOR.

# visudo

The default editor is vi. If uncomfortable with vi, run this command first:

# export EDITOR=nano

Or change it permanently 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.

When the file is saved, Template:Codeline will double-check the file for syntax errors before overwriting the existing Template:Filename file. This safety feature exists because sudo will be rendered unusable if the configuration file contains errors.

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


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


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.

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

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:


and the shell will complete the command for him as:


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#Completion for more information.

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"

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 '


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.

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 "!":


To enable root login again:

$ sudo passwd root


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: