Tuesday, July 20, 2010

file permissions and chmod

When you "ls -l" you get some crazy symbols on the left similar to "drwxr-xr-x". These represent the file permissions. Here is how they work.
You have 10 letters: "0123456789"
0 will be 'd' if it's a directory and '-' otherwise
123, 456, 789 correspond to the rights for "owner", "group" (owner's group), "every body else"
so, 1,4,7 are for the "read" right
2,5,8 are for the "write" right
3,6,9 are for the "execute" right
If you have the right, there will be an 'r', 'w', or 'x' respectively, otherwise a '-'
The command to change those is chmod. To change something, you must have a "write" right, or be a root.
chmod works in 2 ways:
- Either write "chmod xxx file" where x is a single octadecimal digit corresponding to the rights you give to each group, where the 1st x is for the 1st group and so on.
As a side note, here is a table for octadecimal transformation into binary:
0 -> 000
1 -> 001
2 -> 010
3 -> 011
4 -> 100
5 -> 101
6 -> 110
7 -> 111
so, now map the 3 binary digits to "rwx" and you get your rights for one group. For example, 5, which is 101, represents r-x rights.

- the other way to use chmod is to do use some letters
'u' = user (owner)
'g' = group
'o' = others
'a' = all (the 3 permissions)
'r' = read
'w' = right
'x' = execute
'-' = remove
'+' = add
and you write it such as "chmod [a|u|g|o]+ [+|-] [r|w|x]+". What this means in english is, write chmod, then write any combination of "augo" then write '+' or '-' then write any combination of "rwx". So u+x will give the owner execute right and og-rw will remove the read and write from others and group.
Take care because sometimes a-r or a-w or u-r or u-w can be dangerous if you don't have root access. There are a lot of other options, consult the chmod manual (man chmod) for the full details.


No comments: