In Linux, every file and folder has permissions that control access. These permissions are assigned to three categories of users: the owner, the group, and others. Permissions determine whether users can read, write, or execute a file or folder. Managing these permissions is crucial for maintaining security and access control within the system.

Permissions are represented by a string like drwxr-xr-x. The first character indicates if it’s a directory (d) or a file (-). The next nine characters are grouped into three sets, each representing the permissions for the owner (user), group, and others. Within each group, r stands for read, w for write, and x for execute, while a dash (-) indicates no permission.

Here is a quick reference table for understanding the permissions:

CategoryLetterPermissionOctalReadWriteExecute
User (owner)urwx7YesYesYes
Groupgr-x5YesNoYes
Othersor-x5YesNoYes

Permissions can be represented in octal notation, with each digit corresponding to a combination of read, write, and execute permissions:

OctalBinaryPermission
0000none
1001execute
2010write
3011write, execute
4100read
5101read, execute
6110read, write
7111read, write, execute

Understanding how to modify these permissions, using either symbolic representation (like u=rwx) or octal notation (like 755), is key for controlling access to files and folders in Linux.

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:

  1. Check the current permissions of a file or folder.
    $ stat -c "%a : %A" /var/www/html/index.html 
    644 : -rw-r--r--

    The first number represents the octal permission, and the second shows the symbolic representation.

  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 for all files in a directory.
    $ sudo chmod 560 /var/www/html/*

    Using wildcards applies permissions to all files in the directory.

  7. Apply permissions recursively to all files and folders.
    $ sudo chmod -R 560 /var/www/html/

    Be cautious when applying permissions recursively, as it can affect all subdirectories and files.

  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.