How to change file permissions using chmod command in Linux?

Use

chmod command in linux is used to change permissions of files/directories.

Syntax

chmod [OPTION]… MODE… FILE…

Usage

Before using the command, we need to understand that there are 3 types of members who can access a file/directory: owner(user), group and others. Owner is the user who owns the file or who has created the file. Group means the members of the group to which the owner belongs, and others means all other users in the system. Either it is a file or a directory the method of using chmod remains the same.

There are two ways of using chmod command in linux: relative method and absolute method.

Relative Method

In relative method we consider the existing permissions and either add new permission to them or take back some of the permissions.

chmod command syntax for relative method is

                                chmod WhoWhatWhich file|directory

where

Who -> u (user), g (group), o (others) or a (all)

What -> + (add) , – (remove) or = (set exactly)

Which -> r (read), w (write) or x (execute)

Example

Consider a file “linux.txt” with existing permissions as shown in figure below. The owner does not have execute permission.

Listing file permissions
Listing file permissions

Next, we add execute permission to the user using the relative method.

chmod command in linux
chmod command in linux

Similarly, existing permissions can be withdrawn

                $chmod g-w linux.txt

Withdraws write permission from the group members.

Absolute Method

In absolute method we focus on the ultimate permissions that are required. In this case a three digit octal number specifies the permissions. Each permission has a fixed number assigned to it – 4 for read, 2 for write and 1 for execute. Add up the numbers corresponding to the permission required thus getting the final value. For example, let us suppose that the permissions required on the file linux.txt are

User – read, write and execute (4 + 2 + 1 = 7)

Group – read and execute (4 + 1 = 5)

Others – only read (4)

Hence the command becomes

Absolute method with chmod
Absolute method with chmod

Some useful questions

Q1. Create a file “F1.txt”. Change the permissions to read and write for user, write for group and none for others.

Q2. Create a file “DIR”. Change the permissions to read and execute for user, write and execute for group and only read for others.

Q3. Use relative method to assign read permission to user, group and others on the file F1.txt. Use the command only once.

PPT on chmod command

Video on chmod command

Relative commands

umask

Leave a Reply

Your email address will not be published. Required fields are marked *