chmod Command in Linux with Examples
chmod Command
chmod command means change mode. There are three basic modes to files and directories: read, write, and execute. Additionally, each of these modes can be applied to the user, the group, or others.
Representations used for Ownerships
u - User
g - Group
o - Others
Representations used for File permission
r &ndash Read (value is 4)
w &ndash Write (value is 2)
x &ndash Execute (value is 1)
7 = 4+2+1 (read+write+execute)
6 = 4+2 (read+write)
5 = 4+1 (read+execute)
4 = 4 (read)
3 = 2+1 (write+execute)
2 = 2 (write)
1 = 1 (execute)
Syntax
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
Applying single permission to a file/directory
We are going to add the executable permission for the user using chmod command.
Example
First let we list the files present in the current directory.
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-rw-r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod o+w file2 [user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-rw-rw- 1 user1 user1 29 Feb 25 14:46 file2
You can see the others permission of the file has been changed from r to rw
Applying multiple permissions for different ownership in a single command
We are going to change the file permission of the user & group in single commands using chmod command.
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod u+x,g+x file1 [user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rwxrwxr-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2
Now in the above output, the file permission of the user and the group has been changed.
Removal of permission for both file and directory in a single command
Using this chmod command we can modify the permissions of a file and a directory in a single command
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rwxrwxr-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod u-x,g-x file1 dir1/ [user1@linuxhelp Desktop]$ ll total 12 drw-rw-r-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2
Now the file permissions of the user and group is removed from rwx to rw
Removal of permission for file/directory
Here we are just removing the multiple permissions of a single file using chmod command.
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rwxrwxr-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod u-x,g-x file1 [user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2
Removal of permissions of all roles of ownership of a file
Using this chmod command we can remove same permissions of all ownerships.
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod a-r * [user1@linuxhelp Desktop]$ ll total 12 d-wx-wx--x 2 user1 user1 4096 Feb 25 14:45 dir1 --w--w---- 1 user1 user1 21 Feb 25 14:45 file1 --w------- 1 user1 user1 29 Feb 25 14:46 file2
Applying execute permissions only to all directories without making changes in file
The following chmod command make changes only in all directories present inside the target directory and not on files present inside it.
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod a+X * [user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-rw-r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2
Applying permission for a file from same as another reference file
Now we are going to change the file permission of a file as another file by using reference option
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rwxrwxrwx 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod --reference=file1 file2 [user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rwxrwxrwx 1 user1 user1 21 Feb 25 14:45 file1 -rwxrwxrwx 1 user1 user1 29 Feb 25 14:46 file2
In the above output the file permission the file ' sample' is assigned to the file ' example'
Change permission for all ownership on a file/directory
This command is about how to change the permission all role (user, group and others)
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rw-r--r-- 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod a+rwx file1 [user1@linuxhelp Desktop]$ ll total 12 drwxrwxr-x 2 user1 user1 4096 Feb 25 14:45 dir1 -rwxrwxrwx 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2
Apply the permission to all the files under a directory recursively
Now we are going to change the file permission of a directory recursively that is the given file permission is applied to the mentioned directory and their sub-directories.
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxr-xr-x 5 user1 user1 4096 Feb 25 15:29 dir1 -rwxrwxrwx 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod -R o+w dir1/ [user1@linuxhelp Desktop]$ ll total 12 drwxr-xrwx 5 user1 user1 4096 Feb 25 15:29 dir1 -rwxrwxrwx 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ cd dir1/ [user1@linuxhelp dir1]$ ll total 12 drwxr-xrwx 2 user1 user1 4096 Feb 25 15:29 tmp1 drwxr-xrwx 2 user1 user1 4096 Feb 25 15:29 tmp2 drwxr-xrwx 2 user1 user1 4096 Feb 25 15:29 tmp3
Applying permissions on files/directory using the numeric values of the representations
There is another way applying permissions that is by using the permission representation values as in introduction. This can be done by just summing the values of the representations.
Example
[user1@linuxhelp Desktop]$ ll total 12 drwxr-xr-x 5 user1 user1 4096 Feb 25 15:29 dir1 -rwxrwxrwx 1 user1 user1 21 Feb 25 14:45 file1 -rw-r--r-- 1 user1 user1 29 Feb 25 14:46 file2 [user1@linuxhelp Desktop]$ chmod 765 file2 [user1@linuxhelp Desktop]$ ll total 12 drwxr-xr-x 5 user1 user1 4096 Feb 25 15:29 dir1 -rwxrwxrwx 1 user1 user1 21 Feb 25 14:45 file1 -rwxrw-r-x 1 user1 user1 29 Feb 25 14:46 file2