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:

User (root)urwx7YesYesYes
Group (root)gr-x5YesNoYes

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:

3011write, execute
5101read, execute
6110read, write
7111read, 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.

Steps to set file and folder permission on Linux:

  1. Check the current permissions of a file.
    $ stat -c "%a : %A" /var/www/html/index.html 
    644 : -rw-r--r--
  2. Add write permission for the group.
    $ sudo chmod g+w /var/www/html/index.html
    [sudo] password for user:
  3. Remove read permission for others.
    $ sudo chmod o-r /var/www/html/index.html
  4. Set read and execute permissions for the owner (user).
    $ sudo chmod u=rx /var/www/html/index.html
  5. Verify the current permissions of the file.
    $ stat -c "%a : %A" /var/www/html/index.html 
    560 : -r-xrw----
  6. Set permissions using octal notation and wildcards for all files and folders.
    $ sudo chmod 560 /var/www/html/*
  7. Apply permissions recursively to all files and folders.
    $ sudo chmod -R 560 /var/www/html/
  8. 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
Discuss the article:

Comment anonymously. Login not required.