How to give permission to folder and files in Linux – chmod

File permission controls access to files for Users and Groups. Before starting how to give permission to folder and files in Linux ,  you must know about user and permission types. Permission is given to three categories of users i.e. Owner user, group and third type of user is other user. Priority of application user permission is in sequence User, Group and finally other. Only user relevant specific permission will apply.

Each user has three categories of Permission: Read (x), Write (w) and Exec (x). Read Permission will allow listing of Files. Write will allow modification of files. Execute will allow execution of files as commands.


What is permission

In Linux and Unix based systems access to all files and directories are controlled by file permission. Permission is divided into three categories

  • Read – This permission when granted allows user to open and read contents of files.
  • Write – When write permission for a file is granted to user, he can modify contents of file. When this permission is granted for a directory, user can add, remove and rename contents of file.
  • Execute – This permission when set allows user to run a script or program.

To whom these permissions apply

For all files and directories permission there are three set of owners.

  • Users –  A user is owner of file. The user who creates the file is owner of file.
  • Group – A group is set of user. When permission is given to a group all user who are member of group get those permissions.
  • Others – Permissions to others refers to what action all other users ( who are neither covers as user nor as group) can perform on the file.

How to change permission of files and folders from terminal mode

chmod is command which changes permission of a file or folder for particular user or group as per instructions provided. chmod command is followed by which level user i.e. user, group or all. After user level we have provide what needs to be done i.e. + for adding and – for removing. Followed by this we have which permission needs to be changed i.e. r for read ,w for write and x for executable.

Sample command

chmod u-r filename
chmod g-x filename

First command will remove read access for user and second will remove group execute for file. Too add above removed permissions

chmod u+r filename
chmod g+x filename

Permissions can also be given numerically

r=4

w=2

x=1

sum of digits for access like for wx will be 2+1=3. Also remember to give excess as three digit number first number for user second for group and third for others.

chmod 740 filename

This command gives complete permissions to user , write and execute to group and no right to other users.

How to give permission to folder

Permission can be given to a folder at time of creating

chmod 740 directoryname

This will give all permission to user , read permission to group and no permission  to other users

Permission to a directory can also be given at time of creation of directory.

mkdir –m 740 xyz

mkdir command is used for here. This will create a directory with all permission to owner, read and write to group and no permission to all users.

But what if you want to change owner of file

Chown command will have to be used to change user ownership of a file, directory. The default owner of a file is user who has created the file. Only root can change ownership of file in Linux.

Change ownership of file in Linux with chown

First open terminal console and change to root user.

To change owner ship of file type chown followed by name of owner and finally provide file name

chown owner file

To change owner ship of directory type chown followed by name of owner and finally provide directory name

chown owner directory

While above two commands can change owner of File/Directory, but you want to change ownership of entire directory i.e. recursively. In this case use following command

chown -R owner directory

How to view permission of file or Directory

Use ls command to list file or directory.

ls -l file/directory

for details of use of ls command click here