forked from SciLifeLab/courses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix5.html
292 lines (278 loc) · 11.8 KB
/
unix5.html
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
<?xml version="1.0" encoding="iso-8859-1"?>
<!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>
<title> UNIX Tutorial Five</title>
<link href="unixtut2.css" rel="stylesheet" type="text/css" />
<link href="unixtut2-print.css" rel="stylesheet" type="text/css" media="print" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Copyright" content="Michael Stonebank, 1995-2003" />
</head>
<body>
<div id="container">
<h2>A copy from <a href="http://www.ee.surrey.ac.uk/Teaching/Unix/"><font color="lightgrey">UNIX Tutorial for Beginners</font></a> with modified paths<br>Please use <a href="http://www.uppnex.se/emu/"><font color="lightgrey">this terminal emulator</font></a> when doing the exercises!</h2>
<h1>UNIX Tutorial Five </h1>
<h2>5.1 File system security (access rights) </h2>
<p>In your <b>unixstuff</b> directory, type </p>
<p class="cli"> % ls -l (l for long listing!)</p>
<p>You will see that you now get lots of details about the contents of your directory,
similar to the example below. </p>
<p> <img src="file1.gif" alt="File and directory access rights" /> </p>
<p>Each file (and directory) has associated access rights, which may be found
by typing <span class="command">ls -l</span>. Also, <span class="command">ls -lg</span> gives additional information
as to which group owns the file (beng95 in the following example): </p>
<p class="output"> -rwxrw-r-- 1 ee51ab beng95
2450 Sept29 11:52 file1</p>
<p>In the left-hand column is a 10 symbol string consisting of the symbols d,
r, w, x, -, and, occasionally, s or S. If d is present, it will be at the left
hand end of the string, and indicates a directory: otherwise - will be the starting
symbol of the string. </p>
<p>The 9 remaining symbols indicate the permissions, or access rights, and are
taken as three groups of 3. </p>
<ul>
<li> The left group of 3 gives the file permissions for the user that owns the
file (or directory) (ee51ab in the above example); <br />
</li>
<li> the middle group gives the permissions for the group of people to whom
the file (or directory) belongs (eebeng95 in the above example);<br />
</li>
<li> the rightmost group gives the permissions for all others.</li>
</ul>
<p>The symbols r, w, etc., have slightly different meanings depending on whether
they refer to a simple file or to a directory. </p>
<h3>Access rights on files.</h3>
<ul>
<li> r (or -), indicates read permission (or otherwise), that is, the presence
or absence of permission to read and copy the file <br />
</li>
<li> w (or -), indicates write permission (or otherwise), that is, the permission
(or otherwise) to change a file <br />
</li>
<li> x (or -), indicates execution permission (or otherwise), that is, the permission
to execute a file, where appropriate </li>
</ul>
<h3>Access rights on directories.</h3>
<ul>
<li> r allows users to list files in the directory; </li>
<li> w means that users may delete files from the directory or move files into
it; </li>
<li> x means the right to access files in the directory. This implies that you
may read files in the directory provided you have read permission on the individual
files. </li>
</ul>
<p>So, in order to read a file, you must have execute permission on the directory
containing that file, and hence on any directory containing that directory as
a subdirectory, and so on, up the tree. </p>
<h3>Some examples</h3>
<table border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#666666">
<tr>
<td>-rwxrwxrwx</td>
<td>a file that everyone can read, write and execute (and delete).</td>
</tr>
<tr>
<td>-rw-------</td>
<td>a file that only the owner can read and write - no-one else <br />
can read or write and no-one has execution rights (e.g. your <br />
mailbox file).</td>
</tr>
</table>
<h2>5.2 Changing access rights</h2>
<h3>chmod (changing a file mode)</h3>
<p>Only the owner of a file can use chmod to change the permissions
of a file. The options of chmod are as follows </p>
<table border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#666666">
<tr>
<th>Symbol</th>
<th>Meaning</th>
</tr>
<tr>
<td><div align="center">u</div></td>
<td>user</td>
</tr>
<tr>
<td><div align="center">g</div></td>
<td>group</td>
</tr>
<tr>
<td><div align="center">o</div></td>
<td>other</td>
</tr>
<tr>
<td><div align="center">a</div></td>
<td>all</td>
</tr>
<tr>
<td><div align="center">r</div></td>
<td>read</td>
</tr>
<tr>
<td><div align="center">w</div></td>
<td>write (and delete)</td>
</tr>
<tr>
<td><div align="center">x</div></td>
<td>execute (and access directory) </td>
</tr>
<tr>
<td><div align="center">+</div></td>
<td>add permission</td>
</tr>
<tr>
<td><div align="center">-</div></td>
<td>take away permission</td>
</tr>
</table>
<p> For example, to remove read write and execute permissions on the file <strong>biglist</strong>
for the group and others, type </p>
<p class="cli"> % chmod go-rwx biglist </p>
<p>This will leave the other permissions unaffected. </p>
<p>To give read and write permissions on the file <strong>biglist</strong> to
all, </p>
<p class="cli"> % chmod a+rw biglist </p>
<h3>Exercise 5a</h3>
<p>Try changing access permissions on the file <strong>science.txt</strong> and
on the directory <strong>backups</strong></p>
<p> Use <span class="command">ls -l</span> to check that the permissions have changed. </p>
<h2>5.3 Processes and Jobs </h2>
<p>A process is an executing program identified by a unique PID (process identifier).
To see information about your processes, with their associated PID and status,
type </p>
<p class="cli"> % ps </p>
<p>A process may be in the foreground, in the background, or be suspended. In
general the shell does not return the UNIX prompt until the current process
has finished executing. </p>
<p> Some processes take a long time to run and hold up the terminal. Backgrounding
a long process has the effect that the UNIX prompt is returned immediately,
and other tasks can be carried out while the original process continues executing.</p>
<h3>Running background processes</h3>
<p>To background a process, type an <strong>&</strong> at the end of the command
line. For example, the command <span class="command">sleep</span> waits a given number of seconds
before continuing. Type </p>
<p class="cli"> % sleep 10 </p>
<p>This will wait 10 seconds before returning the command prompt %. Until the
command prompt is returned, you can do nothing except wait. </p>
<p> To run sleep in the background, type </p>
<p class="cli"> % sleep 10 &</p>
<p class="output">[1] 6259 </p>
<p>The <strong>&</strong> runs the job in the background and returns the prompt
straight away, allowing you do run other programs while waiting for that one
to finish. </p>
<p>The first line in the above example is typed in by the user; the next line,
indicating job number and PID, is returned by the machine. The user is be notified
of a job number (numbered from 1) enclosed in square brackets, together with
a PID and is notified when a background process is finished. Backgrounding is
useful for jobs which will take a long time to complete. </p>
<h3>Backgrounding a current foreground process</h3>
<p>At the prompt, type </p>
<p class="cli"> % sleep 1000 </p>
<p>You can suspend the process running in the foreground by typing <b>^Z</b>, i.e.hold down the [<b>Ctrl</b>]
key and type [<b>z</b>]. Then to put
it in the background, type </p>
<p class="cli"> % bg </p>
<p class="hint">Note: do not background programs that require user interaction
e.g. vi </p>
<h2>5.4 Listing suspended and background processes </h2>
<p>When a process is running, backgrounded or suspended, it will be entered onto
a list along with a job number. To examine this list, type </p>
<p class="cli"> % jobs </p>
<p>An example of a job list could be </p>
<p class="output"> [1] Suspended sleep 1000<br />
[2] Running netscape<br />
[3] Running matlab </p>
<p>To restart (foreground) a suspended processes, type </p>
<p class="cli"> % fg %<i>jobnumber</i> </p>
<p>For example, to restart sleep 1000, type </p>
<p class="cli"> % fg %1 </p>
<p>Typing <span class="command">fg</span> with no job number foregrounds the last suspended process.</p>
<p> </p>
<h2>5.5 Killing a process </h2>
<h3>kill (terminate or signal a process)</h3>
<p>It is sometimes necessary to kill a process (for example, when an executing
program is in an infinite loop) </p>
<p> To kill a job running in the foreground, type <b>^C</b> (control c).
For example, run </p>
<p class="cli">% sleep 100<br />
^C </p>
<p> To kill a suspended or background process, type </p>
<p class="cli"> % kill %<i>jobnumber</i> </p>
<p>For example, run </p>
<p class="cli">% sleep 100 &<br />
% jobs</p>
<p> If it is job number 4, type </p>
<p class="cli"> % kill %4 </p>
<p>To check whether this has worked, examine the job list again to see if the
process has been removed. </p>
<h3>ps (process status)</h3>
<p> Alternatively, processes can be killed by finding their process numbers (PIDs)
and using kill <var>PID_number</var></p>
<p class="cli"> % sleep 1000 &<br />
% ps</p>
<p class="output">PID TT S TIME COMMAND<br />
20077 pts/5 S 0:05 sleep 1000<br />
21563 pts/5 T 0:00 netscape<br />
21873 pts/5 S 0:25 nedit </p>
<p>To kill off the process <b>sleep 1000</b>, type </p>
<p class="cli"> % kill 20077 </p>
<p>and then type <span class="command">ps</span> again to see if it has been removed from the
list. </p>
<p> If a process refuses to be killed, uses the <strong>-9</strong> option, i.e.
type </p>
<p class="cli"> % kill -9 20077 </p>
<p class="hint">Note: It is not possible to kill off other users' processes !!!</p>
<h2>Summary </h2>
<table border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#666666">
<tr>
<th>Command</th>
<th>Meaning</th>
</tr>
<tr>
<td class="command">ls -lag</td>
<td>list access rights for all files</td>
</tr>
<tr>
<td class="command">chmod [<var>options</var>] <var>file</var></td>
<td> change access rights for named file</td>
</tr>
<tr>
<td class="command"><var>command</var> &</td>
<td>run command in background</td>
</tr>
<tr>
<td class="command">^C</td>
<td>kill the job running in the foreground</td>
</tr>
<tr>
<td class="command">^Z</td>
<td>suspend the job running in the foreground</td>
</tr>
<tr>
<td class="command">bg</td>
<td>background the suspended job</td>
</tr>
<tr>
<td class="command">jobs</td>
<td>list current jobs</td>
</tr>
<tr>
<td class="command">fg %1</td>
<td>foreground job number 1</td>
</tr>
<tr>
<td class="command">kill %1</td>
<td> kill job number 1</td>
</tr>
<tr>
<td class="command">ps</td>
<td>list current processes</td>
</tr>
<tr>
<td class="command">kill 26152</td>
<td>kill process number 26152</td>
</tr>
</table>
<p align="center"><a href="unix4.html"><img src="media/left.gif" alt="Previous" width="37" height="39" border="0" /></a><a href="index.html"><img src="media/home.gif" alt="Home" width="81" height="39" border="0" /></a><a href="unix6.html"><img src="media/right.gif" alt="Next" width="37" height="39" border="0" /></a></p>
<p align="center"><iframe src="http://rcm.amazon.com/e/cm?t=unixtutorialf-20&o=1&p=13&l=ur1&category=computers&banner=07FDJ3ARVCE5SRS3G102&f=ifr" width="468" height="60" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe></p>
<p class="date"> M.Stonebank@surrey.ac.uk, 9th October 2000 </p>
</div>
</body>
</html>