You can set read, write and execute permissions on files and folders in Linux. You can apply these permissions to owner, group and those who are neither owner nor group member.

In the above screenshot you can see the highlighted section as drwxr-xr-x. The first character, d, simply implies that it's a directory where for files it should be -. The latter 9 characters are those that defines the permission for the folder.

The first 3 of the 9 characters are permissions for the user, the following 3 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 is both root.

The above example's permission could then be defined as the followings;

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

Each category is can be represented by a single letter (u, g, o). Octal notation is a way of representing the permission in binary and to 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;

OctalBinaryPermission
0000none
1001execute
2010write
3011write, execute
4100read
5101read, execute
6110read, write
7111read, 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:

  1. Get current permission of file.
    $ stat -c "%a : %A" /var/www/html/index.html 
    644 : -rw-r--r--
  2. Add write permission for group for file.
    $ sudo chmod g+w /var/www/html/index.html
    [sudo] password for user:
  3. Remove read permission to other for file.
    $ sudo chmod o-r /var/www/html/index.html
  4. Set read and execute permission of user for file.
    $ sudo chmod u=rx /var/www/html/index.html
  5. Check current permission of file.
    $ stat -c "%a : %A" /var/www/html/index.html 
    560 : -r-xrw----

  6. Set all permission using octal and wildcard for all files and folders.
    $ sudo chmod 560 /var/www/html/*
  7. Set all permission recursively for all files and folders..
    $ sudo chmod -R 560 /var/www/html/
  8. 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
Discuss the article:

Comment anonymously. Login not required.

Share!