Linux File Permissions Cheat Sheet
Publisher: Psychz Networks, November 29,2021- Introduction
- About File Permissions
- Notations to change permissions
- Chmod
- Changing file ownership
- Changing group ownership
Introduction
Linux systems are not only multitasking but also multi-user. It means that more than one user can be operating the computer at the same time. If the system is attached to a network, or the Internet, remote users can log in via ssh (secure shell) and operate the computer. In fact, remote users can execute graphical applications and have the output displayed on a remote computer.
On a Linux system, each file and directory is assigned access rights for the owner of the file, the members of a group of related users, and everybody else. Rights can be assigned to read a file, to write a file, and to execute a file (i.e., run the file as a program).
Permissions
Permissions on Linux systems are split into three classes:
Files and directories are owned by a user. Files and directories are also assigned to a group. If a user is not the owner, nor a member of the group, then they are classified as other.
Changing permissions
In order to change permissions, we need to first understand the two notations of permissions.
Symbolic notation
It is what you'd see on the left-hand side if you ran a command like ls -l in a terminal. The first character in symbolic notation indicates the file type and isn't related to permissions in any way. The remaining characters are in sets of three, each representing a class of permissions.
The first class is the user class. The second class is the group class. The third class is the other class. Each of the three characters for a class represents the read, write and execute permissions.
Permission | On a File | On a directory |
---|---|---|
r (read) | read file content (cat) | read directory content (ls) |
w (write) | change file content (vi) | create file in directory (touch) |
x (execute) | execute the file | enter the directory (cd) |
Octal notation
Octal (base-8) notation consists of at least 3 digits (sometimes 4, the left-most digit, which represents the setuid bit, the setgid bit, and the sticky bit).
Each of the three right-most digits are the sum of its component bits in the binary numeral system.
For example:
The read bit (r in symbolic notation) adds 4 to its total
The write bit (w in symbolic notation) adds 2 to its total
The execute bit (x in symbolic notation) adds 1 to its total
So what number would you use if you wanted to set a permission to read and write? 4 + 2 = 6.
Let us now see how to change the permissions to file or directory using example of both Symbolic and Octal notations
Chmod
The chmod command is used to change the permissions of a file or directory. To use it, we specify the desired permission settings and the file or files that we wish to modify.
It is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here's how it works:
Where
Now, if we represent each of the three sets of permissions (owner, group, and other) as a single digit, we have a pretty convenient way of expressing the possible permissions settings. For example, if we wanted to set 'dummy_file' to have read and write permission for the owner, but wanted to keep the file private from others, here's how we would do it
# chmod 600 dummy_file
Here is a table of numbers that covers all the common settings. The ones beginning with "7" are used with programs (since they enable execution) and the rest are for other kinds of files.
Value | Meaning |
---|---|
777 | (rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting. |
755 | (rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users. |
700 | (rwx------) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others. |
666 | (rw-rw-rw-) All users may read and write the file. |
644 | (rw-r--r--) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change. |
600 | (rw-------) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private. |
Changing File Ownership
We can change the owner of a file by using the chown command. Here's an example: Suppose we wanted to change the owner of a 'dummy_file' from "me" to "new_user"
# sudo chown new_user dummy_file
Note: In order to change the owner of a file, you must be root user or have sudo privileges.
chown works the same way on directories as it does on files.
Changing Group Ownership
The group ownership of a file or directory may be changed with chgrp. This command is used like this:
In the example above, we changed the group ownership of dummy_file from its previous group to "new_group". We must be the owner of the file or directory to perform a chgrp.
Conclusion
In the above article we have learned three user types in linux viz. User, Group, and other. We have also learned that linux divides the file permissions into Read (r), write (w), and execute (x). How to change file permissions on a file using 'chmod' command using Symbolic and Octal notation.
We hope that you find this article useful and put the information to use. If you like this article, please show your appreciation by clicking on vote button with 'Yes' located on your left hand side.