The chmod command is used to modify the access permissions of files and directories. It can modify the read, write, and executable permissions, which can help you control shell script execution or specific file modifications.
In this article, you will learn how to change referenced file or directory permissions using the chmod command in Linux (with practical examples).
Tutorial Details
Description | Chmod (Change Mode) |
Difficulty Level | Moderate |
Root or Sudo Privileges | Yes (For non-file owners) |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | chmod |
Internet Required | No |
The chmod command is used with different options, references, operators, and modes that can help you assign different permissions to your files.
The following are all valid options supported by the chmod command:
Options | Description |
---|---|
-c, --change | It will report when the changes are identical to the verbose option. |
-f, --silent, --quiet | It will suppress the most error messages. |
-v, --verbose | It will display the diagnostic for every processed file. |
--no-preserve-root | It is used to avoid special treatment of the backslash symbol (‘/’) (the default). |
--preserve-root | It will fail to recursively operate on backslashes (‘/’). |
--reference=RFILE: | It is used to specify the RFILE’s mode instead of MODE values. |
-R, --recursive | It is used to change files and directories recursively. |
The following is a list of references that are used to distinguish between users, groups, others, and all.
Reference | Class | Description |
---|---|---|
u | owner | Owner of the file or directory. |
g | group | User who are the members of the file group. |
o | others | Users who are not the file owner or not members of the file group. |
a | all | All of the above. |
The operators are used to add or remove access permissions to the file or directory.
Operators | Description |
---|---|
+ | It will add the specified mode to the selected file. |
- | It will remove the specified mode from the selected file. |
= | It will add the specified mode and remove the unmentioned mode. |
The modes are the access permissions that can be given to a file or directory or taken away from them.
Modes | Description |
---|---|
r | Permission to read the file. |
w | Permission to modify or delete the file. |
x | Permission to execute the file. |
Instead of specifying the references, operators, and modes to modify the access permission for a file or directory, you can use the numerical shorthand.
The first digit represents the file owner (u
), the second digit represents the group members (g
), and the third digit represents the others (o
).
The following is a list of all values and their permissions.
Values | Permissions |
---|---|
0 | No permission |
1 | Execute permission |
2 | Write permission |
4 | Read permission |
Setting Permissions using the References, Operators, and Modes
The following is an example of modifying file or directory permissions using references, operators, and modes.
For now, we will perform all the permissions modifications on “file.txt
“.
Check the following output to find its current permission.
$ ls -l file.txt
Output:
Right now, the “file.txt
” owner and group have read and write permission, and others have read permission.
The following command will give the “file.txt
” owner read, write, and executable permissions, and members of the group and other users will have the read permission.
$ chmod u=rwx,go=r file.txt
$ ls -l file.txt
Output:
The following command will give the “file.txt
” owner and member of the group permissions, and other users will have no permissions.
$ chmod ug=rwx,o= file.txt
$ ls -l file.txt
Output:
The following command will remove all permissions from “file.txt
” for the owner, members of the group, and other users.
$ chmod ugo= file.txt
$ ls -l file.txt
Output:
The following command will only give the owner access permission to read, write, and modify the file.
$ chmod u+rwx file.txt
$ ls -l file.txt
Output:
The following command will remove the executable permission for the owner and give read and write permission to other members of the group and other users.
$ chmod u-x,go+rw file.txt
$ ls -l file.txt
Output:
Setting Permissions using Numerical Shorthand
The following command will give the owner only read permission and no permission for members of the group or other users.
$ chmod 400 file.txt
$ ls -l file.txt
Output:
The following command will give the owner read, write, and executable permission but no permission to other members of the group or other users.
$ chmod 700 file.txt
$ ls -l file.txt
Output:
The following command will remove the executable permission from the owner and give read permission to members of the group and other users.
$ chmod 644 file.txt
$ ls -l file.txt
Output:
The following command will give the owner all permissions and read and write permissions to members of the group and other users.
$ chmod 766 file.txt
$ ls -l file.txt
Output:
The following command will remove all the permissions for the owner and only give read permission to members of the group and other users.
$ chmod 044 file.txt
$ ls -l file.txt
Output:
I think this is a good enough example to make it clear how and when to use them.
If you struggle while using or understanding them, feel free to ask for help in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.