HomeGuidesHow to Use the chmod Command on Linux

How to Use the chmod Command on Linux

-

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