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