Understanding the chmod Command in Linux

As a Linux user, you may often need to change the permissions of files and directories on your system. The chmod command is a powerful tool that allows you to modify the access rights of these files and directories.

In this article, we will explore how to use the chmod command in Linux. We will start with the basics of file permissions, and then move on to various chmod commands and their usage.

chmod command in Linux for changing permission

What is the chmod command?


The chmod command is a Linux command that allows users to change the permissions of files and directories. In Linux, file permissions dictate who can read, write, and execute files. The chmod command modifies these permissions, allowing users to grant or restrict access to their files.

Basic syntax of the chmod command


The basic syntax of the chmod command is as follows:

chmod [options] mode file

Here, mode refers to the permissions you want to grant or restrict, and file refers to the file or directory whose permissions you want to modify.

Here, options refer to any additional options that you may want to use with chmod. Permissions refer to the new permissions that you want to assign to the file, and filename is the name of the file or directory that you want to modify the permissions of.

Let’s look at some common options that you can use with chmod:

-v: verbose mode, which displays the changes made to the file permissions
-c: changes mode, which displays the changes made only if there were any
-R: recursive mode, which modifies the permissions of all the files and directories inside the specified directory

Understanding File Permissions

Before we dive into chmod, let’s first understand file permissions in Linux. Every file and directory in Linux has three types of permissions: read, write, and execute.

These permissions are assigned to three types of users: the owner of the file, the group that the file belongs to, and other users.

The permissions can be represented using numbers or letters. The read permission is represented by the letter ‘r’ or the number ‘4’, the write permission is represented by the letter ‘w’ or the number ‘2’, and the execute permission is represented by the letter ‘x’ or the number ‘1’.

PermissionNumeric representation
Read (r)4
Write (w)2
Execute (x)1

The owner of the file is represented by the first digit, the group is represented by the second digit, and all other users are represented by the third digit.

For example, if a file has the permissions rwx-r–r–, this means that the owner can read, write & execute to the file, while all other users can only read the file. This permission can also be denoted by the number 744.

Modifying file permissions with the chmod command


Now that we understand how file permissions work in Linux, let’s take a closer look at how the chmod command can be used to modify these permissions. Here are some common use cases:

Granting or revoking permissions for the owner of a file


To grant or revoke permissions for the owner of a file, you can use the following syntax:

chmod u+[permission] [file]
chmod u-[permission] [file]


Here, u stands for “user,” and [permission] refers to the permission you want to grant or revoke (e.g., r for read, w for write, x for execute).

For example, to grant the owner of a file write permission, you could use the following command:

chmod u+w [file]


To revoke that permission, you could use the following command:

chmod u-w [file]
Understanding the chmod Command in Linux


Granting or revoking permissions for the group of a file


To grant or revoke permissions for the group of a file, you can use the following syntax:

chmod g+[permission] [file]
chmod g-[permission] [file]


Here, g stands for “group.”

For example, to grant the group of a file read permission, you could use the following command:

chmod g+r [file]


To revoke that permission, you could use the following command:

chmod g-r [file]


Granting or revoking permissions for all other users


To grant or revoke permissions for all other users, you can use the following syntax:

chmod o+[permission] [file]
chmod o-[permission] [file]

Here, o stands for “others.”

For example, to grant others execute permission on a file, you could use the following command:

chmod o+x [file]


To revoke that permission, you could use the following command:

chmod o-x [file]

Modifying permissions for multiple users


You can also modify permissions for multiple users at once.

For example, to grant read and write permissions to both the owner and the group of a file, you could use the following command:

chmod ug+rw [file]


To grant read, write, and execute permissions to the owner, and only read and execute permissions to the group and all other users, you could use the following command:

chmod u+rwx,g+rx,o+rx [file]

One of the best use cases for this is to grant or revoke execute permission from a bash script or file in Linux. Just use `chmod -x file` and execute permission is gone from the file or a script.

Modifying permissions using octal values


In addition to modifying permissions using symbolic notation, you can also use octal values to modify permissions. In this notation, each permission is represented by a three-digit number.

The first digit represents the permissions for the owner, the second digit represents the permissions for the group, and the third digit represents the permissions for all other users. Each digit is calculated by adding up the values for the permissions you want to grant or revoke.

For example, to grant read, write, and execute permissions to the owner, and only read and execute permissions to the group and all other users, you could use the following command:

chmod 755 [file]
chmod 755 in linux


In this case, the 7 represents rwx (read, write, and execute) for the owner, the 5 represents r-x (read and execute) for the group and other users.

chown Recursive

If you want to change the permission of multiple files present in a directory, you can do it using the -R option which will change the permission of all the files and folders recursively.

For example: if you want to give write & execute permission to all the members of group, you can use the below chmod command in Linux:

chmod -R g+rx directory
# Output 1
➜  linux_st ls -lh data 
total 0
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file1
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file2
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file3
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file4

# Output 2
➜  linux_st chmod -R g+rx data 
➜  linux_st ls -lh data       
total 0
-rw-r-xr--  1 st  staff     0B Mar 24 23:42 file1
-rw-r-xr--  1 st  staff     0B Mar 24 23:42 file2
-rw-r-xr--  1 st  staff     0B Mar 24 23:42 file3
-rw-r-xr--  1 st  staff     0B Mar 24 23:42 file4

# Output 3
➜  linux_st chmod -R g-x data 
➜  linux_st ls -lh data      
total 0
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file1
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file2
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file3
-rw-r--r--  1 st  staff     0B Mar 24 23:42 file4

The above output of the Linux terminal shows how to change file permission in Linux recursively using chmod command.

The first output shows the original permission, the second output shows the new permission with execute permission added to the group recursively and the third output shows permission on files without the execute permission as the command `chmod -R g-x data` removed that permission recursively from the data directory.

Conclusion


In this article, we provided a comprehensive guide to using the chmod command in Linux. We discussed the basic syntax of the command, as well as the different ways in which file permissions can be modified.

With this knowledge, you can now effectively manage file permissions on your Linux system, and ensure that your files and directories remain secure.

Buy me a coffeeBuy me a coffee

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.