This tutorial explains Linux “chmod” command, options and its usage with examples.
chmod command is used to change file/directory mode bits or permissions.
Syntax:
<strong><em>chmod [OPTION]... MODE[,MODE]... FILE...</em></strong> <strong><em>chmod [OPTION]... OCTAL-MODE FILE...</em></strong> <strong><em>chmod [OPTION]... --reference=RFILE FILE...</em></strong>
Here, MODE can be of two types:
- Symbolic Mode
- Octal Mode
In UNIX/LINUX system each file/directory has three permissions read, write, and execute. These permissions are categorized into three classes: user (owner), group, and others. To view and modify these permissions, you can use the chmod command.
You can check a file or directory’s permissions as follows:
sanfoundry-> ls -l 1.txt -rw-rw-r-- 1 user_name user_name 0 Jun 14 12:42 1.txt
Symbolic Representation:
Symbolic representation defines three different roles and their respective permissions:
- Roles:
- “u” – user
- “g” – group
- “o” – others
- Permissions:
- “r” – read
- “w” – write
- “x” – execute
The output format of “ls -l” consists of 10 symbols. The first line begins with “-” or “d,” indicating a file or directory. Following that are the permissions for the user, group, and others, grouped in threes. For example, “-rw-rw-r–” represents a file with user permissions “rw-” (read and write, but no execute), and so on.
Octal Representation:
In octal representation, “r” has a value of “4,” “w” is “2,” and “x” is “1.” Therefore, the octal representation for “rw-” is (4 + 2 + 0 =) 6 for the user. Consequently, for “-rw-rw-r–,” it becomes 664.
chmod Command Examples
Here’s the listing of various examples of using the chmod command to modify file permissions.
Example 1: Making a File Read-Only (chmod 400 file_name):
To make a file read-only means only the owner can read it. You need to grant read permissions to the owner and no permissions to others and the 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
Example 2: Changing Permissions for All Users (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
Example 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 (u), group (g), or others (o) 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
Example 4: Removing File Permissions (Symbolic Mode) Using the chmod Command in 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
Example 5: Removing File Permissions (Octal Mode) Using the chmod Command in 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.
Example 6: Removing Read and Write Access for User/Group/Others with Symbolic Mode (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
Example 7: Removing Read and Write Access for All Users with Symbolic Mode (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
Example 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
Example 9: Copying permissions 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.
Example 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.
- Check Information Technology Books
- Practice Programming MCQs
- Check Linux Books
- Apply for Programming Internship