How permissions work in Linux
Permissions in Linux can be confusing to newcomers. There are 3 different permissions, read, write, and execute. You’ve probably seen how they are represented if you run ls -l:
drwxr-xr-x 3 user group 4096 09 may 2019 exampleDir
The letters and dashes at the left represent the permissions, for the owner, group, and other users, respectively. In this case, the “d” at the beginning tells us that it’s a directory. The first rwx tells us that the owner can read, write, and execute the file. The following letters, r-x, tells us that the group applied to the file can read and execute, and as you can tell by the last 3 letters, the same applies to all users ot in the group.
When you use chmod to alter the permissions, they are represented in a number-format, with 3 digits ranging from 0 to 7, the first represents the owner, the second represents the group, and the third represents all other users.
To calculate which digits you want, add the numbers that represents the permission you want:
- 4 = Read
- 2 = Write
- 1 = Execute
- 0 = No permissions
For example, if you want the owner of a file to be able to read and write to it, and the group and all other users to read it, run the following command:
chmod 644 exampleFile
Now ls -l should list the file as follows:
-rw-r--r-- 3 user group 4096 09 may 2019 exampleFile
Note that on directories, execute allows a user to traverse a directory and list the files in it, unlike files where execute allows you to launch them, like scripts.