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