sudo, sudoedit - execute a command as another user

sudo allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file.

Syntax
      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 [...] 

Options
   -H   Set the HOME environment variable to the homedir of the target user
        (root by default) as specified in passwd(5). By default, sudo does
        not modify HOME (see set_home and always_set_home in sudoers(5)).

   -K  (sure kill) like kill (-k) except that it removes the user's timestamp
        entirely. Like -k, this option does not require a password.

   -L   list defaults, list out the parameters that may be set in a Defaults
        line along with a short description for each. This option is useful
        in conjunction with grep(1).

   -P   Preserve group vector, causes sudo to preserve the invoking user's
        group vector unaltered. By default, sudo will initialize the group
        vector to the list of groups the target user is in. The real and
        effective group IDs, however, are still set to match the target user.

   -S   stdin, read the password from the standard input instead of the terminal.

   -V   Version, print the version number and exit.
        If the invoking user is already root the -V option will print out a
        list of the defaults sudo was compiled with as well as the machine's
        local network addresses.

   -a   Authentication type, 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 has been configured with the
        --with-bsdauth option.

   -b   Background, 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.

   -c   Class, 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 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.
        This option is only available on systems with BSD login classes
        where sudo has been configured with the --with-logincap option.

   -e   Edit, 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 location 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. 

   -h   Help, print a usage message and exit.

   -i   Simulate initial login, run 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.

   -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 a .logout file.

   -l   List, list out the allowed (and forbidden) commands for the user on the current host.

   -p   Prompt, allows you to override the default password prompt and use a custom one.
        The following percent ('%') escapes are supported:
         %u  expanded to the invoking user's login name
         %U  expanded to the login name of the user the command will be run as (defaults to root)
         %h  expanded to the local hostname without the domain name
         %H  expanded to the 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    Shell, runs the shell specified by the SHELL environment variable if it is
        set or the shell as specified in passwd(5).

   -u   User, causes sudo to run the specified command as a user other than 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 database.

   -v   Validate, 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.

   --   Indicates that sudo should stop processing command line arguments.
        It is most useful in conjunction with the -s flag. 

Return Values
Upon successful execution of a program, the return value from sudo will simply 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(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.) This should not happen under normal circumstances. The most common reason for stat(2) to return "permission denied" is if you are running an automounter and one of the directories in your PATH is on a machine that is currently unreachable.

sudo utilizes the following environment variables: EDITOR, HOME, PATH,SHELL,SUDO_PROMPT, SUDO_COMMAND, SUDO_USER, SUDO_UID, SUDO_GID, SUDO_PS1, USER, VISUAL.

Files
List of who can run what: /etc/sudoers
Directory containing timestamps: /var/run/sudo

Examples

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

To get a file listing of an unreadable directory:

$ sudo ls /usr/local/protected

To run the last command as root, useful when you forget to use sudo for a command. !! grabs the last run command.

sudo !!

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 the index.html file as user JDoe:

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

To shutdown a machine:

$ sudo shutdown -r +15 "quick reboot"

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.

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

"It was just like Romeo and Juliet, only it ended in tragedy" ~ Milhouse

Related:

grep - Search file(s) for lines that match a given pattern
su - Substitute user identity
stat(2)
login_cap(3)
sudoers(5)
passwd - Modify a user password
visudo(8)
www.sudo.ws - Todd Miller, sudo maintainer
Equivalent Windows command: RUNAS /user


© Copyright SS64.com 1999-2013
Some rights reserved