Linux File Permissions
I’ve allways struggled with how the linux permissions work. Recently i had another one of those times where i had to change a folders permission recursively so, once again, i googled it. The 5th result is a Google Code University Lab which actually explained it in a way i could get.
This post is a simple sumary of the lab.
When you do an ls -l on your console you will see something like the following
drwxr-xr-x. 2 sapiens sapiens 4096 2009-10-19 14:20 Desktop drwxr-xr-x. 2 sapiens sapiens 4096 2009-10-07 23:55 Documents -rwxr-xr-x. 3 sapiens sapiens 4096 2009-11-05 18:42 myStuff.txt
If you look at the start of the line you will notice a bunch of letters, they are actually 10 of them. At first when you are getting into linux these will seem like gibberish to you and you will basicly ignore them, but when you need to actually change the folders permissions you might want to try and learn how to read them.
The letters on that text means the following :
- d – directory;
- r – read granted;
- w – write granted;
- x – execute granted;
- – – Not granted.
Tha piece of text can be divided in 4 groups (1st group composed of the first caracther, then 3 groups of 3 characters each) :
- first letter states wether the file is a folder or not (if it has a ‘d’ its a directory);
- the next 3 letters states the permissions for the file Owner
- The next 3 letters states the permissions for the Group
- the last 3 letters states the permissions for Everyone
So you’ve learned how to read it, but how do you change it well basicly you will be using chmod for that (just man it). As for the arguments you can either write the whole text for the given file or use the short hand.The following table shows you how to use short hand version to give permissions:
Number | Read (r) | Write | Execute (x) |
---|---|---|---|
0 | – | – | – |
1 | – | – | x |
2 | – | w | – |
3 | – | w | x |
4 | r | – | – |
5 | r | – | x |
6 | r | w | – |
7 | r | w | x |
Note: The above table was directly taken from the google lab, it also presents a good way to remember this table
In order to change a files permission you would write a 3 digit number, using the table above, that defines the permissions for the groups mentioned above (owner, group, everyone else) for instance:
- 644 would change a file/folder pemission to be read-only to everyone but the owner which would have full permissions
- 755 (probably one of the most used) would make the file executable and readable to everyone and full permissions to the file owner.
External Links :
- Google Code Lab – Linux Ownership and Permissions Lab