Learn Usage of chmod (Change Mode) Command in Linux

Since Linux is a member of the Unix-like operating system family, it has inherited some Unix rules like the way it deals with system/user files & directories.

Linux operating system makes use of certain flags which determine which system user has access to which files/directories and how the same users can manipulate those files through various read or write operations.

[ You might also like: 3 Useful Tips on How to Use History Command in Linux ]

These flags define the mode of access (permissions) which further attribute to the Linux system’s file access rules/restrictions.

Chmod Command Usage

As this article takes us closer to the implementation and usage of the chmod (change mode) command, we will be able to inherently understand its usefulness in restricting file access on the Linux operating system environment.

This in-built Linux command adheres to a very specific command syntax as demonstrated below:

$ chmod options permissions file_name

From the above command syntax, the options parameter is not mandatory. Failing to use it will redirect the chmod command to the permissions parameter.

The permissions parameter applies to the file_name owners (user, group, or others). The representation of the permissions parameter under the chmod command is either through octal numbers (0, 1, 2, 3, 4, 5, 6, 7) or alphanumeric characters (symbols).

Symbolic vs Octal Permissions Notation

Supposing we have a file on a Linux system called my_linuxshelltips.

View Linux File Content
View Linux File Content

Let’s also assume that we want to apply the following permissions to this file:

  • read, write, and execute (for the current system user/file owner).
  • read and execute (for the group members controlled by the file owner).
  • read (for other users that might have access to the system).

The chmod command with symbolic permissions notation can be represented as follows:

$ sudo chmod u=rwx,g=rx,o=r my_linuxshelltips

From the above command:

  • u → user
  • g → group
  • o → other
  • r → read
  • w → write
  • x → execute

The chmod command with octal permissions notation can be represented as follows:

$ sudo chmod 754 my_linuxshelltips 

From the above command:

  • 7 represents user permissions
  • 5 represents group permissions
  • 4 represents other permissions

Each of the digits 7,5, and 4 are a combined sum of the numbers 4, 2, 1, and 0 where:

  • 4 → read
  • 2 → write
  • 1 → execute
  • 0 → no permission

In this case:

  • 7 → 4+2+1
  • 5 → 4+0+1
  • 4 → 4+0+0

To view the applied chmod permissions on this file (my_linuxshelltips), we will use the following command.

$ ls -l my_linuxshelltips
View Linux File Permissions
View Linux File Permissions

The above file belongs to the user root and group root. The other users have no access to this file. The user permission is rwxr and the group permission is xr.

Chmod Command Examples

If we want everyone to read and write the my_linuxshelltips file, we would implement the following command:

$ sudo chmod a=rw my_linuxshelltips
OR
$ sudo chmod 666 my_linuxshelltips

If we want to assign a group and others to read and execute while the file owner can read, write, and execute:

$ sudo chmod 755 my_linuxshelltips

Grant file ownership to all users:

$ sudo chmod u+s my_linuxshelltips

Revoke file ownership from all users:

$ sudo chmod u-s my_linuxshelltips

Limit file owner to read and write permissions only.

$ sudo chmod u=rw my_linuxshelltips

Recursive read, write, & execute permissions for the user; read & execute permissions for other users and group members in regards to a given directory (including its files and sub-folders).

$ sudo chmod -R 755 LinuxShellTips_

Group members and other users can read only while file users can read and write.

$ sudo chmod 644 my_linuxshelltips

The chmod command gives you unlimited power and control of Linux system users’ access to files, directories, and their associated permissions.

[ You might also like: Chown Command Examples and Chgrp Command Examples ]

This command is a great way of stretching your Linux administration muscles, especially when dealing with a large set of distinct users accessing a growing network.

Got something to say? Join the discussion.