×
chmod and chown command Linux

The chmod and chown commands are powerful and most popular command line tool that can be used to control access to files in Linux-based operating systems.

The chmod also called change mode that is used to change permissions of a given file according to a certain mode.

The chown command stands for “change owner” is used to change the owner of a given file or folder.

In this tutorial, we will show you how to use the chown and chmod command through simple examples.

Working with chown command

The syntax of the chown command is shown below:

chown [OPTIONS] [USER][:GROUP] FILE(s)

Where USER is the name of the user, GROUP is the name of the group and FILES is the name of the one or more files.

You can find more information about chown command with the following command:

chown --help

First, lets create a test directory with test1.txt and test2.txt file:

mkdir test
touch test/test1.txt
touch test/test2.txt

Now, list permissions for test1.txt and test2.txt files with the following command:

ls -l test/

You should see that the user and group owner of the files is root:


total 0
-rw-r--r-- 1 root root 0 Aug 26 16:41 test1.txt
-rw-r--r-- 1 root root 0 Aug 26 16:41 test2.txt

Next, change the user ownership of this files to vyom user with the following command:

chown vyom test/test1.txt
chown vyom test/test2.txt

Now, check the permissions of the files:

ls -l test/

You should see the following output:


total 0
-rw-r--r-- 1 vyom root 0 Aug 26 16:41 test1.txt
-rw-r--r-- 1 vyom root 0 Aug 26 16:41 test2.txt

Next, change the group ownership of this files to vyom group with the following command:

chown :vyom test/test1.txt
chown :vyom test/test2.txt

Now, check the permissions again with the following command:

ls -l test/

You should see the following output:


total 0
-rw-r--r-- 1 vyom vyom 0 Aug 26 16:41 test1.txt
-rw-r--r-- 1 vyom vyom 0 Aug 26 16:41 test2.txt

You can use -R option with chown command to recursively change ownership of directories and sub-directories.

For example, to change the user and group ownership of the directory test and all sub-directories to vyom user and group, run the following command:

chown -R vyom:vyom test

Working with chmod command

The syntax of the chown command is shown below:

chmod [OPTIONS] [PERMISSIONS] FILE(s)

Or

chmod USER:GROUP:OTHERS FILE(S)

PERMISSIONS define the permissions for the user, group and others. You can represent these permissions with symbols or with octal numbers.

The following list describes a list of several numerical permissions that can be used to set permissions for user, group and others.

7 : rwx, read, write, and execute
6 : rw-, read and write
5 : r-x, read and execute
4 : r–, read-only
3 : -wx, write and execute
2 : -w-, write only
1 : –x, execute only
0 : —, none

For more information about chmod syntax run the following command:

chmod --help

Let’s use chmod with some examples

First, create a directory with name permissions and create files file1, file2 and file3 inside the directory:

mkdir permissions
touch permissions/file1
touch permissions/file2
touch permissions/file3

Next, change the permission of the file file1 so that everybody has full access to it.

chmod 777 permissions/file1

Here, the first 7 set the permissions for the user(owner), the second 7 sets the permissions for the group, and the third 7 sets the permissions for others.

You can see the permission of file1 with the following command:

ls -l permissions/file1


-rwxrwxrwx 1 root root 0 Aug 27 11:08 permissions/file1

You can also set the above permissions using the letters as shown below:

chmod u=rwx,g=rwx,o=rwx permissions/file1

Next, set the permissions of file2 so that only owner of the file2 have full access:

chmod 700 permissions/file2

Now, check the permission with the following command:

ls -l permissions/file2

Output:


-rwx------ 1 root root 0 Aug 27 11:08 permissions/file2

Next, set the permissions of the file3 so that owner of the file has full access, members of the group and others have read and execute permission:

chmod 755 permissions/file3

Now, check the permission with the following command:


ls -l permissions/file3

Output:


-rwxr-xr-x 1 root root 0 Aug 27 11:08 permissions/file3

You can also use symbols to allow and deny specific permissions of the file.

For example deny read permission of file1 to everyone:

chmod a-r permissions/file1

To allow execute permission of file1 to everyone:

chmod a+x permissions/file1

To allow write permission of file1 to the owner of the file:

chmod u+w permissions/file1

Symbol + is used to add specific permission to the file and – is used to remove permission to the file.

You can use -R option with chmod command to recursively change permissions of directories and sub-directories.

For example, give full access to the directory permission recursively with all sub-directories and files:

chmod -R 777 permissions

You can also set the sticky bit permission to file so that only the file owner the root user can delete the file.

You can set the sticky bit permission to file1 with the following command:

chmod 1755 permissions/file1

You can now check the permission of the file1 as shown below:

ls -l permissions/file1

You should see that t flag is added to the file1:

-rwxr-xr-t 1 root root 0 Aug 27 11:08 permissions/file1