Introduction:
The chmod command is a powerful command of the Linux operating system. The name is a short form of change mode. The chmod command allows users to control file and directory permissions, granting or denying read, write, and execute permissions. This article explained the chmod command with examples.
Understanding File Permissions:
First, you need to understand file permissions. All files and directories have a set of permissions that control who can read, write, or execute the file. There are three categories of permissions: read, write, and concurrent permissions, denoted by 'r', 'w', and 'x'. These permissions are assigned to three different user groups: the file's owner, the group the file belongs to, and others (everyone else). The chmod command uses this permission to allow or restrict access to directories and files.
Syntax of chmod Command:
chmod [options] permissions file(s)/directory
Options of chmod Command:
The chmod command has various options to refine its functionality. Here are some commonly used options.
Options |
Description |
`-c` |
This option displays a confirmation message for each file or directory modified. |
`-f` |
This option helps to avoid displaying error messages on the screen. |
-v |
This option displays a message for each file or directory that is modified. |
-R |
This option changes permissions recursively, modifying files and subdirectories within a directory. |
Numeric Mode(Octal mode) of chmod command:
The chmod command supports numeric mode and symbolic mode for setting permissions:. Let's focus on numeric mode first.
In numeric mode, permissions are represented by numbers:
Value |
Permission |
4 |
Read Permission |
2 |
Write Permission |
1 |
Execute Permission |
You can assign permissions in numeric mode, add the numbers for each user group, and pass the result as an argument to the chmod command.
Examples using the Numeric Mode(Octal mode) of chmod command:
1. Suppose you want to grant only read and write permissions to the owner of file "example.txt". Run the following command.
# chmod 600 example.txt
You can check the current permissions of a file or directory using the ls -l command.
2. Suppose you want to grant read, write, and execute permissions to the owner, read and execute permissions to the group, and no permissions to others for a script called "script.sh": Run the following command.
# chmod 750 script.sh
You can check the current permissions of a script.sh file using the ls -l script.sh command.
Symbolic Mode of chmod command:
Symbolic mode allows you to change permissions in a more meaningful way. The symbolic mode consists of three components: User Group, Operator, and Permissions.
Components |
Symbols |
User Group |
u (user/owner), g (group), o (others), a (all) |
Operator |
+ (add), - (remove), = (set) |
Permissions |
r (read), w (write), x (execute) |
Examples using the Symbolic Mode of chmod command:
1. Suppose you want to add execute permission for all user groups to a file named "test.php". Run the following command.
# chmod a+x test.php
You can check permission of test.php using the ls -l test.php command.
2. If you wish to remove write permission for others from a file called "Example.txt". Run the following command.
# chmod o-w Example.txt
You can check the permission of example.txt using the ls -l example.txt command.
Conclusion:
The chmod command is a useful tool that allows users to manage file and directory permissions. By understanding the syntax and different modes of operation, you can safely control access to files and ensure the security and integrity of your system. In this article we have mentioned an overview of the chmod command, explained file permissions, and demonstrated both numeric and symbolic modes with practical examples. Once you understand this, you can take advantage of the chmod to customise permissions for your specific needs.