sudo allows a permitted user to execute a command as the superuser or another user, as specified by the security policy.
The supported security policy is sudoers, which is configured via the file /etc/sudoers.
The security policy determines what privileges, if any, a user has to run sudo. The policy may require that users authenticate themselves with a password. If authentication is required, sudo will exit if the user's password is not entered within a configurable time limit. The default password prompt timeout for the sudoers security policy is 5 minutes.
Security policies may support credential caching to allow the user to run sudo again for a period of time without requiring authentication. The sudoers policy caches credentials for 5 minutes, unless overridden in sudoers. By running sudo with the -v option, a user can update the cached credentials without running a command.
When invoked as sudoedit, the -e option (described below), is implied.
Security policies may log successful and failed attempts to use sudo.
Syntax
sudo -h | -K | -k | -V
|
sudo -v [-AknS][-g group][-p prompt][-u user] |
sudo -l [-AknS][-g group][-p prompt][-U user][-u user][ command] |
sudo [-AbEHnPS][-C num][-g group][-p prompt][-u user][ VAR=value] [-i | -s][command] |
sudoedit [-AknS][-C num][-g group][-p prompt][-u user] file ... |
Normally, if sudo requires a password, it will read it from the user's terminal. If the -A (askpass) option is specified, a helper program is executed to read the user's password and output the password to the standard output. If the SUDO_ASKPASS environment variable is set, it specifies the path to the helper program. If no askpass program is available, sudo will exit with an error.
Run the given command in the background. Note that it is not possible to use shell job control to manipulate background processes started by sudo. Most interactive commands will fail to work properly in background mode.
Close all file descriptors greater than or equal to num before executing a command. Values less than three are not permitted.
Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.
Edit one or more files instead of running a command. In lieu of a path name, the string "sudoedit" is used when consulting the security policy. If the user is authorized by the policy, the following steps are taken:
If the specified file does not exist, it will be created. Note that unlike most commands run by sudo, the editor is run with the invoking user's environment unmodified. If, for some reason, sudo is unable to update a file with its edited version, the user will receive a warning and the edited copy will remain in a temporary file.
Run the command with the primary group set to group instead of the primary group specified by the target user's password database entry. The group may be either a group name or a numeric group ID (GID) prefixed with the '#' character (e.g. #0 for GID 0). When running a command as a GID, many shells require that the '#' be escaped with a backslash ('\'). If no -u option is specified, the command will be run as the invoking user. In either case, the primary group will be set to group.
Request that the security policy set the HOME environment variable to the home directory specified by the target user's password database entry. Depending on the policy, this may be the default behavior.
Display a short help message to the standard output and exit.
Run the shell specified by the target user's password database entry as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution via the shell's -c option. If no command is specified, an interactive shell is executed. sudo attempts to change to that user's home directory before running the shell. The command is run with an environment similar to the one a user would receive at log in. The Command environment section in the sudoers manual documents how the -i option affects the environment in which a command is run when the sudoers policy is in use.
Similar to the -k option, except that it removes the user's cached credentials entirely and may not be used in conjunction with a command or other option. This option does not require a password.
When used without a command, invalidates the user's cached credentials. In other words, the next time sudo is run a password will be required. This option does not require a password and was added to allow a user to revoke sudo permissions from a .logout file. When used in conjunction with a command or an option that may require a password, this option will cause sudo to ignore the user's cached credentials. As a result, sudo will prompt for a password (if one is required by the security policy) and will not update the user's cached credentials.
If no command is specified, list the allowed (and forbidden) commands for the invoking user (or the user specified by the -U option) on the current host. A longer list format is used if this option is specified multiple times and the security policy supports a verbose output format. If a command is specified and is permitted by the security policy, the fully-qualified path to the command is displayed along with any command line arguments. If command is specified but not allowed, sudo will exit with a status value of 1.
Avoid prompting the user for input of any kind. If a password is required for the command to run, sudo will display an error message and exit.
Preserve the invoking user's group vector unaltered. By default, the sudoers policy will initialize the group vector to the list of groups the target user is a member of. The real and effective group IDs, however, are still set to match the target user.
Use a custom password prompt with optional escape sequences. The following percent ('%') escape sequences are supported by the sudoers policy:
expanded to the host name including the domain name (on if the machine's host name is fully qualified or the fqdn option is set in sudoers)
expanded to the local host name without the domain name
expanded to the name of the user whose password is being requested (respects the rootpw, targetpw, and runaspw flags in sudoers)
expanded to the login name of the user the command will be run as (defaults to SYSROOT unless the -u option is also specified)
expanded to the invoking user's login name
two consecutive '%' characters are collapsed into a single '%' character
Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character.
Run the shell specified by the SHELL environment variable if it is set or the shell specified by the invoking user's password database entry. If a command is specified, it is passed to the shell for execution via the shell's -c option. If no command is specified, an interactive shell is executed.
Used in conjunction with the -l option to list the privileges for user instead of for the invoking user. The security policy may restrict listing other users' privileges. The sudoers policy only allows the super user or a user with the ALL privilege on the current host to use this option.
Run the command as a user other than the default target user (usually SYSROOT). The user may be either a user name or a numeric user ID (UID) prefixed with the '#' character (e.g. #0 for UID 0). When running commands as a UID, many shells require that the '#' be escaped with a backslash ('\').
Print the sudo version string as well as the version string of the security policy plugin. If the invoking user is already the super user the -V option will display the arguments passed to configure when sudo was built and plugins may display more verbose information such as default options.
Update the user's cached credentials, authenticating the user if necessary. For the sudoers plugin, this extends the sudo timeout for another 5 minutes by default, but does not run a command.
Indicates that sudo should stop processing command line arguments. Environment variables to be set for the command may also be passed on the command line in the form of VAR=value, e.g.
Variables passed on the command line are subject to restrictions imposed by the security policy plugin. The sudoers policy subjects variables passed on the command line to the same restrictions as normal environment variables with one important exception. If the setenv option is set in sudoers, the command to be run has the SETENV tag set or the command matched is ALL, the user may set variables that would otherwise be forbidden. See sudoers for more information. |
Command execution
When sudo executes a command, the security policy specifies the execution environment for the command. Typically, the real and effective user and group and IDs are set to match those of the target user and the group vector is initialized based on the group database (unless the -P option was specified). The following parameters may be specified by security policy:
Process model When sudo runs a command, it calls fork, sets up the execution environment as described above, and calls the execve system call in the child process. The main sudo process waits until the command has completed, then passes the command's exit status to the security policy's close function and exits. Signal handling When the command is run as a child of the sudo process, sudo will relay signals it receives to the command. The SIGHUP, SIGINT and SIGQUIT signals are not relayed unless they are sent by a user process, not the kernel. Otherwise, the command would receive SIGINT twice every time the user entered control-C. Some signals, such as SIGSTOP and SIGKILL, cannot be caught and thus will not be relayed to the command. As a general rule, SIGTSTP should be used instead of SIGSTOP when you wish to suspend a command being run by sudo. |
Exit status
Upon successful execution of a program, the exit status from sudo will simply be the exit status of the program that was executed. Otherwise, sudo exits with a 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 the standard error. 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 is not really a directory, the entry is ignored and no error is printed.) This should not happen under normal circumstances. |
Security notes
sudo tries to be safe when executing external commands. 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. Please note that sudo will normally only log the command it explicitly runs. If a user runs a command such as |
Variable
sudo utilizes the following environment variables. The security policy has control over the actual content of the command's environment. EDITOR Default editor to use in -e (sudoedit) mode if neither SUDO_EDITOR nor VISUAL is set. In -i mode or when env_reset is enabled in sudoers, set to the mail spool of the target user. HOME Set to the home directory of the target user if -i or -H are specified, env_reset or always_set_home are set in sudoers, or when the -s option is specified and set_home is set in sudoers. PATH May be overridden by the security policy. SHELL Used to determine shell to run with -s option. SUDO_ASKPASS Specifies the path to a helper program used to read the password if no terminal is available or if the -A option is specified. SUDO_COMMAND Set to the command run by sudo. SUDO_EDITOR Default editor to use in -e (sudoedit) mode. SUDO_GID Set to the group ID of the user who invoked sudo. SUDO_PROMPT Used as the default password prompt. SUDO_PS1 If set, PS1 will be set to its value for the program being run. SUDO_UID Set to the user ID of the user who invoked sudo. SUDO_USER Set to the login name of the user who invoked sudo. USER Set to the target user (SYSROOT unless the -u option is specified). VISUAL Default editor to use in -e (sudoedit) mode if SUDO_EDITOR is not set. |
Example
Note: the following examples assume a properly configured security policy. To get a file listing of an unreadable directory:
To edit the index.html file as user www:
To view system logs only accessible to the super user and users in the adm group:
To run an editor as jim with a different primary group:
To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work:
|
See also
sudoers, visudo |