File Permissions

There are 3 permissions that you can assign to a file or directory,

  1. Read – (R)
  2. Write – (W)
  3. EXecute – (X)

Also these 3 types of permissions can affect a file or directory by:

  • Owner (u for UNIX/linux)
  • Group (g for UNIX/linux)
  • Everyone or Others (o for UNIX/linux)

How can I use permission on my Linux box?

Go to your terminal and type:

ls -l

you should see a list like this one:

Ok, Let’s use the info in the image, after the command “ls -l” on my /Desktop folder, you can see that I have 2 things there,
  1. a folder called ‘hacknplay_pictures’
  2. a file called ‘log_Dec14_2011_1348.txt’
Despite the ‘blue’ color in the folder or ‘directory’ called ‘hacknplay_pictures’ you can see that it is a directory because the weird characters at the beginning of the line on the command line:
drwxrwxr-x  2  mckain  mckain 4096 Mar  25  18:10  hacknplay_pictures
If the line starts with:
  • ‘d’ means it’s a directory (folder).
  • ‘l’ means it’s a link
  • ‘-’ (dash) it means it’s a file.
let’s focus on the following characters from the above screenshot:
drwxrwxr-x

d      >> file type (Remember ‘d’ means Directory, but it could be ‘l’ or ‘-’).

rwx >>  The Owner (you (u) ) got all the rights or permissions (read, write and execute).

rwx >> The Group (g) got the same rights or permissions as the Owner (mckain).

r-x >> Everyone (Others (o)) got just ‘read’ and ‘execute’ permissions.

Alright?

How to change file permissions?

You can change the file permissions using the command ‘chmod’, using this command you can customize your file permissions in order to fit your needs.
I created a .txt file on my /Desktop directory just for this example, the file is called ‘frenchfries.txt’
As you can see the permissions for my frenchfries.txt file are:
-rw-rw-r–
If we use the above instructions you can say that:
  • -      >> Describes that it’s a file.
  • rw- >> [Owner permissions] it means ‘read and write’ permissions for the the file.
  • rw- >> [Group permissions] read and write permissions.
  • r–   >> [Other/Everyone permissions] It describes just ‘read‘ permissions.
ok, now let’s say that we want to change permissions to this file (frenchfries.txt), we just want the Owner (mckain) to be able read it.
chmod u-w frenchfries.txt
After type the above command, let’s check the file permissions again, do the ‘ls -l’ and you should see something like:
-r–rw-r–
Congratulations! you just removed the ‘write‘ permissions to the .txt file on the ‘Owner’ group!.

More examples:

Try to figure out what the following command will do:

chmod u-rwx hacknplay_pictures

Answer:

The command removes the read, write and execute permissions to the directory.

What about this one?

chmod guo-r hacknplay_pictures

Answer:

You can use ‘combos’ too, oh yeah! when you do “guo” you are talking about the 3 different groups in the permissions file (Group (g), Owner (u) and Other (o)), so in this command we are removing the ‘read’ permissions to the 3 groups (guo).

and the last example:

chmod uo+r hacknplay_pictures

Answer:

Remember about the ‘combos’ with ‘uo’ we are referring to Owner (u) and Others (o), and with the above command we apply ‘read’ permissions to the hacknplay_pictures directory.

The second way to assign permissions (The Numerical Way):

There’s another way to change permissions to files or directories and it is the ‘numerical way‘ (that’s how I called it),
basically there is a number for:
  1.  Read (r)     —— 4
  2. Write (w)    —— 2
  3. Execute (x) —— 1
In order to remember this configuration I made this table:

Read (x)

Write (w)

Execute (x)

22

21

20

4

2

1

basically you can think in binary and you will have the table.
I think it’s better if you can deduce the stuff, I apply that with my Math courses and it works like a charm!.
Do you remember the Owner (u), Group (g) and Others (o)?
Good! so you just have to remember that there are 3 permissions per group (rwx) so If you use the number (4, 2 and 1) you just have to sum them.
For example, if you want to assign ‘read’ and ‘write’ permissions only to ‘Owner (u)’ on the ‘frenchfries.txt’ file.
Owner (u)  >>  [4 + 2]  (because the numbers for read and write are  4 and 2).
Group (g)   >>  [4] (because we want to block the ‘write’ and ‘execute’ access to the Groups).
Others (o)  >>  [4] (because we want to block the ‘write’ and ‘execute’ access to Others too.).
If we add the final numbers for the 3 Groups we have:
= [4 + 2] + [ 4 ] + [ 4 ] =
= [ 6 ] + [ 4 ] + [ 4 ]
= 644.
Now we can use the ‘chmod‘ command and it will look like:
chmod 644 frenchfries.txt
the final result should look like:
-rw-r–r–
Alright ladies and gentleman, I hope you understand my little tutorial about the ‘File Permissions’ I tried to make it ‘verbose’ in order to explain it with more details.
Hope you like it!
Tagged with →  
Share →

Leave a Reply

Your email address will not be published. Required fields are marked *


× 9 = seventy two

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

show
 
close
Adding a custom app to the launcher (gnome 3) on Fedora 17 >> http://t.co/iDQlSrAp #HacknPlay