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