How to set file and folder permission on Linux
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.
Related: How to change ownership of files and folders in Linux
Related: How to add user to group in Linux
Related: How to remove user from groups in Linux
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;
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;
|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:
Get current permission of file.
$ stat -c "%a : %A" /var/www/html/index.html 644 : -rw-r--r--
Add write permission for group for file.
$ sudo chmod g+w /var/www/html/index.html [sudo] password for user:
Remove read permission to other for file.
$ sudo chmod o-r /var/www/html/index.html
Set read and execute permission of user for file.
$ sudo chmod u=rx /var/www/html/index.html
Check current permission of file.
$ stat -c "%a : %A" /var/www/html/index.html 560 : -r-xrw----
Set all permission using octal and wildcard for all files and folders.
$ sudo chmod 560 /var/www/html/*
Set all permission recursively for all files and folders.
$ sudo chmod -R 560 /var/www/html/
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