HomeGuidesHow to Use the chmod Command on Linux

How to Use the chmod Command on Linux

-

Key Takeaways
  • 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.

How to Use the chmod Command on Linux

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:

LEAVE A REPLY

Please enter your comment!
Please enter your name here

LATEST