Are you a Linux user, or a webmaster managing your own website (which is probably hosted on a Linux server)? Sooner or later you’ll try to upload a file or modify a document and will receive the following error: “You do not have the permissions to upload file to the folder.” After some Googling, the solution is often as easy as setting the file permission to “775” or “777.” But what does “777” mean? And why must it be “7” and not “8” or “9”?
Understanding File Permissions
Unix systems (including Linux and macOS) have a file control mechanism that determines who can access a particular file or folder and what they can do with that file or folder.
There are two parts to the file control mechanism: “Classes” and “Permissions.” Classes determines who can access the file, while the Permissions determines what the user can do with that file.
There are three Classes: Owner, Group, and Others.
- The Owner is usually the creator of the file or folder. In Linux, any files or folders that you create in your Home directory are usually owned by you unless you specifically change the ownership.
- The Group contains a group of users who share the same permissions and user privilege.
- Others means the general public.
As for permissions, there are three type of actions that you can perform on a file or folder:
- Read. You cannot modify the contents of the file in any way. When applied to a Folder, you can only view the files within that folder; you cannot delete or modify the files in any way or add more files to the folder.
- Write. You can modify the file. If you have “write” access to a folder, then you can delete and add files to that folder.
- Execute. Execute is mainly used when you need to run the file and is most commonly used when you need to run a script.
By using Classes in combination with Permissions, you can control who has access to a file and the actions they can perform on said file.
The file owner will usually have all three permissions (read, write and execute). If you’re not the owner of the file or folder, then you’ll typically have to change Ownership to your name, or change the permissions of Group or Others to read, write and/or execute.
In a web server, if you’re unable to upload a file, then it’s probably because you’re not the owner of the destination folder. Alternatively, you may not have sufficient permissions to add files to the folder.
777: What’s in a number?
Now that we have a basic understanding of Classes and Permissions, let’s see why “777” and “775” are so important.
Every file and folder contains 8-bit data that control the permissions. At its basic binary form, “000” means that no permissions of any form are granted.
When you set a “Read” permission, it adds 4-bit to the data, making it “100” (in binary format) or a “4” in the usual decimal format. Setting a “Write” permission will add 2-bit to the data, making it “010” and “2” in decimal form. Lastly, setting an “Execute” permission adds 1-bit to the data, which will result in “001,” or “1” in decimal form. In short:
- Read is equivalent to ‘4’.
- Write is equivalent to ‘2’.
- Execute is equivalent to ‘1.’
When we want to set permissions, we just add a number. For example, to set the permissions to “read and write,” we use “6” (4 + 2) for the permission. For read, write and execute, we’ll use “7” (4 + 2 + 1) for the permission.
Here’s the different permutation:
0 – no permission
1 – execute
2 – write
3 – write and execute
4 – read
5 – read and execute
6 – read and write
7 – read, write, and execute
Depending on the permissions you want to grant to the file, you just need to set the number accordingly.
What does this mean for ‘”777?” 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. If a file has the “777” permission, then everyone can read, write and execute the file.
Here are some of the commonly used permissions:
- 755. This set of permissions is commonly used by web servers. The owner has all the permissions to read, write and execute. Everyone else can read and execute but cannot make changes to the file.
- 777. Everyone can read, write, and execute. In a web server, it’s not advisable to use the “777” permission for your files and folders, as this allows anyone to add malicious code to your server. However, in some cases you’ll need to set the 777 permissions before you can upload any file to the server – for example: uploading images in WordPress.
- 644. Only the owner can read and write. Everyone else can only read. No one can execute this file.
- 655. Only the owner can read and write and cannot execute the file. Everyone else can read and execute and cannot modify the file.
Setting File Permissions in Command Line
In Linux, you can easily change the file permissions by right-clicking the file or folder and then selecting “Properties.” This will open a “Permission” tab where you can change the file permissions.
If you’re a Mac user, then you can change the permission settings by right-clicking the file or folder in question and then selecting “Get Info.” In the subsequent window you can click to expand the “Sharing & Permissions” section. This displays the permission settings for every account registered on your Mac.
To modify these permissions, click any of the little arrows and then select either “Read & Write” or “Read Only.”
You can also change permissions using the
chmod command in the Terminal. In short, “chmod 777” means making the file readable, writable and executable by everyone.
Hopefully, this article helped you better understand file permissions in Unix systems and the origin of the magical number “777.”
Now that you’ve mastered file permissions, you may want to learn how to copy and paste text, files and folders in the Linux terminal or use sticky bit to manage files on shared directories.