Guide to Linux File Permissions
In this guide I will try and help people to understand permissions in Linux, the particular distribution I am using here is Centos 6 32 bit, however the commands are across the Linux board. I will try and make it as simple as possible so bare with me. To start lets list files in a particular folder. To do this we use the following command.
ls
We see the following output, in my tmp directory I have a test folder and a testfile.txt.
This is just a basic listing of the files, If we want to see the permissions are on these files in Linux we use the following command.
ls -l
We then get the following output.
Lets try to make sense of this, the file “testfile.txt” was created on the 20th of june at 05.50. It is owned by the “root” group and specifically the user “root” and it is 4096 bites in size. Now lets break down the -rw-r–r–.
The first “-rw” permission applies to the user “root” and means that user has “read” and “write” which means “modify” permissions on the file.
The next “-r” applies to the root group. This means that all other users who are members of the “root” group can read the file but they cannot modify. So if a user called “Allen” was a member of the “root” group he would only be able to read that file.
After this we have the overall user permissions sometimes know as “world or “other“, this is everyone else who may have access to the pc/server etc.. In the example above they have “read” rights only, the — after would be populate if they had “write” or “execute” rights.
How Do I Change Permissions of Files In Linux?
So we now understand the rights on the file above, how do we change them?. We use a command called “chmod“, before we use it lets go over the the three different levels again and also the levels of permissions.
User
Group
Other
For permissions we have the following.
Read – 4
Write – 2
Execute – 1
No Permissions – 0
Notice the numbers above? They are used when setting these permissions simply by adding them together so for example.
4 – Read only.
6 – Read and Write (Modify).
7 – The works, Read, Write and Execute.
Lets put this into action on testfile.txt. I want to give everyone in the root group full access but everyone else no permissions. This would be 7-7-0. We use the following syntax.
chmod 770 testfile.txt
We have given the “root” users full permission with “7“(4+2+1), the the “root” group full permission with “7“(4+2+1) and everyone else no permissions with “0“. Lets list those permission to see how it looks with the “ ls -l” command again.
So “-rwx” is the user “root” with full permissions, the second “rwx” is the root group also has full permissions and the “—” means everyone else has nothing. Lets see some examples:
chmod 444 testfile.txt – everyone has read.
chmod 640 testfile.txt – Root has modify, the root group can read and everyone else has nothing.
chmod 644 testfile.txt – Root has modify, the root group can read as can the rest of the world.
chmod 777 testfile.txt – everyone has full rights.
What are all the Linux File Permissions Options?
Using a combination of the the permissions we can create all the permissions we need:
7 = 4+2+1 (read/write/execute)
6 = 4+2 (read/write)
5 = 4+1 (read/execute)
4 = 4 (read)
3 = 2+1 (write/execute)
2 = 2 (write)
1 = 1 (execute)
As a beginner to Linux I thought id document this as a step by step guide to Linux permissions as I learn so that this can help other newbies out there. If you want to learn Linux yourself consider a cheap VPS for your learning needs check out 25% OFF VPS Hosting.
Tags: centos, learn linux, linux commands, linuxcommands