How to Change Permission of File in Linux using Chmod Command


Linux is a multiuser operating system where many number of users can access different files and directories at the same time.

Access to all the files is managed using file permission, attribute, and ownership.

Ownership makes sure which user(s) can access, and file permission ensures what operations can be performed by different group of users.
Read, write execute are the available operations on a file or directory.

In this guide I will show you how to change file permission in Linux using chmod command.

But before that let’s understand file permission scheme in Linux.

How to Know File Permissions in Linux

Type ls -l or ls -lah on your terminal.

It will display the files / directories inside the current directory and the file permissions associated.

For better understanding look at the terminal screenshot below:

You can see the permissions are given in the red highlighted part for corresponding files and directories.

Or you can also check the permission for and individual file or directory by typing ls -l filename in your terminal.

Understanding the ls Command Output of a File

Here is the detail of the information showing about filename in the terminal

-rw-r--r-- is permission for different users of the file (We will further discuss it in later part). In the next column, 1 is the number of hard links to the file.

lalit lalit is file owner and group respectively.
20 is the size of the file.
Sep 4 is date and 12:23 is the time of file creation.
And the final column contains the name of the file.


Understanding File Permission Modes

A file or directory can be accessed in the following modes.

r (read) – It shows the respective user has permission to read a file.

w (write)- A file granted with write permission can be edited.

x (execute)- A file with this permission can be run or executed. And a program file must be granted with x permission in order to execute it.


Change File Permission Using chmod Command

To change the file and directory permission, we have to use chmod(change mode) command. The owner who created the file can change the permission for user, group, or other users by adding(+) or removing(-) the read, write, or execute permissions.

There are two ways to use chmod command –

1. Symbolic Method –

In this method, different symbols for access class i.e user(u), group(g), and others(o), operators and file access mode i.e read, write and execute are used with chmod command. Following is the list of symbols that can be used in this method

  • Symbols for access class
    u (user)
    g (group)
    o (other)
    a (used for all of above)
  • Symbols used for the operator are-
    + (add)
    - (remove)
    =(set exact access)
  • Symbols for file access mode
    r (read)
    w (write)
    x (execute)

Here are some example of using chmod in symbolic method-

$ chmod g+w filename

In this example, we are going to add write(w) permissions to group(g) for the filename. Now a group user can edit the filename.

$ chmod a-w filename

In this example write(w) permission will be removed for all(a) the access types.

$ chmod g-w+x filename

In the above example write(w) permission will be removed and execute(x) permission will be added for group users.

$ chmod -R o+r directory_name

where -R flag is used to change permission recursively in all the subdirectories under a specified directory, please. Note that you should have execute(x) permission for a directory in order to access it or use it with the cd command.

2. Absolute Method-

In this method, we use a set of numbers or sum of that number to change the permission of a file or directory

PermissionNumber
r(read)4
w(write)2
x(execute)1

For example –

$ chmod 751 filename

user have all (4+2+1=7) the permissions, group have read and execute(4+0+1=5) permissions, and other users have execute (0+0+1=1) only permission.


Conclusion

chmod command as the name suggests, is used to Change Modes or permissions of a file in Linux.

Modes are basically the permission associated with a file.

If you still have some doubt or face any problem executing this command effectively, leave a comment so that I can help you as soon as possible.

I hope this was a useful information for you. For more Linux guide, browse different categories inside Explore Linux.