Chmod command in Linux with examples – GeeksforGeeks

In Unix-like operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation for change mode.

Syntax :

chmod [reference][operator][mode] file…

References are used to distinguish the users to whom permissions apply, that is, they are a list of letters that specifies to whom to give permissions. References are represented by one or more of the following letters:

Description of the reference class or owner of the file owner g group users who are members of the file group or other users who are neither the file owner nor the members of the file group a The previous three, same as ugo

The operator is used to specify how the modes of a file should be adjusted. The following operators are accepted

: Operator description + Adds the specified modes to the specified classes – Removes the specified modes from the specified classes = The specified modes must be converted to the exact modes for the specified classes

Note: Putting blank spaces around the operator would cause the command to fail.

The modes indicate which permissions to grant or remove from the specified classes. There are three basic modes that correspond to basic permissions:

r Permission to read the file. w Permission to write (or delete) the file. x Permission to run the file or, in the case of a directory, search for it. Types of permissions we

will change using the chmod command: In the Linux terminal, to see all the permissions for different files, type the ls -l command that lists the files in the working directory in long format. The following figure shows an example for using ls -l and its output :


Let’s take a look at the figure above. For ease of understanding, some columns and rows are removed and additional spaces are added to the permissions column for ease of reading as shown below:

– rw- rw- r- mik mik assgn1_client.c – rw- rw- r- mik mik assgn1_server.c d rwx rwx r-x mik mik EXAM – rw- rw- r- mik mik raw.c – rwx r-x r-x mik mik … so on… The first column represents the file type,

  • that is, whether it is a normal file or a directory where d represents a directory y – represents a normal
  • file.

  • The first set of three letters after the file type indicates what the file owner has permissions to do. For example: In assgn1_client.c, you have the owner’s permission as rw-, which means that the mik owner can only read (r) and write (w) the file but cannot execute (x).
  • Note: The 3rd and 4th columns represent the name of the file owner and the group to which the owner belongs, respectively.
  • The next three letters after the user’s permission are the group permissions. For example: has group permissions such as r-x, which means that Other people in the mik group cannot write (w) the script, but can only read it (r) or execute it (x).
  • Note that when a directory has set x, this takes on the special meaning of “allowed to search this directory”.
  • The last three letters of the permissions column tell us what the “others” can do. The general practice is to protect files from external access so that others cannot write any files or directories. They can read(r) or execute(x). For example: The assgn1_client.c has other permissions such as r- – which means that it can only be read by another (external) access but cannot be written or executed by them.

Now, let’s see how you can use the chmod command to change the access mode of a file.

Example 1: Let’s change the assgn1_client.c permission so that the owner cannot write (w) to the file, but can only read it.

BEFORE: -rw-rw-r- mik mik assgn1_client.c COMMAND: chmod u=r assgn1_client.c AFTER: -r-rw-r- mik mik assgn1_client.c Before :BeforeExample1.jpg

After :AferExample1.jpg

Example 2 :Let’s restrict the permission so that the user cannot search the EXAM directory.

BEFORE: drwxrwxr-x mik mik EXAM COMMAND: chmod u=rw EXAM AFTER: drw-rwxr-x mik mik EXAM

After applying the chmod u=rw EXAM command, the user (owner) cannot change the directory. If the user tries to change the directory, then it displays the message “Permission denied” as shown in the following figure :final2.jpgReference :chmod Wikipedia

Contact US