How to change the permission of a file in Linux?

In the last post we discussed about the basic file permission. Now lets see how to change the permission of a file or a directory in Linux. To change the access rights we use the chmod command.
The syntax for chmod is
chmod [option] mode filename

The Linux man page defines chmod as  'change file mode bits'. Here mode or the access mode is nothing but the permission of a file. The file's mode can be represented using  either an octal number method or a symbolic method. Now lets see the two representations one by one.

Symbolic  modes.


Read, Write and Execute are the three permission types associated with a file in Linux. Using Symbolic mode Read, Write and Execute are represented with r, w and x character. The following tables represents symbolic mode equivalent of each permissions.

PermissionSymbolic mode
Read, Write and Executer w x
Read and Writer w –
Read and Executer – x
Read Onlyr – –
Write and Execute– w x
Write Only– w –
Execute Only– – x
Nil– – –


Octal (Numerical) method


Octal modes are methods by which each of the 3 permissions are represented by a number. i.e. 


Read (r) = 4
Write (w) = 2
Execute (x) = 1


Using these values we can determine the read, write and execute permission of a file. The permission of a user on a file can be numerically defined by adding the values of each permission types. The below table displays  permission to number mapping


Permission Symbolic mode Calculation Numeric Representation
Read, Write and Execute r w x 4 + 2 + 1 7
Read and Write r w – 4 + 2 + 0 6
Read and Execute r – x 4 + 0 + 1 5
Read Only r – – 4 + 0 + 0 4
Write and Execute – w x 0 + 2 + 1 3
Write Only – w – 0 + 2 + 0 2
Execute Only – – x 0 + 0 + 1 1
Nil – – – 0 + 0 + 0 0

Now lets observer the below screen shot of ls -l output of a file named my_executable.









From the ls -l output we can find the access rights of each users. The symbolic and numeric permission mode of each users are represented in the below table.


User Description Symbolic mode Numeric Mode
calypso Owner r w x 7
IT_Dept Group r w – 6
Other Other Users r – – 4

Access rights of a file can be changed with chmod command using either Symbolic mode or Numeric Mode. Now lets change the permissions on the my_executable file so that the group can only read and execute the file, other users can only execute the file and there is no change to owner's permission. So the new permission will look like


User Description Symbolic mode Numeric Mode
calypso Owner r w x 7
IT_Dept Group r – x 5
Other Other Users r – – 1

To change the permission using symbolic mode, type
chmod g-w+x,o-r+x my_executable
where g-w+x means removing write permission and adding execute permission to the group. Similarly o-r+x means removing the read permission and adding execute permission to other users. The permissions of the group and other users are separated by a comma(,). The permission for owner (u) is omitted since there is no change in owner's permission

To change the permission using Numeric mode, type
chmod 751 my_executable
The numeric mode,751, in the above example can be divided into 3 fields. The first field (7) represents the read, write and execute permission of owner, the second field (5) represents the read and execute permission of the group, third field (1) represents the execute permission of other users.



Comments

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux