The chmod (change mode) command in Linux is used to change the access mode of a file, based on the type of user accessing the file and the type of permission associated with accessing the file.

File Permissions and User Groups

For understanding what the types of permissions and the types of users are, let us first go back to the ls command, which can list all the files, along with their permissions in the current directory.

Ls File Permission Example

The file permission bits

The first column for every file corresponds to a sequence of 10 characters (bits), as you can see. The first bit is for checking if it is a directory or not (the d bit).

d -> Checks if the node is a directory or not.

The following 9 bits represent the permission of three types of user groups, namely Owner, Group and Other.

And for every user group, there are 3 bits that correspond to the file permissions, ( rwx , in that order).

r -> The user group can read the file/directory.

w -> The user group can write to the file/directory.

x -> The user group can execute the file/directory.

-rw-r--r-- 1 root root 66 Dec 18 20:25 ListText

The first three characters represent permissions of the Owner user group. The Owner is the user that created the file. For the file ListText , the sequence rw- means that the owner can read and write, but cannot execute the file.

The next three characters are for the own Group (all users in the same group as the file). This means that any user in this category can only read the file, but cannot write or execute it.

The next three characters/bits are for Others. Since this is also r-- , all users in this category can only read the file.

From the rwx bits, let us now look at how we can change them using chmod .

Setting file permission bits through chmod

Now that we know what file permissions are, we can set and modify them using chmod . Let us look at the ways through which we could change them.

1. Change absolute file permissions

The file permission bits rwx can be represented as an Octal Character. This enables us to set the absolute file permission of a file using chmod .

We can convert the rwx to an Octal Character, using the below rules:

r = 4 if the read bit is set. Otherwise, r = 0

if the read bit is set. Otherwise, w = 2 if the write bit is set. Else, w = 0

if the write bit is set. Else, x = 1 if the execute permission bit is set. Else, x = 0

if the execute permission bit is set. Else, The Octal character is denoted by r + w + x

This means that the Octal character for the Owner group of the ListText file is r + w + x = 4 + 2 + 0 = 6

Format: chmod 755 filename.txt

Here, 755 correspond to any three Octal Characters, corresponding to the 3 types of user groups.

Now, let us make our ListText file be executable by only all users in Group , keeping others constant. So the absolute bits are: rw-r-xr-- , which corresponds to the Octal characters 654 . Therefore, our command must be:

root@Ubuntu:~# chmod 654 ListText

Chmod Linux Absolute Mode Example

As you can see, our file is now executable to all users in the own Group.

Now, let us look at another way of changing the file mode through Relative Permissions.

2. Setting Relative File Permissions

Since looking up the file permission bits and calculating the absolute value becomes tedious, it is sometimes easier to just work with relative file permission bits when using chmod . The + and - operators are used for this purpose, to set and unset file modes with respect to the current mode.

To change relative file permissions, we use the following syntax:

Format:

chmod +mode filename.txt sets relative permissions for the current user.

chmod group+mode filename.txt sets relative permissions for group

The group bit follows the following rules:

u -> Stands for the user group

g -> Stands for the own group

o -> Stands for others

a -> Stands for all user groups

Now that we know the basic syntax, let us now set the write permission for the group others for our old file ListText using relative modes.

root@Ubuntu:~# chmod o+w ListText

Chmod Linux Relative Permission Example

Similarly, to revert back to the previous mode, we simply use:

root@Ubuntu:~# chmod o-w ListText

The - operator unsets the w bit for the others group.

Similarly, to set execute permission for all users, simply use:

root@Ubuntu:~# chmod a+x ListText

NOTE: There is no change if the bit was already set. Similarly, if you try to unset a bit already unset, it simply remains unset. chmod also never changes the permissions of symbolic links, so any symbolic links created (both soft and hard links) will remain unaffected

Linux chmod Command Options

Although not used very often, there are certain options associated with chmod , which are listed in the table below:

Option Description -c , --changes Similar to verbose, but reports only when a change is made --no-preserve-root Does not treat ‘/’ specially (the default) --preserve-root Fails to operate recursively on ‘/’ -f , --silent , --quiet Suppresses most error messages -v, --verbose Operates in verbose mode --reference=RFILE Uses RFILE ‘s mode as a reference instead of MODE values -R, --recursive Changes files and directories recursively --version Output version and exit

Conclusion

In this article, we learned about the chmod command for changing the file permissions/modes. This is useful if you want to restrict/allow access of the file to any particle user group. We also learned the way to change the absolute and relative file permission bits, which allows different ways of changing the file permissions.

References