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).
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 |
There are other methods and layers of file and folder permissions in Linux such as ACL, SELinux and AppArmor. These however, are beyond the scope of this guide.
Steps to set file and folder permission on Linux:
- Check the current permissions of a file.
$ stat -c "%a : %A" /var/www/html/index.html 644 : -rw-r--r--
- Add write permission for the group.
$ sudo chmod g+w /var/www/html/index.html [sudo] password for user:
- Remove read permission for others.
$ sudo chmod o-r /var/www/html/index.html
- Set read and execute permissions for the owner (user).
$ sudo chmod u=rx /var/www/html/index.html
- Verify the current permissions of the file.
$ stat -c "%a : %A" /var/www/html/index.html 560 : -r-xrw----
- Set permissions using octal notation and wildcards for all files and folders.
$ sudo chmod 560 /var/www/html/*
- Apply permissions recursively to all files and folders.
$ sudo chmod -R 560 /var/www/html/
- Check the current permissions 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
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.