This tutorial explains Linux “chmod” command, options and its usage with examples.
Usage:
chmod [OPTION]… MODE[,MODE]… FILE…
chmod [OPTION]… OCTAL-MODE FILE…
chmod [OPTION]… –reference=RFILE FILE…
Here MODE is of two types : 1) symbolic 2) octal
In UNIX/LINUX system each file/directory has three permissions read, write and execute and three classes user(owner), group and others. So each file is provided permissions in combination of class and permissions.
You can see the permissions of a file or directory as:
sanfoundry-> ls -l 1.txt -rw-rw-r-- 1 user_name user_name 0 Jun 14 12:42 1.txt
** Following are the symbolic representation of three different roles and permissions:
% Roles :
# “u” – user
# “g” – group
# o – others
% permissions :
# r – read
# w – write
# x – execute
The format of output of “ls -l” is of total 10 combinations of the symbols. first line is either “-” or “d” showing file or directory.
Then the permissions for user, group, other in the group of 3. So for e.g. “-rw-rw-r–” shows a file with user permissions “rw-” (means read and write but no execute)and so on.
** In the octal representation, “r” has value “4”, “w” has “2” and “x” has value “1”.So octal representation for “rw-” is (4+2+0 =) 6 for user. Hence for “-rw-rw-r–” is 664.
Here’s the listing of example usage of chmod command. :
1. Making read only file(chmod 400 file_name):
To make a file read only means only owner is able to read it, You have to give read permissions to owner and no permissions to others and group, so “400” is the octal representation for this.
sanfoundry-> ls -l 1.txt -rw-rw-r-- 1 himanshu himanshu 0 Jun 14 12:42 1.txt sanfoundry-> chmod 400 1.txt sanfoundry-> ls -l 1.txt -r-------- 1 himanshu himanshu 0 Jun 14 12:42 1.txt
2. Change file permissions and give all permissions for all user+group_other(chmod 777 file/directory_name):
sanfoundry-> mkdir sample_folder sanfoundry-> ls -l drwxrwxr-x 2 himanshu himanshu 4096 Jun 14 12:42 sample_folder sanfoundry-> chmod 777 sample_folder/ drwxrwxrwx 2 himanshu himanshu 4096 Jun 14 12:42 sample_folder
3. Change permissions only for user, group or others(chmod u/g/o+r/w/x file/directory_name):
In order to change permissions only for a user or group or others at a time, symbolic representations is a good choice, Though you can use the octal representation as well.
sanfoundry-> ls -l 1.txt -r-xr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt sanfoundry-> chmod u+w 1.txt sanfoundry-> ls -l 1.txt -rwxr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt
Similarly , it can be done for the directories as well.
sanfoundry-> ls -l drwxr-xr-x 2 himanshu himanshu 4096 Jun 14 12:42 sample_folder sanfoundry-> chmod g+w sample_folder/ sanfoundry-> ls -l drwxrwxr-x 2 himanshu himanshu 4096 Jun 14 12:42 sample_folder
4.To remove file permission using (symbolic mode) chmod command Unix(chmod u/g/o – r/w/x file/directory):
To remove the premissions You have to use “-” symbol instead od “+” in chmod command.
sanfoundry-> ls -l 1.txt -rwxr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt sanfoundry-> chmod u-w 1.txt sanfoundry-> ls -l 1.txt -r-xr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt
5. To remove file permission using (octal mode) chmod command Unix(chmod “numbers” file/directory):
sanfoundry-> ls -l 1.txt -rwxrw-rw- 1 himanshu himanshu 0 Jun 14 12:42 1.txt sanfoundry-> chmod 555 1.txt sanfoundry-> ls -l 1.txt -r-xr-xr-x 1 himanshu himanshu 0 Jun 14 12:42 1.txt
Here , file “1.txt” has permissions 755. If you want to remove the write permissions of a user, You only have to change 7 of user to 5(since 2 for
write is reduced.) These permissions are overwritten on the previous ones.
6. To remove read and write access from file for user/group/others with symbolic(chmod u/g/o – rw file/directory_name):
sanfoundry-> ls -l 1.txt -rwxrwxrwx 1 himanshu himanshu 0 Jun 14 12:42 1.txt sanfoundry-> chmod u-rw 1.txt sanfoundry-> ls -l 1.txt ---xrwxrwx 1 himanshu himanshu 0 Jun 14 12:42 1.txt
7. To remove read and write access from file for all with symbolic(chmod a – rw file/directory_name):
sanfoundry-> ls -l 1.txt -rwxrwxrwx 1 himanshu himanshu 0 Jun 14 12:42 1.txt sanfoundry-> chmod a-rw 1.txt sanfoundry-> ls -l 1.txt ---x--x--x 1 himanshu himanshu 0 Jun 14 12:42 1.txt
8. Setting execute permission only on directories without touching files(chmod a+X *):
For this purpose you can use “X” option instead of “x” option since “x” option will give permissions to files/directories both.
sanfoundry-> ls -l -rw-rw-r-- 1 himanshu himanshu 0 Jun 14 15:53 1.txt -rw-rw-r-- 1 himanshu himanshu 0 Jun 14 15:53 2.txt drwxrwxr-- 2 himanshu himanshu 4096 Jun 14 15:53 sample_folder1 drwxrwxr-- 2 himanshu himanshu 4096 Jun 14 15:53 sample_folder2 sanfoundry-> chmod a+X * -rw-rw-r-- 1 himanshu himanshu 0 Jun 14 15:53 1.txt -rw-rw-r-- 1 himanshu himanshu 0 Jun 14 15:53 2.txt drwxrwxr-x 2 himanshu himanshu 4096 Jun 14 15:53 sample_folder1 drwxrwxr-x 2 himanshu himanshu 4096 Jun 14 15:53 sample_folder2
9. To copy permission from one file to another in Unix(chmod –reference source_file destination_file):
sanfoundry-> ls -l 1.txt 2.txt -rwxrwxrwx 1 himanshu himanshu 0 Jun 14 15:53 1.txt -rw-rw-r-- 1 himanshu himanshu 0 Jun 14 15:53 2.txt sanfoundry-> chmod --reference 1.txt 2.txt sanfoundry-> ls -l 1.txt 2.txt -rwxrwxrwx 1 himanshu himanshu 0 Jun 14 15:53 1.txt -rwxrwxrwx 1 himanshu himanshu 0 Jun 14 15:53 2.txt
Here as we can see, that here with “–reference option” file permissions of 1.txt are given to 2.txt.
10. Changing multiple permissions in a file/directory(chmod Per1,per2,… file/directory_name):
sanfoundry-> ls -l 1.txt -r-xrw-r-x 1 himanshu himanshu 0 Jun 14 15:53 1.txt sanfoundry-> chmod u+w,g+x 1.txt sanfoundry-> ls -l 1.txt -rwxrwxr-x 1 himanshu himanshu 0 Jun 14 15:53 1.txt
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Apply for Programming Internship
- Practice Programming MCQs
- Buy Linux Books
- Buy Information Technology Books
- Apply for Linux Internship