I’ve been studying Linux for the past few months and one aspect of Linux that has always confused me is how permissions work. For example, when uploading a file to my web server once and receiving an error message, my web host told me to change the file permissions to 755.
I had no idea what that meant, although changing the permissions fixed the problem. Now I realized that Linux permissions are not that complicated, you just need to understand the system. In this article, I will introduce high level Linux permissions and show you how to use the chmod command to change file and folder permissions.
Linux Permissions and Levels
Linux usually has three permissions that you usually need to worry about: read, write, and execute. All three of them speak for themselves. Now, when these permissions are applied to a file, they are applied level by level.
Linux has three levels of permissions: owner, group, and others. Owner is the user who owns the file / folder, the group includes other users in the file’s group, and the other simply represents all other users who are not the owner or in the group.
Reading, writing, and executing are represented by either symbolic characters or octal numbers. For example, if you run ls -l in a directory with some files, you will see a symbolic representation of the permissions.
– /
Permissions are written as follows: the first bit is either a dash or the letter d. A dash means it is a file and d means a directory. Note that the first bit can also be l if the filename is a link. Then there are three groups of three bits. The first bit in each group is for reading, the second bit for writing, and the third bit for executing. The first three bits are for the owner, the second three bits are for the group, and the third three bits are for others. Here’s a more visual explanation.
If you see a dash instead of a letter, it means the owner, group, or all other users do not have this permission. In the above example, owner, group, and everyone else has read, write, and execute permissions.
If you look at the output of the ls -l command, you will notice that my practical text file has the following permissions:
-rw-rw-rw-
This means everyone has read and write permissions for the file. Another example:
drwxr – r–
Looking at the first bit, we can see that the permissions are for the directory. The owner has read / write / execute permissions, but the group and other users have read-only permission.
Representation of an octal number
This is how permissions are displayed in Linux using symbols. A second way to represent the same permissions is to use octal numbers. When we later use the chmod command, you will see that you can change the permissions using characters or octal numbers.
So how does Linux represent reading, writing, and executing with octal numbers? Basically, it just assigns a number to each resolution as shown below.
Read permission is represented by 4, write permission is 2, and execute permission is 1. All you have to do is add them up to get octal permission. For example, take the example above where everyone has all permissions:
-rwxrwxrwx
The owner has rwx, so we add 4 + 2 + 1 to get the value 7. We do the same for the group and the same for the other. The last octal value is 777. Let’s look at an example where we only gave read and write permissions:
-rw-rw-rw-
The first octal number will be 4 + 2 as we add read and write. The second will be the same as the third octal number. Here we have the final octal value 666.
So now let’s try it differently. Let’s say we want to know what is the 755 resolution? Well, that’s pretty easy to figure out if you break it down into individual numbers. The first number is 7, which we can only get by adding 4 + 2 + 1, which means the owner has read / write / execute permission. Five can only be obtained by adding 4 + 1, which means the group and other users have read and execute permissions.
Hopefully this is a good explanation of how to represent permissions in Linux using octal numbers. In general, everything is pretty simple.
Using chmod to modify permissions
Now that we understand how to read permissions, let’s talk about how to change them. The simplest utility for this purpose is the chmod command. This is how it works. The best way to explain a command is to look at an example.
Let’s start with the permissions we talked about above, namely:
-rw-rw-rw-
If we wanted to add execute permission for owner, group and others, we could do it in two ways. We could use the symbolic method or the octal method. For the symbol method, we would do the following as shown below:
Exact command
chmod a + x filename
The syntax is as follows: a letter or letters representing owner (u), group (g), other (o), or all (a), followed by + to add permissions, or a to remove permissions, and then a letter for permission ( r for reading, w for writing, and x for executing).
In the above example, I’ve added execute permission for all users. The result, as you can see in the screenshot above, is x for owner, group and others. Now, suppose I wanted to remove write and execute permissions for the group and other users only.
As you can see here, I used the following command for this:
chmod go-wx filename
Since I want to change the permissions for the group and others, I use the letter g and the letter o. I want to remove permissions, so I use the – sign. Finally, I want to remove write and execute permissions, so I use w and x. Here is a handy little table for using symbols:
That’s all there is to know about using the symbol method. Now let’s talk about the octal method, which I think is a little simpler. The cool thing about Octal is that you can add or remove permissions right away.
If we start with the following file permissions, let’s see how we can change them using the octal method:
-rw-rw-rw-
Above you can see that I used the following command:
chmod 744 filename
This basically means that the owner gets read / write / execute permission and the group and other users get read only permission. As you can see, adding or removing permissions is easy with one simple command. Let’s go ahead and say that I want to change the permissions again.
Now I have used the following command, again very simple:
chmod 640 filename
Here we have given the owner read / write permissions, the group read only, and the other group not. You use zero to indicate no permissions. Pretty simple huh?
In conclusion, this is a very basic overview of Linux permissions, which can be a lot more complicated than this, but for beginners, this is a good place to start. I will be posting more articles on extended permissions in the future. If you have any questions, do not hesitate to comment. Enjoy!
–