Linux sudo command
This page describes the Linux version of sudo.
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.
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.
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
|-V||The -V (version) option causes sudo to print the version number and exit. If the invoking user is already root, the -V option prints out a list of the defaults sudo was compiled with and the machine's local network addresses.|
|-l||The -l (list) option prints out the commands allowed (and forbidden) the user on the current host.|
|-L||The -L (list defaults) option lists out the parameters set in a Defaults line with a short description for each. This option is useful in conjunction with grep.|
|-h||The -h (help) option causes sudo to print a usage message and exit.|
|-v||If given the -v (validate) option, sudo updates the user's timestamp, prompting for the user's password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.|
|-k||The -k (kill) option to sudo invalidates the user's timestamp by setting the time on it to the epoch. 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.|
|-K||The -K (sure kill) option to sudo removes the user's timestamp entirely. Likewise, this option does not require a password.|
|-b||The -b (background) option tells sudo to run the given command in the background. Note that if you use the -b option you cannot use shell job control to manipulate the process.|
|-p||The -p (prompt) option allows you to override the default password prompt and use a custom one. The following percent ('%') escapes are supported:
%u is expanded to the invoking user's login name;
%U is expanded to the login name of the user the command will run as (which defaults to root);
%h is expanded to the local hostname without the domain name;
%H is expanded to the local hostname including the domain name (only if the machine's hostname is fully qualified or the "fqdn" sudoers option is set);
%% (two consecutive % characters) are collapsed into a single % character.
|-c||The -c (class) option causes sudo to run the specified command with resources limited by the specified login class. The class argument can be either a class name as defined in /etc/login.conf, or a single '-' character. Specifying a class of - indicates that the command should run restricted by the default login capabilities for the user running the command. If the class argument specifies an existing user class, the command must run as root, or the sudo command must run from a shell that is already root. This option is only available on systems with BSD login classes where sudo is configured with the --with-logincap option.|
|-a||The -a (authentication type) option causes sudo to use the specified authentication type when validating the user, as allowed by /etc/login.conf. The system administrator may specify a list of sudo-specific authentication methods by adding an "auth-sudo" entry in /etc/login.conf. This option is only available on systems that support BSD authentication where sudo is configured with the --with-bsdauth option.|
|-u||The -u (user) option causes sudo to run the specified command as a user other than root. To specify a uid instead of a username, use #uid.|
|-s||The -s (shell) option runs the shell specified by the SHELL environment variable if it's set or the shell as specified in the file passwd.|
|-H||The -H (HOME) option sets the HOME environment variable to the home directory of the target user (root by default) as specified in passwd. By default, sudo does not modify HOME.|
|-P||The -P (preserve group vector) option causes sudo to preserve the user's group vector unaltered. By default, sudo will initialize the group vector to the list of groups of the target user. The real and effective group IDs, however, are still set to match the target user.|
|-r||The -r (role) option causes the new (SELinux) security context to have the role specified by ROLE.|
|-t||The -t (type) option causes the new (SELinux) security context to have the type (domain) specified by TYPE. If no type is specified, the default type is derived from the specified role.|
|-S||The -S (stdin) option causes sudo to read the password from standard input instead of the terminal device.|
|--||The -- flag indicates that sudo should stop processing command line arguments. It is most useful in conjunction with the -s flag.|
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.
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:
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.
sudo uses the following environment variables:
|PATH||Set to a sane value if SECURE_PATH is set|
|SHELL||Used to determine shell to run with -s option|
|USER||Set to the target user (root unless the -u option is specified)|
|HOME||In -s or -H mode (or if sudo was configured with the --enable-shell-sets-home option), set to home directory of the target user.|
|SUDO_PROMPT||Used as the default password prompt|
|SUDO_COMMAND||Set to the command run by sudo|
|SUDO_USER||Set to the login of the user who invoked sudo|
|SUDO_UID||Set to the uid of the user who invoked sudo|
|SUDO_GID||Set to the gid of the user who invoked sudo|
|SUDO_PS1||If set, PS1 will be set to its value|
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.
Extend/reset sudo's automatic authentication timeout, allowing you to continue issuing sudo commands without entering a password.
"Kill" sudo authentication for the current user. The next sudo command requires a password.