Table of contents of the article:
Linux permissions allow you to set security levels for different users. Linux allows you to define access levels for individual files, specify attributes and tighten security. This way you can provide granular permission levels and improve file security.
Linux allows you to choose between different file permissions for each type of user, but they can generally include a combination of:
- To write
- To execute
Before we talk about Linux file permissions, let's talk about the different types of users.
The three types of users include:
Owners include those who create the file.
Groups generally include different users with the same Linux permissions. For example, the owner can provide read information to a specific group instead of manually granting permission to each user.
This includes any other users who can access the file. These are typically users who have not created the file, nor are they part of a group with specific permissions. So, if you have set permissions for “others”, they will be applied to this group.
Understanding Linux permissions
Now, as mentioned earlier, any file or directory can be assigned any of the three permissions discussed below.
Read permission gives users the ability to open and read a file. If read permission is assigned to a directory, users will be able to list all of its contents.
Write permission allows users to change the contents of a directory or file. This allows a user to add, remove, rename or modify the file as they see fit.
For example, if you need to write permissions for a file, but not for the directory, you will not be able to rename or remove the file from the directory. You will only be able to make changes to the content of the file itself.
This allows the user to run the file. Basically, with this permission, you can run the file.
Linux permissions example
For example, if the file name is:
ls -l example_file
The first character tells you the file type. It could be a directory (d), a normal file (-) or a symbolic link (l). The output of this file provides the following permissions:
In the first part, you can see that the owner of the file has both the permissions of reading that of writing . Hence, the group has only the permission of writing and all others have only the permission of reading.
How to change Linux file permissions
The chmod command is commonly used to change the permissions of Linux files. Any user with sudo, root, and owner privileges of the file is able to change the permissions of the file. When using the symbolic format, here is the format you can use to change the permission of the file:
chmod [OPTIONS] [ugoa] [-+=] perms...[,...] filename...
In this format, the first set of flags simply indicates the user classes for which you want to change permissions. These include:
- The owner (u)
- Group (g)
- Other users (o)
- All users (a)
The next set of flags defines whether to add (+), remove (-) or change existing permissions to specific permissions (=).
For example, if the command is:
chmod -r o-x example_file
It will remove the execute permission for all other users.
Also known as numeric mode, file permissions in this mode are represented by a three-digit number rather than a series of characters. Here are the numerical values for your understanding:
0 = No authorization
1 = Run
2 = Write
3 = Run and write
4 = Read
5 = Read + Run
6 = Read + Write
7 = Read + Write + Run
So, if you were to use the following command:
chmod 754 example_file
Basically you will change the file permissions as follows:
- Owner: read, write and execute (7)
- Group: Read and execute (5)
- All others: Read only (4)
We hope you find this simple Linux permissions guide useful. Note that the chmod command allows you to change permissions, as long as you have sudo privileges or are the owner of the file.