Linux is a secure operating system that uses file and directory permissions to allow and deny different access types. In general, a file or directory has 3 operations named read
, write
and execute
. These operations permissions can be set according to the different users or user groups. The chmod
command is used to change file and directory permissions. The chmod command name comes from change mod
.
chmod Command Syntax
The chmod command has the following syntax
chmod PERMISSION FILE_DIRECTORY
- PERMISSION is the permission we want to set to the FILE_DIRECTORY.
- FILE_DIRECTORY is a file or directory in which permissions are changed.
Permissions
Linux provides the following permissions.
Numeric Value | Characer Value | Permission | Description |
---|---|---|---|
0 | – | None | |
1 | e | Execute | |
2 | w | Write | |
3 | ew | Execute and Write | |
4 | r | Read | |
5 | re | Read and Execute | |
6 | rw | Read and Write | |
7 | rwe | Read, Write and Execute |
Change Owner User Permissions
The u
character is used to change the file or directory permission for the owner user. Every file and directory has an owner user and the permission can be changed like below.
Add User Execute Permission
$ chmod u+x file.txt
Remove User Execute Permission
$ chmod u-x file.txt
Add User Write Permission
$ chmod u+w file.txt
Remove User Write Permission
$ chmod u-x file.txt
Add User Read Permission
$ chmod u+r file.txt
Remove User Remove Permission
$ chmod u-r file.txt
Add User Read and Write Permission
$ chmod u+rw file.txt
Remove User Read and Write Permission
$ chmod u-rw file.txt
Change Group Permissions
Files and directories also have group ownership where the ownership group has provided permissions. Group ownership is used to provide a group of users to the files and directories. User permissions for files and directories can be changed like below.
Add Group Execute Permission
$ chmod g+x file.txt
Remove Group Execute Permission
$ chmod g-x file.txt
Add Group Write Permission
$ chmod g+w file.txt
Remove Group Write Permission
$ chmod g-x file.txt
Add Group Read Permission
$ chmod g+r file.txt
Remove Group Remove Permission
$ chmod g-r file.txt
Add Group Read and Write Permission
$ chmod g+rw file.txt
Remove Group Read and Write Permission
$ chmod g-rw file.txt
Change Other Permissions
The other is used to specify all users except the owner user and owner group which is described in the previous parts. Generally removing all permissions for the other is a secure implementation.
Add Other Execute Permission
$ chmod o+x file.txt
Remove Other Execute Permission
$ chmod o-x file.txt
Add Other Write Permission
$ chmod o+w file.txt
Remove Other Write Permission
$ chmod o-x file.txt
Add Other Read Permission
$ chmod o+r file.txt
Remove Other Remove Permission
$ chmod o-r file.txt
Add Other Read and Write Permission
$ chmod o+rw file.txt
Remove Other Read and Write Permission
$ chmod o-rw file.txt
Change All Permissions
The chmod command also provides the a
to set permission for all user types like user, group and others.
Add All Execute Permission
$ chmod a+x file.txt
Remove All Execute Permission
$ chmod a-x file.txt
Add All Write Permission
$ chmod a+w file.txt
Remove All Write Permission
$ chmod a-x file.txt
Add All Read Permission
$ chmod a+r file.txt
Remove All Remove Permission
$ chmod a-r file.txt
Add All Read and Write Permission
$ chmod a+rw file.txt
Remove All Read and Write Permission
$ chmod a-rw file.txt
Change Permission Recursively
In order to change directories and child content permissions, we should specify the -R
recursive option for the usermod
.
$ chmod u+rw mybackups