1SUDO(8) BSD System Manager's Manual SUDO(8)
2
4 sudo, sudoedit — execute a command as another user
5
7 sudo -h | -K | -k | -V
8 sudo -v [-AknS] [-g group name | #gid] [-p prompt] [-u user name | #uid]
9 sudo -l[l] [-AknS] [-g group name | #gid] [-p prompt] [-U user name]
10 [-u user name | #uid] [command]
11 sudo [-AbEHnPS] [-C fd] [-g group name | #gid] [-p prompt] [-r role]
12 [-t type] [-u user name | #uid] [VAR=value] -i | -s [command]
13 sudoedit [-AnS] [-C fd] [-g group name | #gid] [-p prompt]
14 [-u user name | #uid] file ...
15
17 sudo allows a permitted user to execute a command as the superuser or
18 another user, as specified by the security policy.
19
20 sudo supports a plugin architecture for security policies and input/out‐
21 put logging. Third parties can develop and distribute their own policy
22 and I/O logging plugins to work seamlessly with the sudo front end. The
23 default security policy is sudoers, which is configured via the file
24 /etc/sudoers, or via LDAP. See the PLUGINS section for more information.
25
26 The security policy determines what privileges, if any, a user has to run
27 sudo. The policy may require that users authenticate themselves with a
28 password or another authentication mechanism. If authentication is
29 required, sudo will exit if the user's password is not entered within a
30 configurable time limit. This limit is policy-specific; the default
31 password prompt timeout for the sudoers security policy is 5 minutes.
32
33 Security policies may support credential caching to allow the user to run
34 sudo again for a period of time without requiring authentication. The
35 sudoers policy caches credentials for 5 minutes, unless overridden in
36 sudoers(5). By running sudo with the -v option, a user can update the
37 cached credentials without running a command.
38
39 When invoked as sudoedit, the -e option (described below), is implied.
40
41 Security policies may log successful and failed attempts to use sudo. If
42 an I/O plugin is configured, the running command's input and output may
43 be logged as well.
44
45 The options are as follows:
46
47 -A Normally, if sudo requires a password, it will read it from
48 the user's terminal. If the -A (askpass) option is speci‐
49 fied, a (possibly graphical) helper program is executed to
50 read the user's password and output the password to the stan‐
51 dard output. If the SUDO_ASKPASS environment variable is
52 set, it specifies the path to the helper program. Otherwise,
53 if /etc/sudo.conf contains a line specifying the askpass pro‐
54 gram, that value will be used. For example:
55
56 # Path to askpass helper program
57 Path askpass /usr/X11R6/bin/ssh-askpass
58
59 If no askpass program is available, sudo will exit with an
60 error.
61
62 -b The -b (background) option tells sudo to run the given com‐
63 mand in the background. Note that if you use the -b option
64 you cannot use shell job control to manipulate the process.
65 Most interactive commands will fail to work properly in back‐
66 ground mode.
67
68 -C fd Normally, sudo will close all open file descriptors other
69 than standard input, standard output and standard error. The
70 -C (close from) option allows the user to specify a starting
71 point above the standard error (file descriptor three). Val‐
72 ues less than three are not permitted. The security policy
73 may restrict the user's ability to use the -C option. The
74 sudoers policy only permits use of the -C option when the
75 administrator has enabled the closefrom_override option.
76
77 -E The -E (preserve environment) option indicates to the secu‐
78 rity policy that the user wishes to preserve their existing
79 environment variables. The security policy may return an
80 error if the -E option is specified and the user does not
81 have permission to preserve the environment.
82
83 -e The -e (edit) option indicates that, instead of running a
84 command, the user wishes to edit one or more files. In lieu
85 of a command, the string "sudoedit" is used when consulting
86 the security policy. If the user is authorized by the pol‐
87 icy, the following steps are taken:
88
89 1. Temporary copies are made of the files to be edited with
90 the owner set to the invoking user.
91
92 2. The editor specified by the policy is run to edit the
93 temporary files. The sudoers policy uses the
94 SUDO_EDITOR, VISUAL and EDITOR environment variables (in
95 that order). If none of SUDO_EDITOR, VISUAL or EDITOR
96 are set, the first program listed in the editor
97 sudoers(5) option is used.
98
99 3. If they have been modified, the temporary files are
100 copied back to their original location and the temporary
101 versions are removed.
102
103 If the specified file does not exist, it will be created.
104 Note that unlike most commands run by sudo, the editor is run
105 with the invoking user's environment unmodified. If, for
106 some reason, sudo is unable to update a file with its edited
107 version, the user will receive a warning and the edited copy
108 will remain in a temporary file.
109
110 -g group Normally, sudo runs a command with the primary group set to
111 the one specified by the password database for the user the
112 command is being run as (by default, root). The -g (group)
113 option causes sudo to run the command with the primary group
114 set to group instead. To specify a gid instead of a group
115 name, use #gid. When running commands as a gid, many shells
116 require that the ‘#’ be escaped with a backslash (‘\’). If
117 no -u option is specified, the command will be run as the
118 invoking user (not root). In either case, the primary group
119 will be set to group.
120
121 -H The -H (HOME) option requests that the security policy set
122 the HOME environment variable to the home directory of the
123 target user (root by default) as specified by the password
124 database. Depending on the policy, this may be the default
125 behavior.
126
127 -h The -h (help) option causes sudo to print a short help mes‐
128 sage to the standard output and exit.
129
130 -i [command]
131 The -i (simulate initial login) option runs the shell speci‐
132 fied by the password database entry of the target user as a
133 login shell. This means that login-specific resource files
134 such as .profile or .login will be read by the shell. If a
135 command is specified, it is passed to the shell for execution
136 via the shell's -c option. If no command is specified, an
137 interactive shell is executed. sudo attempts to change to
138 that user's home directory before running the shell. The
139 security policy shall initialize the environment to a minimal
140 set of variables, similar to what is present when a user logs
141 in. The Command Environment section in the sudoers(5) manual
142 documents how the -i option affects the environment in which
143 a command is run when the sudoers policy is in use.
144
145 -K The -K (sure kill) option is like -k except that it removes
146 the user's cached credentials entirely and may not be used in
147 conjunction with a command or other option. This option does
148 not require a password. Not all security policies support
149 credential caching.
150
151 -k [command]
152 When used alone, the -k (kill) option to sudo invalidates the
153 user's cached credentials. The next time sudo is run a pass‐
154 word will be required. This option does not require a pass‐
155 word and was added to allow a user to revoke sudo permissions
156 from a .logout file. Not all security policies support cre‐
157 dential caching.
158
159 When used in conjunction with a command or an option that may
160 require a password, the -k option will cause sudo to ignore
161 the user's cached credentials. As a result, sudo will prompt
162 for a password (if one is required by the security policy)
163 and will not update the user's cached credentials.
164
165 -l[l] [command]
166 If no command is specified, the -l (list) option will list
167 the allowed (and forbidden) commands for the invoking user
168 (or the user specified by the -U option) on the current host.
169 If a command is specified and is permitted by the security
170 policy, the fully-qualified path to the command is displayed
171 along with any command line arguments. If command is speci‐
172 fied but not allowed, sudo will exit with a status value of
173 1. If the -l option is specified with an l argument (i.e.
174 -ll), or if -l is specified multiple times, a longer list
175 format is used.
176
177 -n The -n (non-interactive) option prevents sudo from prompting
178 the user for a password. If a password is required for the
179 command to run, sudo will display an error message and exit.
180
181 -P The -P (preserve group vector) option causes sudo to preserve
182 the invoking user's group vector unaltered. By default, the
183 sudoers policy will initialize the group vector to the list
184 of groups the target user is in. The real and effective
185 group IDs, however, are still set to match the target user.
186
187 -p prompt The -p (prompt) option allows you to override the default
188 password prompt and use a custom one. The following percent
189 (‘%’) escapes are supported by the sudoers policy:
190
191 %H expanded to the host name including the domain name (on
192 if the machine's host name is fully qualified or the fqdn
193 option is set in sudoers(5))
194
195 %h expanded to the local host name without the domain name
196
197 %p expanded to the name of the user whose password is being
198 requested (respects the rootpw, targetpw, and runaspw
199 flags in sudoers(5))
200
201 %U expanded to the login name of the user the command will
202 be run as (defaults to root unless the -u option is also
203 specified)
204
205 %u expanded to the invoking user's login name
206
207 %% two consecutive ‘%’ characters are collapsed into a sin‐
208 gle ‘%’ character
209
210 The prompt specified by the -p option will override the sys‐
211 tem password prompt on systems that support PAM unless the
212 passprompt_override flag is disabled in sudoers.
213
214 -r role The -r (role) option causes the new (SELinux) security con‐
215 text to have the role specified by role.
216
217 -S The -S (stdin) option causes sudo to read the password from
218 the standard input instead of the terminal device. The pass‐
219 word must be followed by a newline character.
220
221 -s [command]
222 The -s (shell) option runs the shell specified by the SHELL
223 environment variable if it is set or the shell as specified
224 in the password database. If a command is specified, it is
225 passed to the shell for execution via the shell's -c option.
226 If no command is specified, an interactive shell is executed.
227
228 -t type The -t (type) option causes the new (SELinux) security con‐
229 text to have the type specified by type. If no type is spec‐
230 ified, the default type is derived from the specified role.
231
232 -U user The -U (other user) option is used in conjunction with the -l
233 option to specify the user whose privileges should be listed.
234 The security policy may restrict listing other users' privi‐
235 leges. The sudoers policy only allows root or a user with
236 the ALL privilege on the current host to use this option.
237
238 -u user The -u (user) option causes sudo to run the specified command
239 as a user other than root. To specify a uid instead of a
240 user name, #uid. When running commands as a uid, many shells
241 require that the ‘#’ be escaped with a backslash (‘\’).
242 Security policies may restrict uids to those listed in the
243 password database. The sudoers policy allows uids that are
244 not in the password database as long as the targetpw option
245 is not set. Other security policies may not support this.
246
247 -V The -V (version) option causes sudo to print its version
248 string and the version string of the security policy plugin
249 and any I/O plugins. If the invoking user is already root
250 the -V option will display the arguments passed to configure
251 when sudo was built and plugins may display more verbose
252 information such as default options.
253
254 -v When given the -v (validate) option, sudo will update the
255 user's cached credentials, authenticating the user's password
256 if necessary. For the sudoers plugin, this extends the sudo
257 timeout for another 5 minutes (or whatever the timeout is set
258 to by the security policy) but does not run a command. Not
259 all security policies support cached credentials.
260
261 -- The -- option indicates that sudo should stop processing com‐
262 mand line arguments.
263
264 Environment variables to be set for the command may also be passed on the
265 command line in the form of VAR=value, e.g.
266 LD_LIBRARY_PATH=/usr/local/pkg/lib. Variables passed on the command line
267 are subject to the same restrictions as normal environment variables with
268 one important exception. If the setenv option is set in sudoers, the
269 command to be run has the SETENV tag set or the command matched is ALL,
270 the user may set variables that would otherwise be forbidden. See
271 sudoers(5) for more information.
272
274 When sudo executes a command, the security policy specifies the execution
275 envionment for the command. Typically, the real and effective uid and
276 gid are set to match those of the target user, as specified in the pass‐
277 word database, and the group vector is initialized based on the group
278 database (unless the -P option was specified).
279
280 The following parameters may be specified by security policy:
281
282 · real and effective user ID
283
284 · real and effective group ID
285
286 · supplementary group IDs
287
288 · the environment list
289
290 · current working directory
291
292 · file creation mode mask (umask)
293
294 · SELinux role and type
295
296 · scheduling priority (aka nice value)
297
298 Process model
299 When sudo runs a command, it calls fork(2), sets up the execution envi‐
300 ronment as described above, and calls the execve system call in the child
301 process. The main sudo process waits until the command has completed,
302 then passes the command's exit status to the security policy's close
303 method and exits. If an I/O logging plugin is configured, a new pseudo-
304 terminal (“pty”) is created and a second sudo process is used to relay
305 job control signals between the user's existing pty and the new pty the
306 command is being run in. This extra process makes it possible to, for
307 example, suspend and resume the command. Without it, the command would
308 be in what POSIX terms an “orphaned process group” and it would not
309 receive any job control signals.
310
311 Signal handling
312 Because the command is run as a child of the sudo process, sudo will
313 relay signals it receives to the command. Unless the command is being
314 run in a new pty, the SIGHUP, SIGINT and SIGQUIT signals are not relayed
315 unless they are sent by a user process, not the kernel. Otherwise, the
316 command would receive SIGINT twice every time the user entered control-C.
317 Some signals, such as SIGSTOP and SIGKILL, cannot be caught and thus will
318 not be relayed to the command. As a general rule, SIGTSTP should be used
319 instead of SIGSTOP when you wish to suspend a command being run by sudo.
320
321 As a special case, sudo will not relay signals that were sent by the com‐
322 mand it is running. This prevents the command from accidentally killing
323 itself. On some systems, the reboot(8) command sends SIGTERM to all non-
324 system processes other than itself before rebooting the systyem. This
325 prevents sudo from relaying the SIGTERM signal it received back to
326 reboot(8), which might then exit before the system was actually rebooted,
327 leaving it in a half-dead state similar to single user mode. Note, how‐
328 ever, that this check only applies to the command run by sudo and not any
329 other processes that the command may create. As a result, running a
330 script that calls reboot(8) or shutdown(8) via sudo may cause the system
331 to end up in this undefined state unless the reboot(8) or shutdown(8) are
332 run using the exec() family of functions instead of system() (which
333 interposes a shell between the command and the calling process).
334
336 Plugins are dynamically loaded based on the contents of the
337 /etc/sudo.conf file. If no /etc/sudo.conf file is present, or it con‐
338 tains no Plugin lines, sudo will use the traditional sudoers security
339 policy and I/O logging, which corresponds to the following /etc/sudo.conf
340 file.
341
342 #
343 # Default /etc/sudo.conf file
344 #
345 # Format:
346 # Plugin plugin_name plugin_path plugin_options ...
347 # Path askpass /path/to/askpass
348 # Path noexec /path/to/sudo_noexec.so
349 # Debug sudo /var/log/sudo_debug all@warn
350 # Set disable_coredump true
351 #
352 # The plugin_path is relative to /usr/libexec unless
353 # fully qualified.
354 # The plugin_name corresponds to a global symbol in the plugin
355 # that contains the plugin interface structure.
356 # The plugin_options are optional.
357 #
358 Plugin policy_plugin sudoers.so
359 Plugin io_plugin sudoers.so
360
361 A Plugin line consists of the Plugin keyword, followed by the symbol_name
362 and the path to the shared object containing the plugin. The symbol_name
363 is the name of the struct policy_plugin or struct io_plugin in the plugin
364 shared object. The path may be fully qualified or relative. If not
365 fully qualified it is relative to the /usr/libexec directory. Any addi‐
366 tional parameters after the path are passed as arguments to the plugin's
367 open function. Lines that don't begin with Plugin, Path, Debug, or Set
368 are silently ignored.
369
370 For more information, see the sudo_plugin(8) manual.
371
373 A Path line consists of the Path keyword, followed by the name of the
374 path to set and its value. E.g.
375
376 Path noexec /usr/libexec/sudo_noexec.so
377 Path askpass /usr/X11R6/bin/ssh-askpass
378
379 The following plugin-agnostic paths may be set in the /etc/sudo.conf
380 file:
381
382 askpass The fully qualified path to a helper program used to read the
383 user's password when no terminal is available. This may be the
384 case when sudo is executed from a graphical (as opposed to
385 text-based) application. The program specified by askpass
386 should display the argument passed to it as the prompt and
387 write the user's password to the standard output. The value of
388 askpass may be overridden by the SUDO_ASKPASS environment vari‐
389 able.
390
391 noexec The fully-qualified path to a shared library containing dummy
392 versions of the execv(), execve() and fexecve() library func‐
393 tions that just return an error. This is used to implement the
394 noexec functionality on systems that support LD_PRELOAD or its
395 equivalent. Defaults to /usr/libexec/sudo_noexec.so.
396
398 sudo versions 1.8.4 and higher support a flexible debugging framework
399 that can help track down what sudo is doing internally if there is a
400 problem.
401
402 A Debug line consists of the Debug keyword, followed by the name of the
403 program to debug (sudo, visudo, sudoreplay), the debug file name and a
404 comma-separated list of debug flags. The debug flag syntax used by sudo
405 and the sudoers plugin is subsystem@priority but the plugin is free to
406 use a different format so long as it does not include a comma (‘,’).
407
408 For instance:
409
410 Debug sudo /var/log/sudo_debug all@warn,plugin@info
411
412 would log all debugging statements at the warn level and higher in addi‐
413 tion to those at the info level for the plugin subsystem.
414
415 Currently, only one Debug entry per program is supported. The sudo Debug
416 entry is shared by the sudo front end, sudoedit and the plugins. A
417 future release may add support for per-plugin Debug lines and/or support
418 for multiple debugging files for a single program.
419
420 The priorities used by the sudo front end, in order of decreasing sever‐
421 ity, are: crit, err, warn, notice, diag, info, trace and debug. Each
422 priority, when specified, also includes all priorities higher than it.
423 For example, a priority of notice would include debug messages logged at
424 notice and higher.
425
426 The following subsystems are used by the sudo front-end:
427
428 all matches every subsystem
429
430 args command line argument processing
431
432 conv user conversation
433
434 edit sudoedit
435
436 exec command execution
437
438 main sudo main function
439
440 netif network interface handling
441
442 pcomm communication with the plugin
443
444 plugin plugin configuration
445
446 pty pseudo-tty related code
447
448 selinux SELinux-specific handling
449
450 util utility functions
451
452 utmp utmp handling
453
455 Upon successful execution of a program, the exit status from sudo will
456 simply be the exit status of the program that was executed.
457
458 Otherwise, sudo exits with a value of 1 if there is a configuration/per‐
459 mission problem or if sudo cannot execute the given command. In the lat‐
460 ter case the error string is printed to the standard error. If sudo can‐
461 not stat(2) one or more entries in the user's PATH, an error is printed
462 on stderr. (If the directory does not exist or if it is not really a
463 directory, the entry is ignored and no error is printed.) This should
464 not happen under normal circumstances. The most common reason for
465 stat(2) to return “permission denied” is if you are running an auto‐
466 mounter and one of the directories in your PATH is on a machine that is
467 currently unreachable.
468
470 sudo tries to be safe when executing external commands.
471
472 To prevent command spoofing, sudo checks "." and "" (both denoting cur‐
473 rent directory) last when searching for a command in the user's PATH (if
474 one or both are in the PATH). Note, however, that the actual PATH envi‐
475 ronment variable is not modified and is passed unchanged to the program
476 that sudo executes.
477
478 Please note that sudo will normally only log the command it explicitly
479 runs. If a user runs a command such as sudo su or sudo sh, subsequent
480 commands run from that shell are not subject to sudo's security policy.
481 The same is true for commands that offer shell escapes (including most
482 editors). If I/O logging is enabled, subsequent commands will have their
483 input and/or output logged, but there will not be traditional logs for
484 those commands. Because of this, care must be taken when giving users
485 access to commands via sudo to verify that the command does not inadver‐
486 tently give the user an effective root shell. For more information,
487 please see the PREVENTING SHELL ESCAPES section in sudoers(5).
488
489 To prevent the disclosure of potentially sensitive information, sudo dis‐
490 ables core dumps by default while it is executing (they are re-enabled
491 for the command that is run). To aid in debugging sudo crashes, you may
492 wish to re-enable core dumps by setting “disable_coredump” to false in
493 the /etc/sudo.conf file as follows:
494
495 Set disable_coredump false
496
497 Note that by default, most operating systems disable core dumps from
498 setuid programs, which includes sudo. To actually get a sudo core file
499 you may need to enable core dumps for setuid processes. On BSD and Linux
500 systems this is accomplished via the sysctl command, on Solaris the core‐
501 adm command can be used.
502
504 sudo utilizes the following environment variables. The security policy
505 has control over the actual content of the command's environment.
506
507 EDITOR Default editor to use in -e (sudoedit) mode if neither
508 SUDO_EDITOR nor VISUAL is set.
509
510 MAIL In -i mode or when env_reset is enabled in sudoers, set
511 to the mail spool of the target user.
512
513 HOME Set to the home directory of the target user if -i or -H
514 are specified, env_reset or always_set_home are set in
515 sudoers, or when the -s option is specified and set_home
516 is set in sudoers.
517
518 PATH May be overridden by the security policy.
519
520 SHELL Used to determine shell to run with -s option.
521
522 SUDO_ASKPASS Specifies the path to a helper program used to read the
523 password if no terminal is available or if the -A option
524 is specified.
525
526 SUDO_COMMAND Set to the command run by sudo.
527
528 SUDO_EDITOR Default editor to use in -e (sudoedit) mode.
529
530 SUDO_GID Set to the group ID of the user who invoked sudo.
531
532 SUDO_PROMPT Used as the default password prompt.
533
534 SUDO_PS1 If set, PS1 will be set to its value for the program
535 being run.
536
537 SUDO_UID Set to the user ID of the user who invoked sudo.
538
539 SUDO_USER Set to the login name of the user who invoked sudo.
540
541 USER Set to the target user (root unless the -u option is
542 specified).
543
544 VISUAL Default editor to use in -e (sudoedit) mode if
545 SUDO_EDITOR is not set.
546
548 /etc/sudo.conf sudo front end configuration
549
551 Note: the following examples assume a properly configured security pol‐
552 icy.
553
554 To get a file listing of an unreadable directory:
555
556 $ sudo ls /usr/local/protected
557
558 To list the home directory of user yaz on a machine where the file system
559 holding ~yaz is not exported as root:
560
561 $ sudo -u yaz ls ~yaz
562
563 To edit the index.html file as user www:
564
565 $ sudo -u www vi ~www/htdocs/index.html
566
567 To view system logs only accessible to root and users in the adm group:
568
569 $ sudo -g adm view /var/log/syslog
570
571 To run an editor as jim with a different primary group:
572
573 $ sudo -u jim -g audio vi ~jim/sound.txt
574
575 To shut down a machine:
576
577 $ sudo shutdown -r +15 "quick reboot"
578
579 To make a usage listing of the directories in the /home partition. Note
580 that this runs the commands in a sub-shell to make the cd and file redi‐
581 rection work.
582
583 $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"
584
586 grep(1), su(1), stat(2), passwd(5), sudoers(5), sudo_plugin(8),
587 sudoreplay(8), visudo(8)
588
590 See the HISTORY file in the sudo distribution
591 (http://www.sudo.ws/sudo/history.html) for a brief history of sudo.
592
594 Many people have worked on sudo over the years; this version consists of
595 code written primarily by:
596
597 Todd C. Miller
598
599 See the CONTRIBUTORS file in the sudo distribution
600 (http://www.sudo.ws/sudo/contributors.html) for an exhaustive list of
601 people who have contributed to sudo.
602
604 There is no easy way to prevent a user from gaining a root shell if that
605 user is allowed to run arbitrary commands via sudo. Also, many programs
606 (such as editors) allow the user to run commands via shell escapes, thus
607 avoiding sudo's checks. However, on most systems it is possible to pre‐
608 vent shell escapes with the sudoers(5) plugin's noexec functionality.
609
610 It is not meaningful to run the cd command directly via sudo, e.g.,
611
612 $ sudo cd /usr/local/protected
613
614 since when the command exits the parent process (your shell) will still
615 be the same. Please see the EXAMPLES section for more information.
616
617 Running shell scripts via sudo can expose the same kernel bugs that make
618 setuid shell scripts unsafe on some operating systems (if your OS has a
619 /dev/fd/ directory, setuid shell scripts are generally safe).
620
622 If you feel you have found a bug in sudo, please submit a bug report at
623 http://www.sudo.ws/sudo/bugs/
624
626 Limited free support is available via the sudo-users mailing list, see
627 http://www.sudo.ws/mailman/listinfo/sudo-users to subscribe or search the
628 archives.
629
631 sudo is provided “AS IS” and any express or implied warranties, includ‐
632 ing, but not limited to, the implied warranties of merchantability and
633 fitness for a particular purpose are disclaimed. See the LICENSE file
634 distributed with sudo or http://www.sudo.ws/sudo/license.html for com‐
635 plete details.
636
637Sudo 1.8.6p3 July 10, 2012 Sudo 1.8.6p3