Going to start with this as it seems to be a fundamental concept central to figuring the rest of this stuff out. And also because it’s a fairly simple concept to grasp.
If you view the files in a directory by typing ls -l, you will see a sequence before the file name as shown below:
root@servername:/var/www# ls -l
total 16
drwxr-xr-x 3 root root 4096 2012-01-24 00:55 .
drwxr-xr-x 15 root root 4096 2012-01-23 04:36 ..
-rw-r--r-- 1 root root 200 2012-01-23 05:25 index.html
drwxr-xr-x 4 root developers 4096 2012-01-24 00:55 websiteurl.com
Now that may seem like a lot of information, but it’s really not. Let’s break it down:
On the first line, you can see that I am logged in as root on my server and inside the folder /var/www. Now at this prompt, I typed ls -l to view the contents of this directory in detail. I was then shown the contents. The first two rows are just . and .. which we can ignore for now (just know that they go back to the root folder and parent folder respectively).
So that leaves us with two things in this directory, index.html and websiteurl.com - One is a file and one is a directory. Let’s have a look at them broken up:
So you can see that directory listing is displayed in 7 columns.
Permissions - This is broken up into 4 parts. On the index.html file the permissions column has the following entry: “-rw-r--r--”. This is essentially a 10 character string. The first character position is ‘-’ for index.html and ‘d’ for websiteurl.com -- This is because the first character tells you if this is a file or a directory. When it is a file, it is left blank, and displays a ‘d’ in case of directory.
The next three characters will tell you what permissions the user (or owner) of the file has. They are depicted by rwx in that order. So, in this case we have ‘rw-’ on index.html which means that the owner has read and write access but not execute.
After the user’s permissions come the three characters which tell you what permissions the “group” assigned to that file or directory has. In the case of index.html we see that the group has ‘r--’ which means that they only have read access and cannot write or execute.
The last three characters tell you what rights others (or everyone) has. In the case of index.html again we see that it is only read access ‘r--’.
Directories - Tells you how many directories are within this particular folder.
Owner - Displays the name of the owner of the file.
Group - Displays the name of the group assigned to this file or directory.
Size - Displays the size of the file or folder.
Date - Date and time of the last update made to said file or folder.
Directory or file - Filename or directory name.
So once we know this information, the jargon that is displayed when you type ls -l doesn’t seem all that intimidating anymore. Now we can jump into ‘modifying’ these permissions.
There are three commands that we’ll discuss here for all things permissions related. They are chmod, chown and chgrp.
chmod - Modifies permissions
chown - Changes the owner of the file
chgrp - Changes the group of the file
Simple? I thought so. You can check out the different arguments you can pass with these commands by typing ‘man chmod’ to see the manual. I am going to use my favorite one in the examples below, which is -R. This basically means “recursively apply this command to all the files and sub-folders within this directory”.
So, for this example, I went to my /var/www folder and created a file called test.txt by typing “vi test.txt” and then inserted the text “This is a test file” and saved.
This is what the file looks like when I type ls -l:
-rw-r--r-- 1 root root 16 2012-01-24 06:31 test.txt
Now I will change the permissions for this file. I want to give:
Owner - Read, write, execute
chmod ug=+x (Give ‘User’ i.e. owner and ‘Group’ execute permission)
Now when we do ls -l, our test.txt file is as such:
-rwxr-xr-- 1 root root 16 2012-01-24 06:31 test.txt
More more details on chmod and it’s literally hundreds of possibly uses, I highly recommend this tutorial. (http://catcode.com/teachmod/chmod_cmd.html)
chown is far simpler to use. Simply type “chown username file-or-directory-name”. This will change the User (owner) of that file.
chgrp is similar in use. Simply type “chgrp groupname file-or-directory-name”. This will change the Group of that file.
That just about covers the basics. Please check out the reference URL above to polish up on the details.