On Unix-like operating systems, the sudo command (“superuser do”) allows a user with proper permissions to execute a command as another user. By default, sudo executes commands as root.

This page describes the Linux version of sudo.

When it was first created, sudo could only switch to the superuser, which is why it’s short for “superuser do.” Today, it can also switch to other users, so some may now refer to this command as “substitute user, do” or “switch user, do.”

Description

sudo allows a permitted user to execute a command as another user, according to specifications in the /etc/sudoers file. The real and effective uid and gid of the issuing user are then set to match those of the target user account as specified in the passwd file.

  • Description
  • Syntax
  • Return values
  • Security notes
  • Environment variables
  • Examples
  • Related commands
  • Linux commands help

By default, sudo requires that users authenticate themselves with a password. By default, this is the user’s password, not the root password itself.

Once a user is authenticated, a timestamp is recorded and the user may use sudo without a password for a short time (5 minutes, unless configured differently in sudoers). This timestamp can be renewed if the user issues sudo with the -v flag.

If a user not listed in sudoers tries to run a command using sudo, it is considered an unsuccessful attempt to breach system security and mail is sent to the proper authorities, as defined at configure time or in the sudoers file. The default authority to be notified of unsuccessful sudo attempts is root. Note that the mail isn’t sent if an unauthorized user tries to run sudo with the -l or -v flags; this allows users to determine for themselves whether or not they are allowed to use sudo.

sudo can log both successful and unsuccessful attempts (and errors) to syslog, a unique log file, or both. By default, sudo will log to syslog but this can be changed at configure time or in the sudoers file.

To edit the sudoers file, use the visudo command.

Syntax

sudo -V | -h | -l | -L | -v | -k | -K | -s | [ -H ] [-P ] [-S ] [ -b ] | [ -p prompt ] [ -c class|- ] [ -a auth_type ] [-r role ] [-t type ] [ -u username|#uid ] command

Options

Return values

Upon successful execution of a program, the return value from sudo will be the return value of the program that was executed.

Otherwise, sudo quits with an exit value of 1 if there is a configuration/permission problem or if sudo cannot execute the given command. In the latter case the error string is printed to stderr. If sudo cannot stat one or more entries in the user’s PATH an error is printed on stderr. (If the directory does not exist or if it’s not a directory, the entry is ignored and no error is printed.) This should not happen under normal circumstances. The most common reason for stat to return “permission denied” is if you are running an auto-mounter and one of the directories in your PATH is on a machine that is currently unreachable.

Security notes

sudo tries to be safe when executing commands. Variables that control how dynamic loading and binding is done can subvert the program that sudo runs. To combat this, some system-specific environment variables are removed from the environment that is passed on to the commands that are executed. Other variables that sudo removes from the environment include:

  • IFS
  • ENV
  • BASH_ENV
  • KRB_CONF
  • KRBCONFDIR
  • KRBTKFILE
  • KRB5_CONFIG
  • LOCALDOMAIN
  • RES_OPTIONS
  • HOSTALIASES
  • NLSPATH
  • PATH_LOCALE
  • TERMINFO
  • TERMINFO_DIRS
  • TERMPATH

as they too can pose a threat. If the TERMCAP variable is set and is a pathname, it too is ignored. Additionally, if certain variables contain the / or % characters, they are ignored.

If sudo was compiled with SecurID support, the VAR_ACE, USR_ACE and DLC_ACE variables are cleared as well. The list of environment variables that sudo clears is contained in the output of sudo -V when run as root.

To prevent command spoofing, sudo checks “.” and "" (both denoting current directory) last when searching for a command in the user’s PATH (if one or both are in the PATH). Note, however, that the actual PATH environment variable is not modified and is passed unchanged to the program that sudo executes.

For security reasons, if your OS supports shared libraries and does not disable user-defined library search paths for setuid programs (most do), either use a linker option that disables this behavior or link sudo statically.

sudo checks the ownership of its timestamp directory (/var/run/sudo by default) and ignore the directory’s contents if it’s not owned by root and only writable by root. On systems that allow non-root users to give away files via chown, if the timestamp directory is located in a directory writable by anyone (e.g.: /tmp), it is possible for a user to create the timestamp directory before sudo is run. However, because sudo checks the ownership and mode of the directory and its contents, the only damage that can be done is to “hide” files by putting them in the timestamp dir. This is unlikely to happen since once the timestamp dir is owned by root and inaccessible by any other user the user placing files there would be unable to get them back out. To get around this issue, you can use a directory that is not world-writable for the timestamps (/var/adm/sudo for instance) or create /var/run/sudo with the appropriate owner (root) and permissions (0700) in the system startup files.

sudo will not honor timestamps set far in the future. Timestamps with a date greater than current_time + 2 * TIMEOUT is ignored and sudo will log and complain. This is done to keep a user from creating his/her own timestamp with a bogus date on systems that allow users to give away files.

Please note that sudo only logs the command it explicitly runs. If a user runs a command such as “sudo su” or “sudo sh”, subsequent commands run from that shell are not logged, nor will sudo’s access control affect them. The same is true for commands that offer shell escapes (including most editors). Because of this, care must be taken when giving users access to commands via sudo to verify that the command does not inadvertently give the user an effective root shell.

Environment variables

sudo uses the following environment variables:

Examples

sudo shutdown -r now

Restart the system; run the shutdown command as root.

sudo -u hope ls /home/otheruser/Documents

List the contents of the /home/otheruser/Documents directory as the user hope.

sudo -u hope -g otherusers mkdir /home/otheruser/Documents/newfiles

Create a new directory with the mkdir command, as the user hope, with hope’s current group set to otherusers. hope must be a member of the otherusers group.

sudo -v

Extend/reset sudo’s automatic authentication timeout, allowing you to continue issuing sudo commands without entering a password.

sudo -k

“Kill” sudo authentication for the current user. The next sudo command requires a password.

su — Become the superuser or another user.visudo — Edit the sudoers file, which defines who can run sudo.