Agenda: permissions-exercises.txt

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