top of page

chmod command in Linux with examples

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

The name is an abbreviation of change mode.

Syntax :

chmod [reference][operator][mode] file... 
The references are used to distinguish the users to whom the permissions apply i.e. they are list of letters that specifies whom to give permissions. The references are represented by one or more of the following letters:

Reference   Class     Description
u          owner      file's owner

g          group      users who are members of
                      the file's group

o          others     users who are neither the
                      file's owner nor members of 
                      the file's group

a          all       All three of the above, 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 modes specified are to be made the exact modes for the specified classes

The modes indicate which permissions are to be granted or removed from the specified classes. There are three basic modes which correspond to the basic permissions:

r       Permission to read the file.
w       Permission to write (or delete) the file.
x       Permission to execute the file, or, in
        the case of a directory, search it.
  • The very first column represents the type of the file i.e. is it a normal file or a directory where d represents a directory and – represents a normal file.

  • The first set three letters after the file type tell what the Owner of the file, have permissions to do. For example: In assgn1_client.c, has owner’s permission as rw-, which means the owner mik can only read(r) and write(w) the file but cannot execute(x).

  • Note: The 3rd and 4th columns represents the name of the owner of the file and the group to which the owner belongs respectively.

  • The next three letters after the user’s permission are the group’s permissions. For example: header.sh has group permissions as r-x, which means Other people in the mik group can not write(w) the header.sh script but can only read(r) or execute(x) it.

  • Note that when a directory has the x set, this takes the special meaning of “permitted to search this directory”.

  • The last three letters in the permissions column tell us what the “others” may do. The general practice is to protect the files from external access so that others can’t write any files or directories. They may read(r) or execute(x) it. For example: The assgn1_client.c has others permission as r- – which means it can only be read by other(external) access but cannot be written or executed by them.

Example 1 :

Let’s change the assgn1_client.c permission so that the owner cannot write(w) in 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 :

After :






 
 
 

Recent Posts

See All
What Is A VPN?

VPN software protects your information by masking your device’s IP address. The software encrypts your data and routes it through...

 
 
 

Comments


bottom of page