You can set read, write, and execute permissions on files and folders in Linux. You can apply these permissions to the owner, group, and those who are neither owner nor group member.

In the above screenshot, you can see the highlighted section as drwxr-xr-x. The first character, d, implies that it's a directory where for files, it should be . The latter nine characters are those that define the permission for the folder.

The first three of the nine characters are permissions for the user, the following three for the group and followed by for other users, represented by r (read), w (write), x (execute), and (no permission). The assigned user and group for the directory are both root.

The above example's permission could then be defined as the followings;

Category Letter Permission Octal Read Write Execute
User (root) u rwx 7 Yes Yes Yes
Group (root) g r-x 5 Yes No Yes
Other o r-x 5 Yes No Yes

Each category can be represented by a single letter (u, g, o). Octal notation is a way of representing the permission in binary and then total up the number. r-x for example is 1*(2^2) + 0*(2^1) + 1*(2^0), which results in 5. Here's a table for easier illustration;

Octal Binary Permission
0 000 none
1 001 execute
2 010 write
3 011 write, execute
4 100 read
5 101 read, execute
6 110 read, write
7 111 read, write, execute

With that out of the way, you can start configuring permission for your files and folders.

There are other methods and layers of file and folder permissions in Linux such as ACL, SELinux and AppArmor but is not covered here.

Steps to set file and folder permission on Linux:

  1. Get current permission of file.

    $ stat -c "%a : %A" /var/www/html/index.html  644 : -rw-r--r--

  2. Add write permission for group for file.

    $ sudo chmod g+w /var/www/html/index.html [sudo] password for user:

  3. Remove read permission to other for file.

    $ sudo chmod o-r /var/www/html/index.html

  4. Set read and execute permission of user for file.

    $ sudo chmod u=rx /var/www/html/index.html

  5. Check current permission of file.

    $ stat -c "%a : %A" /var/www/html/index.html  560 : -r-xrw----

  6. Set all permission using octal and wildcard for all files and folders.

    $ sudo chmod 560 /var/www/html/*

  7. Set all permission recursively for all files and folders.

    $ sudo chmod -R 560 /var/www/html/

  8. Check current permission for all files and folders.

    $ sudo ls -l /var/www/html/ total 20 dr-xrw---- 2 root root  4096 Jan  24 09:58 css -r-xrw---- 1 root root 10918 Jan  23 19:57 index.html dr-xrw---- 2 root root  4096 Jan  24 09:57 js