How to Change Linux files and directory permissions

Share this article

chmod command

In Linux to change file or directory permissions, we will use chmod command.

chmod command can be used by the root user or the file’s owner only. Its syntax is shown below. For more information about chmod command refer to command’s manpage.

chmod [reference][operator][mode] file... 

There are two ways for passing arguments for permissions. Let’s first understand what type of arguments are those.

chmod command arguments

Use binary refrences

In Binary numbers systems, we use base 2 numbers, which means we represent a number in the form of 0 and 1s. The binary system also works the same way as decimal number systems. The only difference is that instead of multiplying the digit by a power of 10, we multiply it by a power of 2. For more understanding about binary number, systems use this refrence.

In the below example we have 101 as a binary number and equivalent decimal number.

Binary number :  1     0     1
Decimal number: 2^2 + 2^1 + 2^0 = 5

In the previous article we understood about the linux and file permissions. See the following picture.

We have divided permission into three classes and each class has 3 permissions read, write and execution respectively. Now in binary refrence 3 permissions are defined by binary numbers.

places      :  4(2^2)  2(2^1)   1(2^0)
Permissions :  read    write    execute
  • 1 (2^0) place represents execute permission
  • 2 (2^1) place represents write permission
  • 4 (2^2) place represents read permission

chmod permission arguments we will pass Decimal number for each class (user,group and others ) which we create on the basis of above explained concept. Look at the below table to see the what number represent which permission.

Binary RepresentationDecimal representationPermissionsRemark
0000No permissions assigned
0011–xonly execute permission
0102-w-only write permission
0113-wxwrite and execution permission
1004r–read only permission
1015r-xread and execution permission
1106rw-read and write permission
1117rwxread, write and execute permission
Permission table

Examples:

In the below example first we listed permission on updated.py file.

For now updated.py file has following permission according to the users

  • test user has read and write operation.
  • test group has again read and write operation.
  • others have only read operation
test@test:~/exp$ ls -l
total 40
-rw-rw-r-- 1 test test 38560 Aug  8 16:16 updated.py
test@test:~/exp$ 

In the following example we have added read, write and execute permission for test user and ommited all the permission for all the users and test group. By looking at the permission table lets assign the decimal number for each class.

  • test user have all the permissions: 7
  • test group have no permissions: 0
  • Others also have no permissions: 0
test@test:~/exp$ chmod 700 updated.py 
test@test:~/exp$ ls -l 
total 40
-rwx------ 1 test test 38560 Aug  8 16:16 updated.py
test@test:~/exp$ 

In next example Assign all the permssion to all the users.

  • test user have all the permissions: 7
  • test group have all the permissions: 7
  • others also have all the permissions: 7
test@test:~/exp$ chmod 777 updated.py 
test@test:~/exp$ ls -l 
total 40
-rwxrwxrwx 1 test test 38560 Aug  8 16:16 updated.py
test@test:~/exp$ 

Defining Explicit Permissions

For defining explicit permission, symbolic method/UGO syntax is used.
UGO stands for:

  • u for user
  • g : for group
  • o : for others
    Also,
  • a : all three of the above

With UGO syntax we use three operators to set the permissions (r,w and x):

  • + : Adds a permission
  • – : Removes a permission
  • = : Sets a permission

Examples

In the following example, we are changing permission on all to read and write. Earlier they have all the permissions.

test@test:~/exp$ ls -l 
total 40
-rwxrwxrwx 1 test test 38560 Aug  8 16:16 updated.py
test@test:~/exp$ 
test@test:~/exp$ chmod  a=rw  updated.py 
test@test:~/exp$ ls -l 
total 40
-rw-rw-rw- 1 test test 38560 Aug  8 16:16 updated.py
test@test:~/exp$ 

Adding permission for the test user for execution.

test@test:~/exp$ chmod  u+x  updated.py 
test@test:~/exp$ ls -l 
total 40
-rwxrw-rw- 1 test test 38560 Aug  8 16:16 updated.py
test@test:~/exp$ 

Leave a Comment

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