summaryrefslogtreecommitdiffstats
path: root/slackbook/html/filesystem-structure-permissions.html
blob: 0f951e77711cbc4197e049dd5e989cd8369a5566 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<title>Permissions</title>
<meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.7" />
<link rel="HOME" title="Slackware Linux Essentials" href="index.html" />
<link rel="UP" title="Filesystem Structure" href="filesystem-structure.html" />
<link rel="PREVIOUS" title="Filesystem Structure" href="filesystem-structure.html" />
<link rel="NEXT" title="Links" href="filesystem-structure-links.html" />
<link rel="STYLESHEET" type="text/css" href="docbook.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body class="SECT1" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#840084"
alink="#0000FF">
<div class="NAVHEADER">
<table summary="Header navigation table" width="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<th colspan="3" align="center">Slackware Linux Essentials</th>
</tr>

<tr>
<td width="10%" align="left" valign="bottom"><a href="filesystem-structure.html"
accesskey="P">Prev</a></td>
<td width="80%" align="center" valign="bottom">Chapter 9 Filesystem Structure</td>
<td width="10%" align="right" valign="bottom"><a href="filesystem-structure-links.html"
accesskey="N">Next</a></td>
</tr>
</table>

<hr align="LEFT" width="100%" />
</div>

<div class="SECT1">
<h1 class="SECT1"><a id="FILESYSTEM-STRUCTURE-PERMISSIONS"
name="FILESYSTEM-STRUCTURE-PERMISSIONS">9.2 Permissions</a></h1>

<p>Permissions are the other important part of the multiuser aspects of the filesystem.
With these, you can change who can read, write, and execute files.</p>

<p>The permission information is stored as four octal digits, each specifying a different
set of permissions. There are owner permissions, group permissions, and world
permissions. The fourth octal digit is used to store special information such as set user
ID, set group ID, and the sticky bit. The octal values assigned to the permission modes
are (they also have letters associated with them that are displayed by programs such as
<tt class="COMMAND">ls</tt> and can be used by <tt class="COMMAND">chmod</tt>):</p>

<div class="TABLE"><a id="AEN3142" name="AEN3142"></a>
<p><b>Table 9-1. Octal Permission Values</b></p>

<table border="0" frame="void" class="CALSTABLE">
<col width="3*" />
<col width="1*" align="CENTER" />
<col width="1*" align="CENTER" />
<thead>
<tr>
<th>Permission Type</th>
<th>Octal Value</th>
<th>Letter Value</th>
</tr>
</thead>

<tbody>
<tr>
<td>&#8220;sticky&#8221; bit</td>
<td>1</td>
<td>t</td>
</tr>

<tr>
<td>set user ID</td>
<td>4</td>
<td>s</td>
</tr>

<tr>
<td>set group ID</td>
<td>2</td>
<td>s</td>
</tr>

<tr>
<td>read</td>
<td>4</td>
<td>r</td>
</tr>

<tr>
<td>write</td>
<td>2</td>
<td>w</td>
</tr>

<tr>
<td>execute</td>
<td>1</td>
<td>x</td>
</tr>
</tbody>
</table>
</div>

<p>You add the octal values for each permission group. For example, if you want the group
permissions to be &#8220;read&#8221; and &#8220;write&#8221;, you would use
&#8220;6&#8221; in the group portion of the permission information.</p>

<p><tt class="COMMAND">bash</tt>'s default permissions are:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls -l /bin/bash</kbd>
-rwxr-xr-x   1 root     bin  477692 Mar 21 19:57 /bin/bash
</pre>
</td>
</tr>
</table>

<p>The first dash would be replaced with a &#8220;d&#8221; if this was a directory. The
three permission groups (owner, group, and world) are displayed next. We see that the
owner has read, write, and execute permissions (<var class="LITERAL">rwx</var>). The
group has only read and execute (<var class="LITERAL">r-x</var>). And everyone else has
only read and execute (<var class="LITERAL">r-x</var>).</p>

<p>How would we set permissions on another file to resemble <tt
class="COMMAND">bash</tt>'s? First, let's make an example file:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">touch /tmp/example</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls -l /tmp/example</kbd>
-rw-rw-r---  1 david    users    0 Apr 19 11:21 /tmp/example
</pre>
</td>
</tr>
</table>

<p>We will use <tt class="COMMAND">chmod</tt>(1) (which means &#8220;change mode&#8221;)
to set the permissions on the example file. Add the octal numbers for the permissions you
want. For the owner to have read, write, and execute, we would have a value of <var
class="LITERAL">7</var>. Read and execute would have <var class="LITERAL">5</var>. Run
those together and pass them to <tt class="COMMAND">chmod</tt> like this:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">chmod 755 /tmp/example</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls -l /tmp/example</kbd>
-rwxr-xr-x   1 david    users    0 Apr 19 11:21 /tmp/example
</pre>
</td>
</tr>
</table>

<p>Now you may be thinking, &#8220;Why didn't it just create a file with those
permissions in the first place?&#8221; Well the answer is simple. <tt
class="COMMAND">bash</tt> includes a nice little built-in called <tt
class="COMMAND">umask</tt>. This is included with most Unix shells as well, and controls
what file permissions are assigned to newly created files. We discussed <tt
class="COMMAND">bash</tt> built-ins to some degree in <a
href="shell-bash.html#SHELL-BASH-ENVIRONMENT">Section 8.3.1</a>. <tt
class="COMMAND">umask</tt> takes a little getting used to. It works very similar to <tt
class="COMMAND">chmod</tt>, only in reverse. You specify the octal values you do not wish
to have present in newly created files. The default umask value is <var
class="LITERAL">0022</var>.</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">umask</kbd>
0022
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">umask 0077</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">touch tempfile</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls -l tempfile</kbd>
-rw--------  1 david    users    0 Apr 19 11:21 tempfile
</pre>
</td>
</tr>
</table>

<p>See the man page for <tt class="COMMAND">bash</tt> for more information.</p>

<p>To set special permissions with <tt class="COMMAND">chmod</tt>, add the numbers
together and place them in the first column. For example, to make it set user ID and set
group ID, we use 6 as the first column:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">chmod 6755 /tmp/example</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls -l /tmp/example</kbd>
-rwsr-sr-x   1 david    users    0 Apr 19 11:21 /tmp/example
</pre>
</td>
</tr>
</table>

<p>If the octal values confuse you, you can use letters with <tt
class="COMMAND">chmod</tt>. The permission groups are represented as:</p>

<div class="INFORMALTABLE"><a id="AEN3246" name="AEN3246"></a>
<table border="0" frame="void" class="CALSTABLE">
<col />
<col />
<tbody>
<tr>
<td>Owner</td>
<td>u</td>
</tr>

<tr>
<td>Group</td>
<td>g</td>
</tr>

<tr>
<td>World</td>
<td>o</td>
</tr>

<tr>
<td>All of the above</td>
<td>a</td>
</tr>
</tbody>
</table>
</div>

<p>To do the above, we would have to use several command lines:</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">chmod a+rx /tmp/example</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">chmod u+w /tmp/example</kbd>
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">chmod ug+s /tmp/example</kbd>
</pre>
</td>
</tr>
</table>

<p>Some people prefer the letters over the numbers. Either way will result in the same
set of permissions.</p>

<p>The octal format is often faster, and the one you see most often used in shell
scripts. Sometimes the letters are more powerful however. For example, there's no easy
way to change one group of permissions while preserving the other groups on files and
directories when using the octal format. This is trivial with the letters.</p>

<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="SCREEN">
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls -l /tmp/</kbd>
-rwxr-xr-x   1 alan    users    0 Apr 19 11:21 /tmp/example0
-rwxr-x---   1 alan    users    0 Apr 19 11:21 /tmp/example1
----r-xr-x   1 alan    users    0 Apr 19 11:21 /tmp/example2
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">chmod g-rwx /tmp/example?</kbd>
-rwx---r-x   1 alan    users    0 Apr 19 11:21 /tmp/example0
-rwx------   1 alan    users    0 Apr 19 11:21 /tmp/example1
-------r-x   1 alan    users    0 Apr 19 11:21 /tmp/example2
</pre>
</td>
</tr>
</table>

<p>We mentioned set user ID and set group ID permissions in several places above. You may
be wondering what this is. Normally when you run a program, it is operating under your
user account. That is, it has all the permissions that you as a user have. The same is
true for the group. When you run a program, it executes under your current group. With
set user ID permissions, you can force the program to always run as the program owner
(such as &#8220;root&#8221;). Set group ID is the same, but for the group.</p>

<p>Be careful with this, set user ID and set group ID programs can open major security
holes on your system. If you frequently set user ID programs that are owned by <tt
class="USERNAME">root</tt>, you are allowing anyone to run that program and run it as <tt
class="USERNAME">root</tt>. Since <tt class="USERNAME">root</tt> has no restrictions on
the system, you can see how this would pose a major security problem. In short, it's not
bad to use set user ID and set group ID permissions, just use common sense.</p>
</div>

<div class="NAVFOOTER">
<hr align="LEFT" width="100%" />
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="33%" align="left" valign="top"><a href="filesystem-structure.html"
accesskey="P">Prev</a></td>
<td width="34%" align="center" valign="top"><a href="index.html"
accesskey="H">Home</a></td>
<td width="33%" align="right" valign="top"><a href="filesystem-structure-links.html"
accesskey="N">Next</a></td>
</tr>

<tr>
<td width="33%" align="left" valign="top">Filesystem Structure</td>
<td width="34%" align="center" valign="top"><a href="filesystem-structure.html"
accesskey="U">Up</a></td>
<td width="33%" align="right" valign="top">Links</td>
</tr>
</table>
</div>
</body>
</html>