CHMOD Command in Linux: chomd is one of the most powerful and important command in Linux. This CHMOD command is mainly used to set, edit and remove the file permission to the user, user group and others. This CHMOD can be expanded as “Change Mode“, the name itself it explains that it is mainly used to change the permission mode of a file. You can use this command to restrict the file by access from others.
- CHMOD Command Syntax
- How it works
- Chmod Command Options Table
- View File Permissions
- Chmod Command Examples
CHMOD Command Syntax:
The basic syntax of CHMOD command in Linux is,
If you didn’t specify any certain options along with chmod command and then chmod modifies the permissions of the file specified by file_name to the permissions specified by permissions.
permissions define the file permissions of the user, member of the groups and others. You can define the permission in two ways and they are:
- Alphanumeric characters
- Octal number representation
The basic alphanumeric symbol syntax of the file permissions is,
chmod u=rwx,g=rx,o=r filename
From the above syntax:
- U stands for User (file owner)
- G stands for the member to the Group
- O stands for the others
- “=” assign the permissions to the user, group members, and others
- r stands for “Read”
- w stands for “Write”
- x stands for “Execute”
- “,” which separates the classes of permissions.
How it Works:
The equivalent octal numeric symbol syntax of the file permission is,
chmod 754 filename
The above syntax also represents the same file permission but in octal numeric representation.
From the above syntax,
The digits 7,5,4 is the sum of “4, 2, 1, 0“. Each digit “7, 5, 4” represents the file permission settings of User, members of the group.
- 4 stands for “Read”
- 2 stands for “Write”
- 1 stand for “Execute”
- 0 stand for “Nothing”
So the number 7 (4+2+1+0) gives read, write and execute permissions to the user, the number 5 (4+1+0) gives read & execute permissions to the members of the group and finally, the number 4 (4+0) gives read permission to the others outside to the file and groups.
Chmod Command Options Table:
You can find the list of options that are available in CHMOD command, follow the instructions below:
- Open the Terminal
- Enter the command ” chmod –help “
The following chmod options table will appear on the screen.
[email protected]:~$ chmod --help Usage: chmod [OPTION]... MODE[,MODE]... FILE... or: chmod [OPTION]... OCTAL-MODE FILE... or: chmod [OPTION]... --reference=RFILE FILE... Change the mode of each FILE to MODE. With --reference, change the mode of each FILE to that of RFILE.
Chmod Options table:
||like verbose but report only when a change is made|
||suppress most error messages|
||output a diagnostic for every file processed|
||do not treat ‘/’ specially (the default)|
|–preserve-root||fail to operate recursively on ‘/’|
||use RFILE’s mode instead of MODE values|
||change files and directories recursively|
||display this help and exit|
||output version information and exit|
Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'. GNU coreutils online help: <https://www.gnu.org/software/coreutils/> Full documentation at: <https://www.gnu.org/software/coreutils/chmod> or available locally via: info '(coreutils) chmod invocation' [email protected]:~$
View File Permissions:
Once you assigned the file permission, you can verify it by view the permissions of the files. To do this, we recommend you to use the ls command in Linux.
Use the ls command to show the file permissions:
The output of the above command is,
[email protected]:~$ ls -l example.txt -rw-r--r-- 1 tecrobust tecrobust 34 Aug 17 16:48 example.txt [email protected]:~$
The complete meaning of the above output is,
- – stands for a regular file. If you get the output as “d” and then it means a directory or if you get the output as “l” and then it means a regular symbolic link.
- rw- stands for the file have read and write permission for the user. The first 3 character (rw-) represents the file permissions of the user. The user doesn’t have the permission to execute the file.
- r– This 3 character represents the file permission for the members of the group. In this scenario, The member of groups has the read permission only. They don’t have the permission to write and execute the file.
- r– This 3 character represents the file permission for the others. Other can read the file and have no further permissions. They don’t have the permission to write and execute the file.
- 1 stand for the number of hard links to this file.
- tecrobust stands for the files owner name
- tecrobust stands for the group where the file belongs
- 34 stands for the size of the file
- Aug 17 16:48 stands for the file’s date and time
- example.txt represents the name of the file.
Chmod Command Examples:
Some essential chmod commands examples,
To set the permission of file “tecrobust.html” to the owner can read and write; the group can read-only; others can read-only.
To change the file permission for the owner of the image “flower.png” & so that the owner may read and write the file. Do not change the permissions for the group, or for others.
To set the file permission of “chrome.deb” to “read, write, and execute by owner” and “read and execute by the group and everyone else”.
To set the file to read and write by everyone,
To set and unset the Set-User-ID of a file,The above command sets the set user id to the file example.txt.
The above command unsets the set user id to the file example.txt.
That’s it. You have reached the conclusion section. As we said earlier CHMOD is one of the most useful commands in Linux. Try to learn the options of the chmod command and use the chmod command to set the permissions to your files. Set the file permission to the user, member of the group and others with chmod command. You can view the permission with the help of ls command. If you have any queries related to this topic and then feel free to comment us below or drop us the question in our Forum.
For more articles, Kindly visit our Homepage