Bootcamp: permissions-exercises.txt

File permissions-exercises.txt, 9.2 KB (added by dean, 5 years ago)
Line 
1Linux System Administration and IP Services
2
3Exercises: Permissions
4----------------------
5
6# Notes
7
8* Commands preceded with "$" imply that you should execute the command as
9  a general user - not as root.
10* Commands preceded with "#" imply that you should be working as root with
11  "sudo"
12* Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>")
13  imply that you are executing commands on remote equipment, or within
14  another program.
15
16REFERENCE
17
18If you look at files in a directory using "ls -al" you will see the
19permissions for each file and directories. Here is an example:
20
21drwxrwxr-x    3 hervey   hervey       4096 Feb 25 09:49 directory
22-rwxr--r--   12 hervey   hervey       4096 Feb 16 05:02 file
23
24The left column is important. You can view it like this:
25
26Type User    Group Other Links  owner  group  size   date   hour  name
27d    rwx     rwx   r-x   3      hervey hervey 4096   Feb 25 09:49 directory
28-    rwx     r     r     12     hervey hervey 4096   Feb 16 05:02 file
29
30So, the directory has r (read), w (write), x (execute) access for the
31User and Group. For Other it has r (read) and x (execute) access. The
32file has read/write/execute access for User and read only access for
33everyone else (Group and Other).
34
35To change permissions you use the "chmod" command. chmod uses a base
36eight (octal) system to configure permissions. Or, you can use an
37alternate form to specify permissions by column (User/Group/Other) at a
38time.
39
40Permissions have values like this:
41Letter  Permission   Value
42
43R       read         4
44W       write        2
45X       execute      1
46-       none         0
47
48Thus you can give permissions to a file using the sum of the values for
49each permission you wish to give for each column. Here is an example:
50
51Letter  Permission                   Value
52
53---     none                         0
54--x     execute                      1
55-w-     write only (rarely used)     2
56-wx     write and execute (rare)     3
57r--     read only                    4
58r-x     read and execute             5
59rw-     read and write               6
60rwx     read, write, and execute     7
61
62
63This is just one column. Since we have three areas of permissions (User,
64Group, Other), it looks like this will all 3 sets:
65
66Permissions  Numeric      Description
67             equivalent 
68
69-rw-------   600          User has read & execute permission.
70-rw-r--r--   644          User has read & execute.
71                          Group and Other have read permission.
72-rw-rw-rw-   666          Everyone (User, Group, Other) have read & write
73                          permission (dangerous?)
74-rwx------   700          User has read, write, & execute permission.
75-rwxr-xr-x   755          User has read, write, & execute permission.
76                          Rest of the world (Other) has read & execute
77                          permission (typical for web pages or 644).
78-rwxrwxrwx   777          Everyone has full access (read, write, execute).
79-rwx--x--x   711          User has read, write, execute permission.
80                          Group and world have execute permission.
81drwx------   700          User only has access to this directory.
82                          Directories require execute permission to access.
83drwxr-xr-x   755          User has full access to directory. Everyone else
84                          can see the directory.
85drwx--x--x   711          Everyone can list files in the directory, but Group
86                          and Other need to know a filename to do this.
87
881.) CHANGING FILE PERMISSIONS
89
90
91If you are logged in as the root user on your machine please do the following:
92
93        # exit
94
95To become a normal user, like sysadm. Your prompt should change to include a “$” sign.
96       
97        $
98
99Once logged in we'll create a file and set permissions on it in various ways.
100
101        $ cd
102        $ echo “test file” > working.txt
103        $ chmod 444 working.txt
104
105What does that look like?
106
107        $ ls -lah working.txt
108
109In spite of the fact that the file does not have write permission
110for the owner, the owner can still change the file's permissions so
111that they can make it possible to write to it.
112
113        $ chmod 644 working.txt
114
115Or, you can do this by using this form of chmod:
116
117        $ chmod u+w working.txt
118
119Note: when you type these command you should be able to use the tab key for
120command completion once you've typed the "w" in the file name "working.txt" -
121This will save you quite a bit of time. It's highly recommended! :-)
122
123To remove the read permission for the user on a file you would do
124
125        $ chmod u-r working.txt
126
127Or, you can do something like:
128
129        $ chmod 344 working.txt
130
131You probably noticed that you can use the "-" (minus) sign to remove
132permissions from a file. Try reading your file:
133
134        $ cat working.txt
135
136What happened? Uh oh! You can't read your file. Please make the file readable
137by you !
138
139        $ chmod ??? working.txt
140
141Ask your instructor for help if you don't know what to put in for
142“???”. Or, look at your reference at the start of these exercises to
143figure this out.
144
145
1462. PROGRAM EXECUTION, PRIVILEGES & SUDO
147
148As a general user you can see that there is a file called “/etc/shadow”:
149
150        $ ls /etc/shadow
151
152But, you cannot see its contents:
153
154        $ less /etc/shadow
155
156What permissions does this file have? Use the examples above to figure this out. Fill
157in the blanks below once you know the permissions. We've filled in one item to get you
158stated:
159
160-___R_____
161
162As a general user, however, you can see the /etc/shadow file if you do the following:
163
164        $ sudo less /etc/shadow
165
166What is sudo? Read about it:
167
168        $ man sudo
169
170
171
1723. CREATE A NEW GROUP
173
174        $ sudo groupadd team1
175
176Prove that it really exists:
177
178        $ grep team1 /etc/group
179
180Now let's place our sysadm user in this new group:
181
182        $ whoami
183
184Just to be sure we really are the "sysadm" user right now:
185
186        $ groups
187
188You can see that sysadm is a member of the groups:
189
190        sysadm adm cdrom plugdev lpadmin sambashare admin
191
192Let's add our user to the team1 group - the '-a' is important!
193
194        $ sudo usermod -a -G team1 sysadm
195
196You won't be able to use your new group until you have logged in and out from
197your account, or have simulated this process by doing this:
198
199        $ su - sysadm
200
201        (type your own password)
202
203Now try typing:
204
205        $ groups
206
207You should see something like this:
208
209        sysadm adm cdrom plugdev lpadmin sambashare admin team1
210
211sysadm is now a member of the team1 group.
212
213Using groups like this can be useful for working in teams on a project,
214giving access to web directories, etc.
215
216
2174. GIVE GROUP ACCESS TO A FILE
218
219Do the following:
220
221        $ cd
222        $ echo “This is our group test file” > group.txt
223        $ chgrp team1 group.txt
224       
225What permissions does the file have now?
226
227        $ ls -l group.txt
228
229You should see something like:
230
231        -rw-r--r-- 1 sysadm team1 28 2012-04-16 01:32 group.txt
232
233How would you give members of the group team1 read/write access to this
234file? Before you look below try solving this on your own.
235
236We'll use the numeric chmod functionality.
237
238        $ chmod 664 group.txt
239
240Alternatively you could have typed:
241
242        $ chmod g+w group.txt
243
244Look at the file's permissions:
245
246        $ ls -l group.txt
247
248You should see something like:
249
250        -rw-rw-r-- 1 sysadm team1 28 2012-04-16 01:32 group.txt
251
252By the way
 Did you remember to just type the "g" in the filename "group.txt"
253and then use the tab key to save time in the exercises above? If not, try using
254tab in upcoming exercises. It's really worth it!
255
256
2575. MAKE A FILE EXECUTABLE
258
259Do this exercise as the sysadm user.
260
261        $ cd
262        $ touch hello
263
264Now add a single line to the file that reads:
265
266        echo 'Hello, world!'
267
268        $ echo "echo 'Hello, world'" > hello
269
270NOTE: We'll use file editors for operations like this after our next session.
271
272Let's try to run this file:
273
274        $ ./hello
275
276You'll probably see something like:
277
278        bash: ./hello: Permission denied
279
280This implies that the file is not executable. We need to set the file's permission to be executable by our sysadm user. How would you do this?
281
282        $ chmod 755 hello
283
284would work. Now try running the file:
285
286        $ ./hello
287
288You should see ...
289
290        Hello, world!
291
292... on your screen.
293
294Congratulations: you've just written your first script!
295
296Now set your hello file to be readable by everyone, NOT executable by
297the sysadm user, and executable by the Group and by Other. Can you
298figure out how to do this on your own?
299
300Look at the file's permissions to get started:
301
302        $ ls -l hello
303
304        -rwxr-xr-x 1 sysadm sysadm 20 2012-04-16 01:38 hello
305
306You want the permission to be:
307
308        -rw-r-xr-x 1 sysadm sysadm 20 2012-04-16 01:38 hello
309
310There are several ways you can do this with the chmod command.
311
312Once you have set the permissions like this, what happens if you now type?
313
314        $ ./hello
315
316Why does this happen? If you execute the file as a different user it
317will still work! Does this seem odd? (Hint: think “left to right”)
318
319You can get the file to execute, for example, by typing:
320
321        $ sudo ./hello
322
323Now set the file back so that the sysadm can execute it. Verify that this
324works.
325
326CONCLUSION
327
328What's the “./” about?
329
330In our example above when you typed “hello” the file “hello”
331is in your home directory. Your home directory is not in your default
332path as configured for the bash shell. Thus, bash will not find the
333hello file, even though it's in the same directory where you are typing
334the command. By using “./” before the filename we tell bash to
335explicitly look in the same directory for the file to execute.
336