- The "chmod" tool in Linux is used to manage file, directory, and script access by controlling permissions like read, write, and execute.
- Linux file permissions consist of owner, group, and other access levels, each with specific read, write, and execute rights.
- The chmod command can be used in numerical or symbolic forms to modify file permissions, allowing users to add, remove, or define specific access rights for different user classes.
The โchmodโ tool in Linux can be used to control who has access to your system files, directories, and scripts. Although it may appear complicated at first, using this command to modify a fileโs permissions in Linux is straightforward if you get how it works. Chmod only accepts three fundamental inputs, R, W, and X, which are read, write, and execute, respectively. You can modify the file and folder permissions by adding and removing different combinations of the arguments. Letโs first go through the basics of Linux file permissions before talking about the chmod command.
What are File Permissions in Linux
File permissions refer to the type of access associated with a file. However, every file under Linux has an owner, a group, and permission access for the file owner, the group members, and others. Each user class can access files with the read, write, and execute rights. Hence, You can specify which users can run, read, or write the file by understanding file permissions.
File Permissions are Modified by chmodย
Linux uses permissions to manage who has access to a file or directory and how. Three sets are provided: one for the fileโs owner, one for the group that the file belongs to, and a fourth set for everyone else.
Permissions govern the actions that may be taken about the file or directory. A file can either be read, edited, or, if itโs a script or program, run, depending on whether they allow it or donโt. The permissions for a directory control who can create or change files inside the directory and who can cd into the directory.
How to Check File Permission in Linux
To check the permissions of the files already on your system, use the โlsโ command. Specify the fileโs name when using the โlsโ command to view its permissions. To check the โtestfileโsโ file permissions, for instance, we will use the program provided below:
The โ-lโ option is added in this case to obtain the contents of the โtestfile,โ along with its file permissions:
In the output, the first character designates the entry type, with โ-โ standing for โfileโ and โdโ for โdirectory.โ Then there are three sets of nine characters, the first three of which stand for file owner permissions, the second three of which stand for group permissions, and the third of which stands for permissions for other users not included in the first two categories:
There are three characters in each set of permissions. Access permissions are prohibited if the character is a dash โ-.โ If the character is โr,โ โw,โ or โx,โ the user has been granted permission. A file can only be opened and viewed if the user only has read permission, which is indicated by the letter โrโ in a permission set. The letter โw,โ on the other hand, denotes that you have written permission for the particular file and can thus edit, alter, and delete the file. Last but not least, the โxโ letter stands for execute permissions; for example, if your file is a C++ program or script, it will have the execute permission denoted by the letter โx.โ
No permission is granted if the โlsโ command displays โโโ for any set. Another example might be โrwx,โ which denotes that all permissions, including reading, writing, and executing, are allowed.
The background you now have on file permissions will make it easier for you to comprehend how the chmod function operates.
What is chmod Command in Linux?
The abbreviation โchmodโ stands for โchange mode.โ Your systemโs directories, files, and scripts have their access changed. Your systemโs directories, files, and scripts have their access changed as a result. The โchmodโ command has distinct modes that govern the permission for modification. Both numerical form (letters) and symbolic form are used to represent these modes (octal numbers). The syntax for the chmod command in numerical form is as follows:
In numerical representation, we have theseย options:
- โ0โ represents โno permissionโ.
- โ1โ represents โexecute permissionโ.
- โ2โ represents โwrite permissionโ.
- โ4โ represents โread permissionโ.
Following is the syntax for the chmod command when using the symbolic representation:
We have the followingย optionsย in the symbolic form:
- โuโ indicates file owner.
- โgโ indicates groups.
- โoโ indicatesย others.
- โaโ indicates all usersย as owner, group, and others (ugo).
The chmod command, however, only allows the following operators:
- โ+โ: This operator is utilized toย add specified permissions.
- โโโ: This operator is utilized to remove specified permissions.
- โ=โ: This operator is utilized to define the exact file permission for any user.
Letโs look at several instances of how to use the symbolic form of the chmod command in Linux:
Setting โRead by Owner Onlyโ File Permission using chmod Command
We will modify the file permissions of โtestfileโ in this example so that only the owner can read it. No other group or user has access to read, write, or execute this file except for this permission. Even the fileโs owner wonโt be able to open, edit, or execute the file. To do this, add โ0โ for the โgroupsโ and โothersโ modes, which will prevent those users from receiving any rights, and use โ4โ as a numerical representation of โread-onlyโ at the beginning of the three character set:
Now, use the โ-lโ option of the โlsโ command to list the file permissions system:
However, The first โ-โ denotes the existence of a file named โtestfile,โ and the letter โrโ denotes the fact that only the file owner has access to read the file. Additionally, you may verify that neither groups nor other users have any corresponding permissions:
Setting โRead by Group Onlyโ File Permission using chmod Command
In the โownerโ and โothersโ modes, place the โ4โ as the โgroupโ mode between the zeroes. The file will be given โready by group onlyโ permission after this process:
Here โrโ represents the โreadโ permission:
Similarly, you can enable the โread by others onlyโ file permission by specifying the โ004โ mode in the chmod command.
Once more, use the โlsโ command provided below to verify the modifications we made to the โtestfileโ:
Use the โlsโ command to confirm the adjustments we made to the โtestfileโ once more.
Setting โWrite by Owner Onlyโ File Permission using chmod Command
The โwriteโ permissions are represented by the number โ2โ in the numerical representation of the modes. Put a โ2โ at the beginning of the permission set, then two zeros:
Only the file owner will be able to write to the test file after this command has been run:
However, Using the โlsโ command, confirm the modified file permissions:
Here, โwโ stands for the โwriteโ permission:
Hence, Similarly, you can provide the โwrite by group onlyโ and โwrite by other onlyโ permissions using the โ020โ and โ002โ modes, respectively.
Setting โExecute by Owner Onlyโ File Permission using chmod Command
The execute mode is indicated by the โ1โ digit in the chmod command. Execute the command listed below in your terminal to set the โexecute by owner onlyโ permission:
Afterward, Using the โlsโ command once more, list the permissions for the โtestfileโ
โXโ here stands for the โexecute permissionโ:
However, Similar to this, if you want to alter a fileโs permission to โexecute by group only,โ declare the mode as โ010,โ and then add โ001โ as the permission mode to the chmod command to allow other users to execute the file:
Setting โRead by Everyoneโ File Permission using chmod Command
If you modify the file permission to โread by everyoneโ via symbolic links, then run the following command in your terminal:
Here, โaโ stands for โall users,โ โrโ stands for โread permissions,โ and the โ+โ operator is used to provide the read permission to the specified users as well:
Use the โlsโ command to verify the modified file permissions:
ย Setting โexecute by ownerโ File Permission using chmod Command
However, A fileโs โownerโ will be given โexecuteโ permission according to the โu+xโ permission mode:
Now, To confirm the file permission changes, perform the following right away:
In the list of owner permissions, the entry โxโ indicates that the file owner now has permission to run the file:
Conclusion
A technique to limit user access to a specific file to maintain security is setting file permission. However, the chmod command is used in Linux-based operating systems to modify file permissions. You can quickly set these file permissions using the numeric and symbolic modes. We have provided numerous examples of how to use the chmod command in Linux in this post. Weโve demonstrated how to use the chmod commandโs numerical and symbolic modes to modify file permissions.
RELATED GUIDES: