In Linux, you can assign read, write, and execute permissions to files and folders for three categories of users: the owner, the group, and others (neither owner nor group members).
Related: How to change file and folder ownership in Linux
Related: How to add a user to a group in Linux
Related: How to remove a user from one or more groups in Linux
In the above screenshot, the permissions are represented by a string of characters like drwxr-xr-x. The first character (d or -) indicates whether it's a directory or a file, respectively. The remaining nine characters define the permissions for the owner (user), group, and others.
These nine characters are grouped into sets of three, with each set representing permissions for a user category (r for read, w for write, x for execute, and - for no permission). In our example, the permissions are as follows:
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 is represented by a single letter (u, g, o). Octal notation is used to represent permissions as a single number. For instance, r-x converts to binary 101 and then to octal 5. Here's a quick reference table:
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 this knowledge, you can begin configuring permissions 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.
$ stat -c "%a : %A" /var/www/html/index.html 644 : -rw-r--r--
$ sudo chmod g+w /var/www/html/index.html [sudo] password for user:
$ sudo chmod o-r /var/www/html/index.html
$ sudo chmod u=rx /var/www/html/index.html
$ stat -c "%a : %A" /var/www/html/index.html 560 : -r-xrw----
$ sudo chmod 560 /var/www/html/*
$ sudo chmod -R 560 /var/www/html/
$ 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
Comment anonymously. Login not required.