Linux
The chmod command
Introduction
If you are like me, you find man pages very complicated. This blog aims to simplify the chmod man page and serve as a cheat sheet for the command. We will go over permissions, permission strings, and how to manipulate those permissions with CHMOD and octal numbers. By the end of the blog, you will be able to view, update, and understand how to change the permissions of any file.
File System Users and Their Permissions
First, the chmod command heavily deals with permissions and to understand you first need to understand the 3 types of permissions that can be present on a file:
- Read — allows a given user to read the contents of a file
- Write — allows a user to edit or write to the file
- Execute — allows a user to execute the file
These permissions are not mutually exclusive. Each permission is independent of the others, and a file or directory can have any combination of these permissions.
Second, there are 3 categories of users who could have permission to read, write or execute any given file. These users are often summarized by the following acronym
- U — The user that created the file or the owner of the file. A file cannot exist without an owner.
- G — Group — The group associated with a file, which typically defaults to the primary group of the user who creates the file.
- O — Other users who may try to access the file
A file’s owner cannot be deleted. Once a file is created, its ownership is permanently tied to that user. Even if the user account is removed, the file’s ownership remains intact, similar to specifyingON DELETE = NO ACTION when defining foreign key relationships in SQL.
Now let’s dive into how we can use the chmod command to grant or revoke these three types of permissions for these three categories of users:
Permission Strings
To modify a file’s permissions, we first need to understand permission strings. Let’s view an example:
First, to create a file we can use the touch command to create a file called example.txt:
touch example.txt
This will create a file in our current directory called example.txt. We can view the file along with it’s permissions by leveraging the ls command and filtering for our newly created file:
ls -l | grep "example.txt"
>>> -rw-r--r--@ 1 robertcampbell staff 0 Dec 3 18:24 example.txt
-rw-r--r--@
shows us the permission string. The breakdown for the meaning of each character in a permission string is as follows:
- The first character represents whether or not the candidate is a file (-) or a directory (d)
- The next three characters specify the permissions — read, write or executable in that order — for the user that owns the file. The “-” signifies that the user does not have the given permission
- The middle three characters specify the permissions for the group that is associated with the file
- The last three characters specify the permissions for any other user.
The permission string -rw-r--r--@
means that the owner of the file has read and write permission while everyone else has only read permission. I have provided a visualization of how the format of the permission string works.
Absolute Mode and Octal Numbers
Finally, let’s change these permissions so that the group has write permissions as well. In the following example, I use chmod with octal numbers to change the permissions of the file:
chmod 664 example.txt
Remember our acronym UGO. In the chmod command, the first number represents the permissions for the owner of the file. The second number represents the permissions for the group associated with the file. Finally, the last number represents permissions for other users. In the diagram below, we show how the above command grants read and write permissions to the user and group and only read permissions to other users.
We can now view the updated permissions on our file with ls -l | grep “example.txt” . If you view the output we can see that -rw-r--r--@
changed to -rw-rw-r--@
.
Conclusion
Man pages can often feel overwhelming, but understanding and using chmod doesn’t have to be. This blog simplified the essentials, turning the dense details of the chmod man page into a practical cheat sheet. We covered permissions, how to read permission strings, and how to modify them using symbolic and octal notation. With these skills, you can confidently view, update, and manage file permissions on any system. Now, you're ready to take control of your files and directories like a pro!
References
Man Pages (GNU Coding Standards), www.gnu.org/prep/standards/html_node/Man-Pages.html. Accessed 4 Dec. 2024.