Permissions in Linux
Learn how permissions work in Linux
In Linux, file and directory permissions are essential to control who can access, modify, or execute files and directories on the system. Permissions are assigned to three types of users:
- Owner: The user who created the file or directory.
- Group: A set of users who share permissions.
- Others: All other users of the system.
When we talk about others it may sound a bit confusing but for example, we have a file called example.txt let’s see what we mean by “others”:
Each type of user can have three different permissions:
Read - r
: Allows you to view the contents of a file or list the contents of a directory.Write - w
: Allows you to modify the content of a file or create, delete and rename files in a directory.Execute - x
: For files, allows you to execute a program or script. For directories, allows access to their content.
Well, returning to the example in the example.txt file, we can deduce the following: The file example.txt permissions are= rw-r–r– what does this mean?
- That the owner has Read and Write permissions:
- That the assigned group and others only have read permission:
CHMOD
chmod
changes the file permission bits for each given file in whatever way you tell it to, which can be a symbolic representation of the changes to be made or an octal number representing the bit pattern for the new permission bits.
Symbolic mode
A combination of letters will define which directory/file permissions will be modified.
chmod <u/g/o/a>
- u: represents the permissions of the owner user
- g: represents the permissions of the assigned group
- or: represents the permissions of other users
- a: represents the permissions of all users (the owner’s permissions will not be affected).
We also have the operators +,-,= which are what will cause a permission to be added or removed from a file/directory.
chmod <u/g/o/a><+/-/=>
- +: will cause the indicated permission to be added
- -: will cause the indicated permission to be removed
- =: will cause the indicated permission to be added and in turn eliminate the unmentioned permission.
There are also a series of letters that will help us define or identify what permissions we want to add or remove from this file/directory.
chmod <u/g/o/a><+/-/=><r/w/x/s> <filename>
- r: reading
- w: writing
- x: execution (or in the case of directories, search in them)
- s: sets user or group ID in the execution, such as SUID permissions
Once this is known, we can modify the permissions of our example.txt file, for example assigning execution permissions to the assigned group and the owner:
chmod g+x,u+x example.txt
Let’s remove the execute permission from the example.txt file
chmod g-x example.txt
Absolute mode
There is another way to add permissions to a file/directory, in this mode, file permissions are not represented as characters but as a three-digit octal number.
The following table provides numbers for all types of permits.
Example, we want to set the following permissions using the Absolute method:
- Owner: read, write, execute
- Group: write, execute
- others: read
Looking at the table, the permission configuration would look something like this:
chmod 734 example.txt
You must place the numbers in the order in which the permissions are represented (as explained at the beginning):
chmod 7 3 4 filename
| | |
| | +--- Permissions for others
| +------- Permissions for the group
+----------- Permissions for the owner