sudo - execute a command as another user (Super User do)

sudo -K | -L | -V | -h | -k | -l | -v

sudo [-HPSb] [-a auth_type] [-c class|-] [-p prompt] [-u username|#uid] {-e file [...] | -i | -s | command}

sudoedit [-S] [-a auth_type] [-p prompt] [-u username|#uid] file [...]

Allows execution of a command as another user.

Frequently used by admins to perform an administrative function.
Sometimes used by admins to run a command for a user.

If a user not listed in sudoers tries to run sudo, mail may be sent as defined at configure time or in sudoers

If sudo is run by root and the SUDO_USER environment variable is set, sudo will use this value to determine who the actual user is. This can be used by a user to log commands through sudo even when a root shell has been invoked. It also allows -e to remain useful even when being run via a sudo-run script or program. the sudoers lookup is still done for root, not the user specified by SUDO_USER.

sudo can log attempts.

OPTIONS
-H sets $HOME to the homedir of the target user (root by default) as specified in /etc/passwd By default, sudo does not modify HOME (see set_home and always_set_home in sudoers).
-L lists parameters that may be set with Defaults along with a short description for each.
syslog Syslog facility if syslog is being used for logging
syslog_goodpri priority when user authenticates
syslog_badpri priority when user authentication fails
long_otp_prompt Put OTP prompt on its own line
ignore_dot Ignore '.' in $PATH
mail_always Always send mail when sudo is run
mail_badpass if user authentication fails
mail_no_user if user is not in sudoers
mail_no_host if user is not in sudoers for this host
mail_no_perms if user is not allowed to run a command
mailerpath Path to mail program
mailto Address to send mail to
mailsub Subject line for mail messages
mailerflags Flags for mail program
tty_tickets Use a separate timestamp for each user/tty combo
lecture Lecture user first time running sudo
lecture_file File containing the lecture
authenticate Require users to authenticate by default
root_sudo Root may sudo
logfile Path to log file
log_host Log the hostname in the (non-syslog) log
log_year Log the year in the (non-syslog) log
loglinelen Length at which to wrap log file lines
(0 for no wrap)
shell_noargs If sudo is invoked with no arguments, start a shell
set_home Set $HOME to the target user when starting a shell with -s
always_set_home Always set $HOME to the target user's home directory
path_info Allow some information gathering to give useful error messages
fqdn Require fully-qualified hostnames in the sudoers
insults Insult the user when they enter an incorrect password
requiretty Only allow the user to run sudo if they have a tty
rootpw Prompt for root's password, not the users's
runaspw Prompt for the runas_default user's password, not the users's
targetpw Prompt for the target user's password, not the users's
passprompt Default password prompt
passwd_timeout Password prompt timeout
passwd_tries Number of tries to enter a password
timestamp_timeout Authentication timestamp timeout
badpass_message Incorrect password message
use_loginclass Apply defaults in the target user's login class if there is one
stay_setuid Only set the effective uid to the target user, not the real uid
preserve_groups Don't initialize the group vector to that of the target user
set_logname Set the LOGNAME and USER environment variables
env_reset Reset the environment to a default set of variables
env_check Environment variables to check for sanity
env_delete Environment variables to remove
env_keep Environment variables to preserve
umask Umask to use or 0777 to use user's
env_editor Visudo honors EDITOR environment variable
editor Path to the editor for use by visudo
timestampdir Path to authentication timestamp dir
default: /var/db/sudo
timestampowner Owner
exempt_group Users in this group are exempt from password and PATH requirements
runas_default Default user to run commands as
listpwWhen to require a password for 'list' pseudocommand
verifypw When to require a password for 'verify' pseudocommand
noexec Preload the dummy exec functions contained in 'noexec_file'
noexec_file File containing dummy exec functions
ignore_local_sudoers If LDAP directory is up, do we ignore local sudoers file
-P preserve invoking user's group vector.
By default, sudo initializes the group vector to the list of groups the target user is in. The real and effective group IDs, are set to match the target user.
-S stdin; causes sudo to read the password from the standard input instead of the terminal device.
-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.
-e (edit) option indicates that, instead of running a command, the user wishes to edit one or more files. In lieu of a command, the string "sudoedit" is used when consulting the sudoers file. If the user is authorized by sudoers the following steps are taken:
  1. Temporary copies are made of the files to be edited with the owner set to the invoking user.
  2. The editor specified by the VISUAL or EDITOR environment variables is run to edit the temporary files. If neither VISUAL nor EDITOR are set, the program listed in the editor sudoers variable is used.
  3. If they have been modified, the temporary files are copied back to their original loca- tion and the temporary versions are removed.
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.
-V version; If the invoking user is root prints out a list of the defaults compiled with and the machine's local network addresses.
-h -h (help) option causes sudo to print a usage message and exit.
-i The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the user that the command is being run as. The command name argument given to the shell begins with a - to tell the shell to run as a login shell. sudo attempts to change to that user's home directory before running the shell. It also initializes the environment, leaving TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, and unsetting all other environment variables. Note that because the shell to use is determined before the sudoers file is parsed, a runas_default setting in sudoers will specify the user to run the shell as but will not affect which shell is actually run.
-l list the allowed (and forbidden) commands for the user on the current host.
-p prompt); override the default password prompt and use a custom one.
%u user's login name that invoked sudo
%U user login name the command will be run as (defaults to root)
%h local hostname without the domain name
%H local hostname including the domain name (on if the machine's hostname is fully qualified or the fqdn sudoers option is set)
%% two consecutive % characters are collapsed into a single % character
-s The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd(5).
-- end of command line arguments. useful in conjunction with -s
-u username Default root. To specify a uid instead of a username, use #uid. Note that if the targetpw Defaults option is set (see sudoers(5)) it is not possible to run commands with a uid not listed in the password data- base.
-v given the -v (validate) option, sudo will update 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 kill 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 .logout .
-K sure kill removes the user's timestamp entirely.
Does not require a password.
-a authentication type; causes sudo to use the specified authentication type when validating the user, as allowed by "auth-sudo" in /etc/login.conf.
Only available on systems that support BSD authentication where sudo has been configured with --with-bsdauth .
-c class run the command with resources limited by the login class.
The class argument can be either a class name as defined in /etc/login.conf, or a single - character, the command should be run restricted by the default login capabilities for the user the command is run as.
If the class argument specifies an existing user class, the command must be run as root, or the sudo command must be run from a shell that is already root. Only available on systems with BSD login classes where sudo has been configured with the --with-logincap option.
When invoked as sudoedit, the -e option (described below), is implied.

RETURN VALUES

Upon successful execution of a program, the return value from sudo will be the return value of the command executed. (seems to mean 0) 1 a configuration/permission problem or sudo cannot execute the given command and error string is printed to stderr.
If sudo cannot stat(2) 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.)
A common reason for "permission denied": running an automounter 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 external commands.
Variables that control how dynamic loading and binding is done can be used to subvert the program that sudo runs. To combat this the LD_*, _RLD_*, SHLIB_PATH (HP-UX ), and LIBPATH (AIX only) environment variables are removed from the environment passed on to all commands executed.

sudo removes IFS, CDPATH, ENV, BASH_ENV, KRB_CONF, KRBCONFDIR, KRBTKFILE, KRB5_CONFIG, LOCALDOMAIN, RES_OPTIONS, HOSTALIASES, NLSPATH, PATH_LOCALE, TERMINFO, TERMINFO_DIRS and TERMPATH

If TERMCAP is set and is a pathname it is ignored.
If LC_* or LANGUAGE containing / or % are ignored.
Environment variables with a value beginning with () are removed as they could be interpreted as bash functions.
If sudo has been compiled with SecurID support, VAR_ACE, USR_ACE and DLC_ACE are cleared. The list of environment variables that sudo clears is contained in the output of -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). The actual PATH environment variable is not modified and is passed 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 will check the ownership of its timestamp directory (/var/db/sudo by default) and ignore the directory's contents if not owned by 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. 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. To get around this issue use a directory that is not world-writable for the timestamps (/var/adm/sudo for instance) or create /var/db/sudo with the appropriate owner (root) and permissions (0700) in the system startup files.

sudo will not honor timestamps farther in the future than current_time + 2 * TIMEOUT and will log this and complain.

sudo only logs the command it explicitly runs.
For commands sudo su or sudo sh, subsequent commands run from that shell will not be logged, nor will sudo's access control affect them. The same is true for commands that offer shell escapes (including most editors). must be taken when giving users access to commands via sudo to ve ify that the command does not inadvertently give the user an effective root shell.

ENVIRONMENT

EDITOR
Default editor to use in -e (sudoedit) mode if VISUAL is not set
HOME
In -s or -H mode (or if sudo was configured with the --enable-shell-sets-home option), set to homedir of the target user
PATH
Set to a sane value if sudo was configured with the --with-secure-path option
SHELL
Used to determine shell to run with -s option
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
prompt, PS1, will be set to its value
USER
the target user (root unless the -u option is specified)
VISUAL
Default editor to use in -e (sudoedit) mode

FILES

/private/etc/sudoers List of who can run what
 # sudoers file.
 # This file MUST be edited with the 'visudo' command as root.
 # See the sudoers man page for the details on how to write a sudoers file.
 # Host alias specification

 # User alias specification

 # Cmnd alias specification

 Defaults    env_reset
 Defaults    env_keep += "BLOCKSIZE"
 Defaults    env_keep += "COLORFGBG COLORTERM"
 Defaults    env_keep += "__CF_USER_TEXT_ENCODING"
 Defaults    env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE"
 Defaults    env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME"
 Defaults    env_keep += "LINES COLUMNS"
 Defaults    env_keep += "LSCOLORS"
 Defaults    env_keep += "SSH_AUTH_SOCK"
 Defaults    env_keep += "TZ"
 Defaults    env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY"
 Defaults    env_keep += "EDITOR VISUAL"

 # Runas alias specification

 # User privilege specification
 root    ALL=(ALL) ALL
 %admin  ALL=(ALL) ALL

 # Uncomment to allow people in group wheel to run all commands
 # %wheel    ALL=(ALL)   ALL

 # Same thing without a password
 # %wheel    ALL=(ALL)   NOPASSWD: ALL

 # Samples
 # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
 # %users  localhost=/sbin/shutdown -h now
 

/var/db/sudo Directory containing timestamps

EXAMPLES

Note: the following examples assume suitable sudoers(5) entries.

To get a listing of an protected directory:
$ sudo ls -l /usr/local/secret

To list the home directory of user yazza on a machine where the file system holding ~yazza is not exported as root:
$ sudo -u yazza ls ~yazza

To edit index.html as user www:

$ sudo -u www vi ~www/index.html

To reboot a machine:
$ sudo shutdown -r now "Rebooting to recycle log files. "

listing of the directories in /home to usage
This runs the commands in a sub-shell to make cd and file redirection work.

$ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"

SEE ALSO grep(1), su(1), stat(2), sudoers(5), passwd(5), visudo(8)