1 Copyright (c) 1994-1996, 1998-2005, 2007-2011
2 Todd C. Miller <Todd.Miller@courtesan.com>
4 Permission to use, copy, modify, and distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
8 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 Sponsored in part by the Defense Advanced Research Projects
18 Agency (DARPA) and Air Force Research Laboratory, Air Force
19 Materiel Command, USAF, under agreement number F39502-99-1-0512.
25 sudo, sudoedit - execute a command as another user
29 B<sudo> S<[B<-D> I<level>]> B<-h> | B<-K> | B<-k> | B<-V>
31 B<sudo> B<-v> [B<-AknS>]
32 S<[B<-a> I<auth_type>]>
34 S<[B<-g> I<group name>|I<#gid>]> S<[B<-p> I<prompt>]>
35 S<[B<-u> I<user name>|I<#uid>]>
37 B<sudo> B<-l[l]> [B<-AknS>]
38 S<[B<-a> I<auth_type>]>
40 S<[B<-g> I<group name>|I<#gid>]> S<[B<-p> I<prompt>]>
41 S<[B<-U> I<user name>]> S<[B<-u> I<user name>|I<#uid>]> [I<command>]
44 S<[B<-a> I<auth_type>]>
47 S<[B<-c> I<class>|I<->]>
48 S<[B<-g> I<group name>|I<#gid>]> S<[B<-p> I<prompt>]>
49 S<[B<-r> I<role>]> S<[B<-t> I<type>]>
50 S<[B<-u> I<user name>|I<#uid>]>
51 S<[B<VAR>=I<value>]> S<[B<-i> | B<-s>]> [I<command>]
54 S<[B<-a> I<auth_type>]>
56 S<[B<-c> I<class>|I<->]>
58 S<[B<-g> I<group name>|I<#gid>]> S<[B<-p> I<prompt>]>
59 S<[B<-u> I<user name>|I<#uid>]> file ...
63 B<sudo> allows a permitted user to execute a I<command> as the
64 superuser or another user, as specified by the security policy.
65 The real and effective uid and gid are set to match those of the
66 target user, as specified in the password database, and the group
67 vector is initialized based on the group database (unless the B<-P>
68 option was specified).
70 B<sudo> supports a plugin architecture for security policies and
71 input/output logging. Third parties can develop and distribute
72 their own policy and I/O logging modules to work seemlessly with
73 the B<sudo> front end. The default security policy is I<sudoers>,
74 which is configured via the file F<@sysconfdir@/sudoers>, or via
75 LDAP. See the L<PLUGINS> section for more information.
77 The security policy determines what privileges, if any, a user has
78 to run B<sudo>. The policy may require that users authenticate
79 themselves with a password or another authentication mechanism. If
80 authentication is required, B<sudo> will exit if the user's password
81 is not entered within a configurable time limit. This limit is
82 policy-specific; the default password prompt timeout for the
83 I<sudoers> security policy is C<@password_timeout@> minutes.
85 Security policies may support credential caching to allow the user
86 to run B<sudo> again for a period of time without requiring
87 authentication. The I<sudoers> policy caches credentials for
88 C<@timeout@> minutes, unless overridden in L<sudoers(5)>. By
89 running B<sudo> with the B<-v> option, a user can update the cached
90 credentials without running a I<command>.
92 When invoked as B<sudoedit>, the B<-e> option (described below),
95 Security policies may log successful and failed attempts to use
96 B<sudo>. If an I/O plugin is configured, the running command's
97 input and output may be logged as well.
101 B<sudo> accepts the following command line options:
107 Normally, if B<sudo> requires a password, it will read it from the
108 user's terminal. If the B<-A> (I<askpass>) option is specified,
109 a (possibly graphical) helper program is executed to read the user's
110 password and output the password to the standard output. If the
111 C<SUDO_ASKPASS> environment variable is set, it specifies the path
112 to the helper program. Otherwise, if F<@sysconfdir@/sudo.conf>
113 contains a line specifying the askpass program, that value will be
116 # Path to askpass helper program
117 Path askpass /usr/X11R6/bin/ssh-askpass
119 If no askpass program is available, sudo will exit with an error.
123 The B<-a> (I<authentication type>) option causes B<sudo> to use the
124 specified authentication type when validating the user, as allowed
125 by F</etc/login.conf>. The system administrator may specify a list
126 of sudo-specific authentication methods by adding an "auth-sudo"
127 entry in F</etc/login.conf>. This option is only available on systems
128 that support BSD authentication.
132 The B<-b> (I<background>) option tells B<sudo> to run the given
133 command in the background. Note that if you use the B<-b>
134 option you cannot use shell job control to manipulate the process.
135 Most interactive commands will fail to work properly in background
140 Normally, B<sudo> will close all open file descriptors other than
141 standard input, standard output and standard error. The B<-C>
142 (I<close from>) option allows the user to specify a starting point
143 above the standard error (file descriptor three). Values less than
144 three are not permitted. The security policy may restrict the
145 user's ability to use the B<-C> option. The I<sudoers> policy only
146 permits use of the B<-C> option when the administrator has enabled
147 the I<closefrom_override> option.
151 The B<-c> (I<class>) option causes B<sudo> to run the specified command
152 with resources limited by the specified login class. The I<class>
153 argument can be either a class name as defined in F</etc/login.conf>,
154 or a single '-' character. Specifying a I<class> of C<-> indicates
155 that the command should be run restricted by the default login
156 capabilities for the user the command is run as. If the I<class>
157 argument specifies an existing user class, the command must be run
158 as root, or the B<sudo> command must be run from a shell that is already
159 root. This option is only available on systems with BSD login classes.
163 Enable debugging of B<sudo> plugins and B<sudo> itself. The I<level>
164 may be a value from 1 through 9.
168 The B<-E> (I<preserve> I<environment>) option indicates to the
169 security policy that the uses wishes to preserve their existing
170 environment variables. The security policy may return an error if
171 the B<-E> option is specified and the user does not have permission
172 to preserve the environment.
176 The B<-e> (I<edit>) option indicates that, instead of running a
177 command, the user wishes to edit one or more files. In lieu of a
178 command, the string "sudoedit" is used when consulting the security
179 policy. If the user is authorized by the policy, the following
186 Temporary copies are made of the files to be edited with the owner
187 set to the invoking user.
191 The editor specified by the policy is run to edit the temporary files.
192 The I<sudoers> policy uses the C<SUDO_EDITOR>, C<VISUAL> and C<EDITOR>
193 environment variables (in that order). If none of C<SUDO_EDITOR>,
194 C<VISUAL> or C<EDITOR> are set, the first program listed in the
195 I<editor> L<sudoers(5)> option is used.
199 If they have been modified, the temporary files are copied back to
200 their original location and the temporary versions are removed.
204 If the specified file does not exist, it will be created. Note
205 that unlike most commands run by B<sudo>, the editor is run with
206 the invoking user's environment unmodified. If, for some reason,
207 B<sudo> is unable to update a file with its edited version, the
208 user will receive a warning and the edited copy will remain in a
213 Normally, B<sudo> runs a command with the primary group set to the
214 one specified by the password database for the user the command is
215 being run as (by default, root). The B<-g> (I<group>) option causes
216 B<sudo> to run the command with the primary group set to I<group>
217 instead. To specify a I<gid> instead of a I<group name>, use
218 I<#gid>. When running commands as a I<gid>, many shells require
219 that the '#' be escaped with a backslash ('\'). If no B<-u> option
220 is specified, the command will be run as the invoking user (not
221 root). In either case, the primary group will be set to I<group>.
225 The B<-H> (I<HOME>) option requests that the security policy set
226 the C<HOME> environment variable to the home directory of the target
227 user (root by default) as specified by the password database.
228 Depending on the policy, this may be the default behavior.
232 The B<-h> (I<help>) option causes B<sudo> to print a short help message
233 to the standard output and exit.
237 The B<-i> (I<simulate initial login>) option runs the shell specified
238 by the password database entry of the target user as a login shell.
239 This means that login-specific resource files such as C<.profile>
240 or C<.login> will be read by the shell. If a command is specified,
241 it is passed to the shell for execution via the shell's B<-c> option.
242 If no command is specified, an interactive shell is executed.
243 B<sudo> attempts to change to that user's home directory before
244 running the shell. The security policy shall initialize the
245 environment to a minimal set of variables, similar to what is present
250 The B<-K> (sure I<kill>) option is like B<-k> except that it removes
251 the user's cached credentials entirely and may not be used in
252 conjunction with a command or other option. This option does not
253 require a password. Not all security policies support credential
258 When used alone, the B<-k> (I<kill>) option to B<sudo> invalidates
259 the user's cached credentials. The next time B<sudo> is run a
260 password will be required. This option does not require a password
261 and was added to allow a user to revoke B<sudo> permissions from a
262 .logout file. Not all security policies support credential
265 When used in conjunction with a command or an option that may require
266 a password, the B<-k> option will cause B<sudo> to ignore the user's
267 cached credentials. As a result, B<sudo> will prompt for a password
268 (if one is required by the security policy) and will not update the
269 user's cached credentials.
271 =item -l[l] [I<command>]
273 If no I<command> is specified, the B<-l> (I<list>) option will list
274 the allowed (and forbidden) commands for the invoking user (or the
275 user specified by the B<-U> option) on the current host. If a
276 I<command> is specified and is permitted by the security policy,
277 the fully-qualified path to the command is displayed along with any
278 command line arguments. If I<command> is specified but not allowed,
279 B<sudo> will exit with a status value of 1. If the B<-l> option
280 is specified with an B<l> argument (i.e. B<-ll>), or if B<-l> is
281 specified multiple times, a longer list format is used.
285 The B<-n> (I<non-interactive>) option prevents B<sudo> from prompting
286 the user for a password. If a password is required for the command
287 to run, B<sudo> will display an error messages and exit.
291 The B<-P> (I<preserve> I<group vector>) option causes B<sudo> to
292 preserve the invoking user's group vector unaltered. By default,
293 the I<sudoers> policy will initialize the group vector to the list
294 of groups the target user is in. The real and effective group IDs,
295 however, are still set to match the target user.
299 The B<-p> (I<prompt>) option allows you to override the default
300 password prompt and use a custom one. The following percent (`C<%>')
301 escapes are supported by the I<sudoers> policy:
307 expanded to the host name including the domain name (on if
308 the machine's host name is fully qualified or the I<fqdn> option
309 is set in L<sudoers(5)>)
313 expanded to the local host name without the domain name
317 expanded to the name of the user whose password is being requested
318 (respects the I<rootpw>, I<targetpw> and I<runaspw> flags in
323 expanded to the login name of the user the command will be run as
324 (defaults to root unless the C<-u> option is also specified)
328 expanded to the invoking user's login name
332 two consecutive C<%> characters are collapsed into a single C<%> character
336 The prompt specified by the B<-p> option will override the system
337 password prompt on systems that support PAM unless the
338 I<passprompt_override> flag is disabled in I<sudoers>.
342 The B<-r> (I<role>) option causes the new (SELinux) security context to
343 have the role specified by I<role>.
347 The B<-S> (I<stdin>) option causes B<sudo> to read the password from
348 the standard input instead of the terminal device. The password must
349 be followed by a newline character.
353 The B<-s> (I<shell>) option runs the shell specified by the I<SHELL>
354 environment variable if it is set or the shell as specified in the
355 password database. If a command is specified, it is passed to the
356 shell for execution via the shell's B<-c> option. If no command
357 is specified, an interactive shell is executed.
361 The B<-t> (I<type>) option causes the new (SELinux) security context to
362 have the type specified by I<type>. If no type is specified, the default
363 type is derived from the specified role.
367 The B<-U> (I<other user>) option is used in conjunction with the
368 B<-l> option to specify the user whose privileges should be listed.
369 The security policy may restrict listing other users' privileges.
370 The I<sudoers> policy only allows root or a user with the C<ALL>
371 privilege on the current host to use this option.
375 The B<-u> (I<user>) option causes B<sudo> to run the specified
376 command as a user other than I<root>. To specify a I<uid> instead
377 of a I<user name>, use I<#uid>. When running commands as a I<uid>,
378 many shells require that the '#' be escaped with a backslash ('\').
379 Security policies may restrict I<uid>s to those listed in the
380 password database. The I<sudoers> policy allows I<uid>s that are
381 not in the password database as long as the I<targetpw> option is
382 not set. Other security policies may not support this.
386 The B<-V> (I<version>) option causes B<sudo> to print its version
387 string and the version string of the security policy plugin and any
388 I/O plugins. If the invoking user is already root the B<-V> option
389 will display the arguments passed to configure when I<sudo> was
390 built and plugins may display more verbose information such as
395 When given the B<-v> (I<validate>) option, B<sudo> will update the
396 user's cached credentials, authenticating the user's password if
397 necessary. For the I<sudoers> plugin, this extends the B<sudo>
398 timeout for another C<@timeout@> minutes (or whatever the timeout
399 is set to in I<sudoers>) but does not run a command. Not all
400 security policies support cached credentials.
404 The B<--> option indicates that B<sudo> should stop processing command
409 Environment variables to be set for the command may also be passed
410 on the command line in the form of B<VAR>=I<value>, e.g.
411 B<LD_LIBRARY_PATH>=I</usr/local/pkg/lib>. Variables passed on the
412 command line are subject to the same restrictions as normal environment
413 variables with one important exception. If the I<setenv> option
414 is set in I<sudoers>, the command to be run has the C<SETENV> tag
415 set or the command matched is C<ALL>, the user may set variables
416 that would overwise be forbidden. See L<sudoers(5)> for more information.
420 Plugins are dynamically loaded based on the contents of the
421 F<@sysconfdir@/sudo.conf> file. If no F<@sysconfdir@/sudo.conf>
422 file is present, or it contains no C<Plugin> lines, B<sudo>
423 will use the traditional I<sudoers> security policy and I/O logging,
424 which corresponds to the following F<@sysconfdir@/sudo.conf> file.
427 # Default @sysconfdir@/sudo.conf file
430 # Plugin plugin_name plugin_path
431 # Path askpass /path/to/askpass
432 # Path noexec /path/to/noexec.so
434 # The plugin_path is relative to @prefix@/libexec unless
436 # The plugin_name corresponds to a global symbol in the plugin
437 # that contains the plugin interface structure.
439 Plugin policy_plugin sudoers.so
440 Plugin io_plugin sudoers.so
442 A C<Plugin> line consists of the C<Plugin> keyword, followed by the
443 I<symbol_name> and the I<path> to the shared object containing the
444 plugin. The I<symbol_name> is the name of the C<struct policy_plugin>
445 or C<struct io_plugin> in the plugin shared object. The I<path>
446 may be fully qualified or relative. If not fully qualified it is
447 relative to the F<@prefix@/libexec> directory. Any additional
448 parameters after the I<path> are ignored. Lines that don't begin
449 with C<Plugin> or C<Path> are silently ignored
451 For more information, see the L<sudo_plugin(8)> manual.
455 A C<Path> line consists of the C<Path> keyword, followed by the
456 name of the path to set and its value. E.g.
458 Path noexec @noexec_file@
459 Path askpass /usr/X11R6/bin/ssh-askpass
461 The following plugin-agnostic paths may be set in the
462 F<@sysconfdir@/sudo.conf> file.
468 The fully qualified path to a helper program used to read the user's
469 password when no terminal is available. This may be the case when
470 B<sudo> is executed from a graphical (as opposed to text-based)
471 application. The program specified by I<askpass> should display
472 the argument passed to it as the prompt and write the user's password
473 to the standard output. The value of I<askpass> may be overridden
474 by the C<SUDO_ASKPASS> environment variable.
478 The fully-qualified path to a shared library containing dummy
479 versions of the execv(), execve() and fexecve() library functions
480 that just return an error. This is used to implement the I<noexec>
481 functionality on systems that support C<LD_PRELOAD> or its equivalent.
482 Defaults to F<@noexec_file@>.
488 Upon successful execution of a program, the exit status from B<sudo>
489 will simply be the exit status of the program that was executed.
491 Otherwise, B<sudo> exits with a value of 1 if there is a
492 configuration/permission problem or if B<sudo> cannot execute the
493 given command. In the latter case the error string is printed to
494 the standard error. If B<sudo> cannot L<stat(2)> one or more entries
495 in the user's C<PATH>, an error is printed on stderr. (If the
496 directory does not exist or if it is not really a directory, the
497 entry is ignored and no error is printed.) This should not happen
498 under normal circumstances. The most common reason for L<stat(2)>
499 to return "permission denied" is if you are running an automounter
500 and one of the directories in your C<PATH> is on a machine that is
501 currently unreachable.
503 =head1 SECURITY NOTES
505 B<sudo> tries to be safe when executing external commands.
507 To prevent command spoofing, B<sudo> checks "." and "" (both denoting
508 current directory) last when searching for a command in the user's
509 PATH (if one or both are in the PATH). Note, however, that the
510 actual C<PATH> environment variable is I<not> modified and is passed
511 unchanged to the program that B<sudo> executes.
513 Please note that B<sudo> will normally only log the command it
514 explicitly runs. If a user runs a command such as C<sudo su> or
515 C<sudo sh>, subsequent commands run from that shell are not subject
516 to B<sudo>'s security policy. The same is true for commands that
517 offer shell escapes (including most editors). If I/O logging is
518 enabled, subsequent commands will have their input and/or output
519 logged, but there will not be traditional logs for those commands.
520 Because of this, care must be taken when giving users access to
521 commands via B<sudo> to verify that the command does not inadvertently
522 give the user an effective root shell. For more information, please
523 see the C<PREVENTING SHELL ESCAPES> section in L<sudoers(5)>.
527 B<sudo> utilizes the following environment variables. The security
528 policy has control over the content of the command's environment.
534 Default editor to use in B<-e> (sudoedit) mode if neither C<SUDO_EDITOR>
539 In B<-i> mode or when I<env_reset> is enabled in I<sudoers>, set
540 to the mail spool of the target user
544 Set to the home directory of the target user if B<-i> or B<-H> are
545 specified, I<env_reset> or I<always_set_home> are set in I<sudoers>,
546 or when the B<-s> option is specified and I<set_home> is set in
551 May be overridden by the security policy.
555 Used to determine shell to run with C<-s> option
557 =item C<SUDO_ASKPASS>
559 Specifies the path to a helper program used to read the password
560 if no terminal is available or if the C<-A> option is specified.
562 =item C<SUDO_COMMAND>
564 Set to the command run by sudo
568 Default editor to use in B<-e> (sudoedit) mode
572 Set to the group ID of the user who invoked sudo
576 Used as the default password prompt
580 If set, C<PS1> will be set to its value for the program being run
584 Set to the user ID of the user who invoked sudo
588 Set to the login of the user who invoked sudo
592 Set to the target user (root unless the B<-u> option is specified)
596 Default editor to use in B<-e> (sudoedit) mode if C<SUDO_EDITOR>
605 =item F<@sysconfdir@/sudo.conf>
607 B<sudo> plugin and path configuration
613 Note: the following examples assume a properly configured security policy.
615 To get a file listing of an unreadable directory:
617 $ sudo ls /usr/local/protected
619 To list the home directory of user yaz on a machine where the
620 file system holding ~yaz is not exported as root:
622 $ sudo -u yaz ls ~yaz
624 To edit the F<index.html> file as user www:
626 $ sudo -u www vi ~www/htdocs/index.html
628 To view system logs only accessible to root and users in the adm group:
630 $ sudo -g adm view /var/log/syslog
632 To run an editor as jim with a different primary group:
634 $ sudo -u jim -g audio vi ~jim/sound.txt
636 To shutdown a machine:
638 $ sudo shutdown -r +15 "quick reboot"
640 To make a usage listing of the directories in the /home
641 partition. Note that this runs the commands in a sub-shell
642 to make the C<cd> and file redirection work.
644 $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"
648 L<grep(1)>, L<su(1)>, L<stat(2)>,
650 L<passwd(5)>, L<sudoers(5)>, L<sudo_plugin(8)>, L<sudoreplay(8)>, L<visudo(8)>
654 Many people have worked on B<sudo> over the years; this
655 version consists of code written primarily by:
659 See the HISTORY file in the B<sudo> distribution or visit
660 http://www.sudo.ws/sudo/history.html for a short history
665 There is no easy way to prevent a user from gaining a root shell
666 if that user is allowed to run arbitrary commands via B<sudo>.
667 Also, many programs (such as editors) allow the user to run commands
668 via shell escapes, thus avoiding B<sudo>'s checks. However, on
669 most systems it is possible to prevent shell escapes with the
670 L<sudoers(5)> module's I<noexec> functionality.
672 It is not meaningful to run the C<cd> command directly via sudo, e.g.,
674 $ sudo cd /usr/local/protected
676 since when the command exits the parent process (your shell) will
677 still be the same. Please see the EXAMPLES section for more information.
679 Running shell scripts via B<sudo> can expose the same kernel bugs that
680 make setuid shell scripts unsafe on some operating systems (if your OS
681 has a /dev/fd/ directory, setuid shell scripts are generally safe).
685 If you feel you have found a bug in B<sudo>, please submit a bug report
686 at http://www.sudo.ws/sudo/bugs/
690 Limited free support is available via the sudo-users mailing list,
691 see http://www.sudo.ws/mailman/listinfo/sudo-users to subscribe or
696 B<sudo> is provided ``AS IS'' and any express or implied warranties,
697 including, but not limited to, the implied warranties of merchantability
698 and fitness for a particular purpose are disclaimed. See the LICENSE
699 file distributed with B<sudo> or http://www.sudo.ws/sudo/license.html
700 for complete details.