CHMOD and CHOWN- Must Know Linux Commands

This tutorial explains CHMOD and CHOWN commands that are broadly used in Linux.

 

CHMOD and CHOWN

 

The command CHMOD stands for change mode, and this is used to change the permission of a File or Directory.The Command CHOWN stands  for Change Owner and this is used to change the ownership of a File or Directory.

 

Also Read : Linux Tutorial for Beginners

&& Git Tutorial for Beginners

 

Let us understand CHMOD and CHOWN commands in detail.

CHMOD

 

Before starting with CHMOD command, let’s have some discussion about permissions( also called file modes)

Permissions define the permission for the owner of the file (the user), the members of the group who owns the file (the group), and anyone else (others).

 

There are two ways to represents these permissions:  with symbols or with numbers.

 

Symbolic permissions are represented as follows:

x :execute

w :write

r : read

– : No permission

 

Numeric permissions are represented as follows:

4 : execute

2 : write

1 : read

0 : no permission

 

Any file or directories, permissions are set in below order:

 

User Group Other
Read write Execute Read write Execute Read write Execute
                          Diagram (1.1)

Let suppose, we have a file with read, write and execute permission for all ( User , its group and others).

It will be represented as follows:

 

User Group Other
Read write Execute Read write Execute Read write Execute
r w x r w x r w x
                         Diagram (1.2)

You can see the permission by using the following command.

$ ls -l filename.txt

-rwx-rwx-rwx- 1 chandan chandan 0 Jun 5 21:48 filename.txt

 

Here (-)sigh represents that filenae.txt is a file, the first set of rwx represents, the current user(chandan) has read , write and execute permission; the second set of  rwx represents the group associated with the file also has read, write and execute permission and lastly the third set of rwx represents the other users(Everyone) also has read write and execute permission.

Let’s see one more example.

$ ls -l Desktop/

drwxr-x- -x 2 chandan devops 4096 Jun 6 09:01 test/

(To see the permission of all the files and directories in Desktop)

 

In the above example, ‘d’ represents the file(test) is a directory (A special type of file which can contain another files), the first set rwx represents the user(chandan) has full permission i.e. read, write and execute permission; the second set r-x represents the group(devops) associated with the file has read and execute permission but not write permission; the third set – – x represents the other users have only execute permission.

 

Changing Permission of a file or directory

 

Having gathered, knowledge about permission let’s start how to change the permission of a file or a directory. As we already read that there are two ways to represent the permissions, in the same way, there are two ways to change the permission as well:

  1. Symbolic Permission notation
  2. Octal Permission notation

 

Symbolic Permission Notation:

 

In this method, letters u, g and o are used to represent user, group and other while the letters r, w, and x are used to represent read, write and execute respectively.

Syntax to change the permission in symbolic notation:

chmod [u=rwx, g=rwx, o=rwx ] [File Name/Directory Name]

Here rwx can be changed as rw or rx or wx according to the requirement of the permission

e.g:

$ ls -l filename.txt
-rwxrwxrwx 1 chandan chandan 0 Jun 5 21:48 filename.txt

$ sudo chmod u=rwx,g=rw,o=rx filename.txt

 $ ls -l filename.txt

-rwxrw-r-x 1 chandan chandan 0 Jun 5 21:48 filename.txt

 

Octal Permission Notation:

 

As described earlier, numbers 4 , 2 , 1 and 0 are used to represent the read, write and execute and no permission respectively.

 

See the below diagram to understand and set the permission through the octal notation method.

 

User Group Other
Read write Execute Read write Execute Read write Execute
r w x r w x r w x
4 2 1 4 2 1 4 2 1
7 7 7
                         Diagram (2.1)

In this diagram  all the three (user, group and other ) have read, write and execute permission, so if we add the value for read=4 + write= 2 + other = 2, the sum total for all the three would be 7, so we can represent the permission of the file as 777.

Let’s see some more example of this

 

User Group Other
Read write Execute Read write Execute Read write Execute
r w x r w x r w x
4 2 1 4 0 1 0 0 1
7 5 1
                          Diagram (2.2)

In the above diagram the user has read, write and execute permission so the octal permission value would be 4+2+1= 7, while for the group has read and execute permission, and the value would be 4+0+1 =5 similarly for other the value would be 0+0+1= 1. Hence the permission of the file will be represented as 751.

 

Syntax to change the permission in Octal Notation:

chmod [Octal Permission for file] [File/Directory Name]

e.g –

a) If we want to change the  permission as per diagram 2.1 we need to execute below command

$  chmod 777 filename.txt

$ ls -l filename.txt

-rwxrwxrwx 1 chandan chandan 0 Jun 5 21:48 filename.txt

 

b) Similarly, if we want to change the  permission as per diagram 2.1 we need to execute below command.

$chmod 751 filename.txt

$ ls -l filename.txt

-rwxr-x-x 1 chandan chandan 0 Jun 5 21:48 filename.txt

c) You can also use ‘+’ sign to add permission while ‘–‘ Sign to remove permission.

 $ chmod -777 filename.txt && ls -l filename.txt

  ———- 1 chandan chandan 0 Jun 5 21:48 filename.txt

  $ chmod +751 filename.txt && ls -l filename.txt

-rwxr-x-x 1 chandan chandan 0 Jun 5 21:48 filename.txt*

 $ chmod -rwx filename.txt && ls -l filename.txt

———- 1 chandan chandan 0 Jun 5 21:48 filename.txt

 $ chmod +x filename.txt && ls -l filename.txt

—x-x-x 1 chandan chandan 0 Jun 5 21:48 filename.txt*

 

CHOWN

As stated earlier, CHOWN command is used to change the ownership of a file or Directory

 

Changing Ownership of a file or Directory

Syntax

To  Change the both user and group ownership for a file

When Username and Groupname are different or same

 chown [username]:[groupname] [filename/Directoryname]

or

 chown [username].[groupname] filename

E.g-

$ sudo chown chandan:devops filename.txt

$ ls -l

-rwxr-xr-x 1 chandan devops 0 Jun 5 21:48 filename.txt*

$ sudo chown chandan:chandan filename.txt && ls -l filename.txt

-rwxr-xr-x 1 chandan chandan 0 Jun 5 21:48 filename.txt

$ sudo chown chandan.devops filename.txt && ls -l filename.txt

-rwxr-xr-x 1 chandan devops 0 Jun 5 21:48 filename.txt

$ sudo chown chandan.chandan filename.txt && ls -l filename.txt

-rwxr-xr-x 1 chandan chandan 0 Jun 5 21:48 filename.txt

 

When Username and Groupname are same

chown [username.] [File Name/Directory Name]

or

chown {Username:] [File Name/Directory Name]

 

e.g –

$ sudo chown Shreyansh. filename.txt && ls -l filename.txt

-rwxr-xr-x 1 Shreyansh Shreyansh 0 Jun 5 21:48 filename.txt

 

$ sudo chown chandan: filename.txt && ls -l filename.txt

-rwxr-xr-x 1 chandan chandan 0 Jun 5 21:48 filename.txt

 

 

To change the user ownership only

chown [username] [File Name/Directory Name]

E.g

$ sudo chown chandan filename.txt && ls -l filename.txt

-rwxr-xr-x 1 chandan Shreyansh 0 Jun 5 21:48 filename.txt

 

To Change the group ownership only

chown [.groupname] [File Name]

or

chown [:groupname] [File Name]

Eg.-

$ sudo chown .devops filename.txt && ls -l filename.txt
-rwxr-xr-x 1 chandan devops 0 Jun 5 21:48 filename.txt

or

$ sudo chown :chandan filename.txt && ls -l filename.txt
-rwxr-xr-x 1 chandan chandan 0 Jun 5 21:48 filename.txt

 

Changing ownership of a directory recursively

Syntax

chown -R [username]:[groupname] [/Dirctory/ALL files and SubDirectories]

 

 E.g-  If you want to change the ownership of a folder name test and  also  files and folders inside test folder recursively use the following command.

 $ sudo chown -R chandan:chandan test/

 $ ls -l

drwxr-xr-x 2 chandan chandan 4096 Jun 6 20:50 test

$ ls -l test/

-rwxr-xr-x 1 chandan chandan 0 Jun 6 20:50 file1
-rwxr-xr-x 1 chandan chandan 0 Jun 6 20:50 file2
-rwxr-xr-x 1 chandan chandan 0 Jun 6 20:50 file3

 

Changing Ownership of a file/folder using a reference file

 chown  [- -reference=Reference File Name]  [File Name]

e.g –

If you want to change the ownership of filename2.txt same as filename.txt

$sudo chown -reference=filename.txt filename2.txt && ls -l

-rw-r-r- 1 chandan devops 0 Jun 6 19:35 filename2.txt
-rwxr-xr-x 1 chandan devops 0 Jun 5 21:48 filename.txt

 

Click to tweet CHMOD and CHOWN!

I hope you enjoyed this tutorial and learned about CHMOD and CHOWN commands . If you think this is really helpful, please do share this article for CHMOD and CHOWN with others as well. Please also share your valuable feedback, comment, or any query in the comment box. I will really happy to resolve your all queries any.

Thank You

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

You may also like…

2 Responses

  1. sedlav says:

    chown and chmod are not Linux commands they are not part of the Linux Project in any way else they are part of GNU core utils then they are GNU commands.

    • cchakravarty says:

      Hi Sedlav,
      Really appreciate your comment!
      Actually we use this command in Linux that’s why I put this heading so as to help beginners.

Leave a Reply

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