Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/help/en/bash/sudo.html
blob: 58533458535ae325a74405f4986690f5f061f4cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<link rel="STYLESHEET" href="../term_tools/main.css" type="text/css">
<title>sudo MAN Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body><!-- #BeginLibraryItem "/Library/head_bash.lbi" --><div id="nav-menu">
<ul>
<li><a class="rl" href="../term_tools/index.html"><img src="../term_tools/images/ss64.gif" title="Home" alt="Home"></a></li>
<li><a class="rl" href="../term_tools/bash"><img src="../term_tools/images/bash-l.gif" title="bash" alt="bash"></a></li>
<li><!-- Search Google -->
<form action="http://www.google.com/search" method="get" style="margin:0px;padding:0px;">
<div align="right">
<input name="q" type="text" class="body" size="25">
<input class="submit" type="submit" value="Search">
<input type="hidden" name="q" value="site:ss64.com/bash/">
</div>
</form></li>
<li><a class="rr" href="syntax.html"><img src="../term_tools/images/syntax-r.gif" title="Bash Syntax"></a></li>
</ul>
</div> <!-- #EndLibraryItem --><h1> sudo, sudoedit - execute a command as another user </h1> 
<p>sudo allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file.</p>
<pre>Syntax
      sudo -K | -L | -V | -h | -k | -l | -v 
 
      sudo [-HPSb] [-a <i>auth_type</i>] [-c <i>class</i>|-] [-p <i>prompt</i>]
        [-u username|#<i>uid</i>] {-e <i>file</i> [...] | -i | -s | <i>command</i>}

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

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. </pre>
<p><b> Return Values<br>
</b>Upon successful execution of a program, the return value from sudo will simply be the return value of the program that was executed.</p>
<p>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 &quot;permission denied&quot; is if you are running an automounter and one of the directories in your PATH is on a machine that is currently unreachable. </p>
<p>sudo utilizes the following environment variables: EDITOR, HOME, PATH,SHELL,SUDO_PROMPT, SUDO_COMMAND, SUDO_USER, SUDO_UID, SUDO_GID, SUDO_PS1, USER, VISUAL. </p>
<p><b>Files<br>
</b>List of who can run what<span class="telety">: 
/etc/sudoers</span> <br>
Directory containing timestamps:<span class="telety"> /var/run/sudo</span> </p>
<p><b>Examples</b></p>
<p>Note: the following examples assume suitable sudoers(5) entries.</p>
<p>To get a file listing of an unreadable directory:</p>
<p class="code">$ sudo ls /usr/local/protected</p>
<p>To  run the last command as root, useful when you forget to use sudo for a command. <span class="code">!!</span> grabs the last run command.</p>
<p class="code">sudo !!</p>
<p>To list the home directory of user yazza on a machine where the file system holding ~yazza is not exported as root:</p>
<p class="code">$ sudo -u yazza ls ~yazza</p>
<p>To edit the index.html file as user JDoe:</p>
<p class="code">$ sudo -u JDoe vi ~www/htdocs/index.html</p>
<p>To shutdown a machine:</p>
<p class="code">$ sudo shutdown -r +15 &quot;quick reboot&quot;</p>
<p>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.</p>
<p class="code">$ sudo sh -c &quot;cd /home ; du -s * | sort -rn &gt; USAGE&quot;</p>
<p><i>"It was just like Romeo and Juliet, only it ended in tragedy" - Milhouse</i><br>
  <br>
  <b> Related</b>:<br>
  <br>
  <a href="grep.html">grep</a> - Search file(s) for lines that match a given pattern<br>
<a href="su.html">su</a> - Substitute user identity<br>
stat(2)<br>
login_cap(3)<br>
sudoers(5)<br>
<a href="passwd.html">passwd</a> - Modify a user password<br>
visudo(8) <br>
<a href="http://www.sudo.ws/">www.sudo.ws</a> - Todd Miller, sudo maintainer<br>
Equivalent Windows command:  <a href="../term_tools/nt/runas.html">RUNAS</a> /user</p>
<!-- #BeginLibraryItem "/Library/foot_bash.lbi" --><p align="left"><script type="text/javascript"><!--
google_ad_client = "pub-6140977852749469";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
google_ad_channel ="1984328893";
google_color_border = "CCCCCC";
google_color_bg = "CCCCCC";
google_color_link = "000000";
google_color_text = "333333";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br>
</p>
<div align="center"><hr size="1">
  <p id="top"><a href="#"><img src="../term_tools/images/top-4.gif" width="47" height="53" border="0" alt="Back to the Top" title="Back to the Top"></a></p>
  <p class="tagline">&copy; Copyright <a href="http://ss64.com/">SS64.com</a> 1999-2010<br>
Some rights reserved<br>
</p>
</div><!-- #EndLibraryItem --></body>
</html>