File and Folder permissions plays critical security role for any website. Sometimes website stops working due to incorrect file permissions. This guide explains you how to correct file and folders permissions using command line of linux.
Using Command Line tool or Terminal to change file permissions
Its very easy to update file permissions using terminal, if you are using Linux based Operating System like Ubuntu, Debian. Press Ctrl + Alt + T to open a terminal, or search for terminal using app launcher.
Now you need root permissions to change file permissions. If you are the only user, enter system password if prompted.
#Syntax of chmod for single file/folder
sudo chmod [MODE] filename
#Syntax of chmod for whole folder to update permissions for all files inside a folder
sudo -R [MODE] foldername
Examples:
#Single file or folder - set to read/write/execute for owner, read and execute only to group or others.
sudo chmod 755 filename
#Loop through folder - set to read/write for owner, read/execute for others or group
sudo chmod 655 foldername
File Permission Modes Explained
How numbers from 0 to 7 works and what do they mean:
We have three main operations allowed in any file system, Which are READ, WRITE and EXECUTE.
000 – No permissions, file completely locked
111 – Only File execution allowed, no read and write allowed
222 – Only File write is allowed, no execution and read are allowed
333 – Only File execution and write are allowed, no read allowed
444 – Only Reading of file contents allowed, no execution and writing is allowed – (This is recommended for making important and sensitive files locked from others).
555 – Only Read and Execution of files is allowed, no write allowed (This is recommended for making important and sensitive files locked from others).
644 – Owner can read, write to a file, but cannot execute it, others can only read it. Most common file permission.
655 – Owner can read and write to a file, but cannot execute it, others can read and execute it. Most common file permission.
755 – Owner can do all read, write and execute operations, others can only read and execute it. Most preferred folder permission.
777 – Every one can read, execute and write to file/folder, most dangerous and risky permission.
Preffered Permissions for Files and Folders
For Files – Use 644 or 655
For Folders – Use 755
Thats all, keeping it simple, there are many chmod options which were not added as they are not required for most users or server operations.