If you are a Linux user, or a webmaster running your own website (which is probably hosted on a Linux server), you will surely come across a situation when you try to upload a file or edit a document and receive the error “You do not have the necessary permissions to download the file to the folder“. And after a little research on Google, the solution is often as easy as setting the file permission to “775” or “777”. So what exactly does “777” mean? And why does it have to be “7” and not “8” or “9”?
Understand file permissions
Unix systems (including Linux and macOS) come with a file control mechanism to determine who can access a particular file or folder and what actions they can take there. The file control mechanism has two parts, namely “Classes” and “Permissions”. Classes determines who can access the file while the Permissions determines the type of action the user can perform on the file.
There are three classes: owner, group, others.
- The owner is usually the creator of the files / folders. In Linux, the files or folders that you created in your home directory are usually possesses by you, unless you specifically change the ownership.
- The group contains a group of users who share the same permissions and user privileges.
- Others means the general public.
When it comes to permissions, there are 3 types of actions you can perform on a file / folder. You can either “read”, “write” or “execute”.
- Lily – You can only view the file, but cannot edit the contents of the file. When applied to the folder, you can only view files in the folder, but you cannot delete or add files to the folder.
- To write – You can edit and modify the file. For folders, you can delete and add files to the folder.
- Execute – Execute is mainly used when you need to run the file (commonly used when you need to run a script).
When you combine classes and permissions, you’ll be able to control who can access the file and what actions they can take on it.
For example, the owner will usually have full permissions (read, write, and execute) to access the file. If you are not the owner of the file / folder, you must change the ownership of the file to your name, or change Group or others permissions to read, write, or execute. On a web server, if you are having trouble uploading a file, it is most likely because you are not the owner of the destination folder or you do not have sufficient permissions to add files to the folder.
What about the number?
With the basic understanding of classes and permissions, let’s dig deeper into them and see how “777” or “775” occurs.
Each file and folder contains 8-bit data that controls permissions. In its basic binary form, this will be “000”, which means that no permission of any form is granted. When you set a “Read” permission, it will add 4 bits to the data, making it “100” (in binary format) or a “4” in the usual decimal format. Setting a “Write” permission will add 2 bits to the data, making it “010” and “2” as a decimal. Finally, setting an “Execute” permission adds 1 bit to the data, resulting in “001” or “1” in decimal form. In short:
- Read is equivalent to “4”.
- The writing is equivalent to “2”.
- Execute is equivalent to “1”
When we want to set permissions, we just add the number. For example, to set the permissions to “read and write” we will use “6” (4 + 2) for the permission. For read, write and execute we will use ‘7’ (4 + 2 + 1) for authorization. Here are the different permutations:
0 – no permission
1 – run
2 – write
3 – write and execute
4 – read
5 – read and execute
6 – read and write
7 – read, write and execute
Depending on what permissions you want to grant to the file, just set the number accordingly.
What about the 3 digits “777”? Well, the first digit is assigned to the owner, the second digit is assigned to the group, and the third digit is assigned to the others. So for a file with the ‘777’ permission, anyone can read, write and execute the file. Here are some of the commonly used permissions:
- 755 – This permission set is commonly used in web servers. The owner has full read, write, and execute permissions. Everyone else can only read and execute, but cannot make changes to the file.
- 777 Anyone can read, write and execute. In a web server, it is not advisable to set permission “777” for your files and folders as this allows anyone to add malicious code to your server. However, in some cases you will need to set permissions 777 before you can upload a file to the server (for example uploading images in WordPress)
- 644 – Only the owner can read and write. Everyone can only read. No one can run the file.
- 655 – Only the owner can read and write, but not execute the file. Anyone can read and execute, but cannot edit the file.
Setting file permissions on the command line
In Linux, you can easily change file permissions by right clicking on the file or folder and selecting “Properties”. There will be a Permission tab where you can change file permissions. In the terminal, the command to use to modify the file authorization is
chmod. In short, “chmod 777” means to make the file readable, writable and executable by everyone.
Hope this article can help you better understand file permissions in Unix system and the origin of the magic number “777”.
Now that you know file permissions, you might want to know how to copy and paste text, files, and folders into the Linux terminal, or how to use sticky bit to manage files on shared directories.
Image Credit: Pixelated Linux by DepositPhotos
Is this article useful?