Explain the chmod command in Linux | Linux chmod command

Chmod-command-in-Linux

In this blog, we will learn about the chmod command in Linux. If you are thinking about how to modify the file permission then don’t worry chmod command is there to do it. We will learn about chmod in very deep and what ways we can modify the file permission with the chmod command. Let’s get started.

Introduction to chmod command in Linux

The chmod command in Linux changes the access permissions of files and directories.

In the chmod, ch stands for change mode.

The permission set for a file is vital to its basic functionality.

In Linux File System, each file in the Linux file system has a specific owner and can be accessed by multiple users depending on the permissions set. Among the user classes are:

  1. owner – The Owner of the file.
  2. group – The users who are the members of the file’s group.
  3. Others – The users who are neither the file’s owner nor members of the file’s group.
  4. all – All the above.

File permission in Linux follows three basic types:

  1. read (r) – Permission to read the file.
  2. write (w) – Permission to write the file.
  3. execute (x) – Permission to execute the file, or, in the case of the directory, search it.

The operators are used to specify how the modes of a file should be adjusted. The operators are as follows:

OperatorDescription
+This operator adds the specified modes to the specified classes.
Removes the specified modes from the specified classes.
=The modes specified are to be made the exact modes for the specified classes.
Chmod Commands Operators

Note: Putting a blank space(s) around the operator would make the command fail.

The syntax for the chmod command in Linux:

$ chmod [option] [permission] [filename]

Options for the chmod command:

-c, –changes: Similar to the verbose option, but the difference is that it is reported if a change has been made.

-f, –silent, –quiet: It is used to suppress the error messages.

-v, –verbose: It is used to display a diagnostic for every processed file.

–no-preserve-root: It is used for not treating the backslash symbol (‘/’), especially the default.

-R, –recursive: It is used to change files and directories recursively.

Octal Method of setting the permission

ReadWriteExecuteDecimalPermission
0000No Permission
0011Only Execute
0102Only Write
0113Write and Execute
1004Only Read
1015Read and Execute
1106Read and Write
1117Read, Write and Execute.
Octal method of setting the permission

File Permission Syntax

If you are a beginner, you can get confused with the different types of letters used to set the file permissions. So, before proceeding further with the chmod command, let’s understand the file permission syntax:

To set the permission of a file or directory, we have to specify the following tips:

  1. Who: Who we are i.e; user.
  2. What: What change are we going to make (modifying the permissions like adding or removing.)
  3. Which: Which of the permissions?

The permission statement is represented in indicators such as u+x or u-x.

Here,

u stands for users.

x stands for the executable.

+ stands for adding permission.

– stands for removing the permission.

The user value can be:

u: owner of the file.

g: group member.

o: others

a: all

The permission types can be r,w, and x.

Setting and Updating the Permissions of files in the chmod command

To see the permission of the file, first, we have to check the permission of the file which means that the file has already permission.

So type the command ls -l in the terminal.

$ ls -l

So, let’s see an example of the chmod command.

$ touch chmod_demo1.sh

$ ls -l
total 4

-rw-rw-r-- 1 binfintech binfintech    0 Jan 22 18:56 chmod_demo1.sh
drwxrwxr-x 2 binfintech binfintech 4096 Jan 22 19:04 dir_chmod

Let us take at the above figure. In order to make things simpler to grasp, certain columns and rows have been removed, and the permissions column has additional spaces added to make it easier to read, as shown below:

  Permissions               Owner          Groups         File_name
   
-  r w -    r w -   r - -    binfintech    binfintech    chmod_demo1.sh   

d  r w x    r w x   r - x    binfintech    binfintech    dir_chmod

Let’s understand the above permission:

a) The first column represents the type of file. i.e is it a normal file or a directory,

where

d represents the directory,

dash (-) represents the file.

b) The first set of three letters after the file type represents the owner of the file, have permission to do.

For example, In chmod_demo1.sh, the owner has the permission of rw-, which means that the owner can read(r) and write(w) the file but cannot execute(x) it.

c) The next column represents the group’s permissions.

For example: In chmod_demo.sh, the group has the permission of rw-, which means that the owner can read(r) and write(w) the file but cannot execute(x) it.

When a directory has the x set, this takes the special meaning of “permitted to search this directory”.

d) The last three letters in the permissions column tell us what the “others” may do. The standard procedure is to block outside access to the files so that nobody else may add new files or directories.

For example: In chmod_demo.sh, the others have the permission of r–, which means it can only be read by other(external) access but cannot be written or executed by them.

e) The next two columns represent the owner of the file and the group to which the owner belongs respectively.

Now let’s see how we can modify the file permission

$ ls -l

total 4
-rw-rw-r-- 1 itsvek03 itsvek03    0 Jan 22 18:56 chmod_demo1.sh


$ chmod u+x chmod_demo1.sh 

$ ls -l
total 4
-rwxrw-r-- 1 itsvek03 itsvek03    0 Jan 22 18:56 chmod_demo1.sh

Before using the chmod command, the user does not have permission to execute the file.

chmod u+x chmod_demo1.sh

This command tells that, please add execution permission to the users.

That’s why we can see x in the chmod_demo1.sh file.

Let’s see the second example of that remove the permission of the group.

$ ls -l
d rwx rwx r-x 2 itsvek03 itsvek03 4096 Jan 22 19:04 dir_chmod

$ chmod g-x dir_chmod/

$ls -l

d rwx rw- r-x 2 itsvek03 itsvek03 4096 Jan 22 19:04 dir_chmod

You can see in the above terminal, that we have successfully removed the permission for the execution of the directory for the group.

FAQ

What is the chmod command in Linux?

The chmod command in Linux changes the access permissions of files and directories.
In the chmod, ch stands for change mode.
The permission set for a file is vital to its basic functionality.

What is chmod 777 command?

In the 777 number, the first number 7 can be defined as permission provided to the owner, the second number 7 can be defined as the group permission, and the third number 7 can be defined as the permission of every other.

In other words, 777 can be represented as:
7 users execute, write, and read
7 groups execute, write, and read
7 others execute, write, and read

What is chmod 755 command in Linux?

In the 755 number, the first number 7 can be defined as permission provided to the owner, the second number 5 can be defined as the group permission, and the third number 5 can be defined as the permission of every other.
In other words, 777 can be represented as:
7 users execute, write, and read
5 groups execute and read
5 others execute and read

What does chmod 644 mean in Linux?

In the 644 number, the first number 6 can be defined as permission provided to the owner, the second number 4 can be defined as the group permission, and the third number 4 can be defined as the permission of every other.
In other words, 755 can be represented as:
6 users write and read.
4 groups read-only.
4 others are read-only.

By Vivek Maurya

Write blogs related to Ethical hacking, Computer networks, Linux, Penetration testing and Web3 Security.

Leave a Reply

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