How to use the chmod command in Linux
The “chmod”tool in Linux can be used to control who has access to your system files, directories, and scripts. Although it may seem complicated at first, using this command to change the permissions of a file in Linux is not difficult once you understand how it works. Chmod only accepts three basic inputs, R, W, and X, which are read, written, and executed, respectively. You can change file and folder permissions by adding and removing different combinations of arguments. Let’s first look at the basics of Linux file permissions before talking about the chmod command.
Content:
- 1 What are file permissions in Linux
- 2 How to check file permissions in Linux
- 3 What is the chmod command in Linux?
- 3.1 Setting file permissions to “Owner Only”using the chmod command
- 3.2 Setting File Permission to “Group Only”Using the chmod Command
- 3.3 Setting File Permission to “Write by Owner Only”Using the chmod Command
- 3.4 Setting File Permission to “Execute Owner Only”Using the chmod Command
- 3.5 Setting file permissions to “Read by all”using the chmod command
- 3.6 Setting file permissions to “execute by owner”using the chmod command
- 3.7 Conclusion
What are File Permissions in Linux
File permissions refer to the type of access associated with a file. However, every file in Linux has an owner, a group, and permissions for the file’s owner, group members, and others. Each user class can access files with read, write, and execute permissions. Therefore, you can specify which users can run, read, or write a file by understanding the file’s permissions.
File permissions are changed with chmod
Linux uses permissions to control who and how has access to a file or directory. There are three sets: one for the owner of the file, one for the group the file belongs to, and a fourth set for everyone else.
Permissions control the actions that can be taken on a file or directory. The file can be read, edited, or, if it’s a script or program, run, depending on whether they allow it or not. Directory permissions control who can create or modify files within a directory and who can navigate to a directory.
How to Check File Permissions in Linux
To check the permissions of files already on your system, use the “ls”command. Specify the filename when using the “ls”command to view its permissions. For example, to check the permissions of the file “testfile”, we will use the program below:
There are three characters in each permission set. Access permissions are denied if the character is a dash “-“. If the character ‘r’, ‘w’, or ‘x’ is used, the user has been granted permission. The file can only be opened and viewed if the user has read-only permission, which is represented by the letter “r”in the permission set. On the other hand, the letter “w”means that you have written permission for a particular file, and thus you can edit, modify, and delete the file. Last but not least, the “x”stands for execute permissions; for example, if your file is a C++ program or script, it will have execute permission, denoted by the letter “x”.
Permission is not granted if the “ls”command displays “-“for any set. Another example would be “rwx”, meaning that all permissions are allowed, including read, write, and execute.
The background you now know about file permissions will make it easier for you to understand how the chmod function works.
What is the chmod command in Linux?
The abbreviation “chmod”means “change mode”. Access to directories, files, and scripts on your system has changed. As a result, access to system directories, files, and scripts has changed. The “chmod”command has different modes that determine the permission to change. Both numeric form (letters) and symbolic form (octal numbers) are used to represent these modes. The syntax for the chmod command in numeric form is as follows:
In numerical representation, we have the following options:
- “0”means “no permission”.
- “1”represents “permission to execute”.
- “2”means “write permission”.
- “4”represents “permission to read”.
The following is the syntax for the chmod command when using symbolic representation:
We have the following options in symbolic form:
- “u”indicates the owner of the file.
- “g”indicates groups.
- “o”indicates others.
- “a”specifies all users as owner, group, and others (ugo).
However, the chmod command only allows the following statements:
- “+”: This operator is used to add the specified permissions.
- “-“: This operator is used to remove the specified permissions.
- “=”: This operator is used to determine the exact file permissions for any user.
Let’s look at a few examples of using the symbolic form of the chmod command in Linux:
Setting file permissions to “Read Only by Owner”using the chmod command
Now use the “-l”option of the “ls”command to list the file permission system:
Setting File Permission to “Group Only”Using the chmod Command
Here “r”represents “read”permission:
Similarly, you can enable “read-only by others”permission for a file by specifying “004”mode in the chmod command.
Use the “ls”command to re-confirm the changes we’ve made to “testfile”.
Setting File Permission to “Write by Owner Only”Using the chmod Command
Write permissions are represented by the number “2”in the numeric mode representation. Put a “2”at the beginning of the permission set, followed by two zeros:
However, use the “ls”command to confirm the changed file permissions:
Therefore, in the same way, you can grant “write only by group”and “write only by others”permissions using modes “020”and “002”respectively.
Setting File Permission to “Execute by Owner Only”Using the chmod Command
After that, using the “ls”command again, list the permissions for “testfile”.
However, as in this case, if you want to change the file’s permission to “group execution only”, declare the mode “010”and then add “001”as the permission mode to the chmod command to allow other users to execute the file:
Setting file permissions to “Read by all”using the chmod command
If you change the file’s permission to “read by all”via symlinks, run the following command in your terminal:
Use the “ls”command to check the changed file permissions:
Setting file permission to “execute by owner”using chmod command
Now, to confirm changes to file permissions, immediately follow these steps:
Conclusion
The technique for restricting user access to a particular file for security purposes is to set file permissions. However, the chmod command is used on Linux-based operating systems to change file permissions. You can quickly set these file permissions using the numeric and character modes. In this post, we have provided many examples of how to use the chmod command in Linux. We have demonstrated how to use the numeric and character modes of the chmod command to change file permissions.
Leave a Reply