Linux file permissions form the foundation of the system’s security model. They define who can read, write, or execute files and directories, ensuring only authorized users or processes can access sensitive data. You can modify these permissions using the chmod
command.
chmod +rwx filename
– Adds read, write, and execute permissions.chmod -rwx directoryname
– Removes all permissions.chmod +x filename
– Grants executable permission.chmod -wx filename
– Removes write and execute rights.Every file or directory has three types of permissions:
Permissions are assigned to three categories of users:
What are Permission Groups in LinuxNote: All these permissions are being granted at three different levels based on their group.
First, you must think of those nine characters as three sets of three characters (see the box at the bottom). Each of the three "rwx" characters refers to a different operation you can perform on the file.
--- --- --- rwx rwx rwx user group otherUser, Group, and others Option in Linux File Permission Reference Class Description `u` user The user permissions apply only to the owner of the file or directory, they will not impact the actions of other users. `g` group The group permissions apply only to the group that has been assigned to the file or directory, they will not affect the actions of other users. `o` others The other permissions apply to all other users on the system, this is the permission group that you want to watch the most. `a` All three All three (owner, groups, others) How to Check the Permission of Files in Linux
Let's dive in to understand the possible methods to check all the desired details of a file including "File Permission"
1. The "Trusty Command"Here's the command to execute it within the terminal. Let's show you with an example:
Input:
We're taking 'NarX' as a default file name:
ls -l NarX.txt
Output:
-rw-r--r-- 1 user group 46 Apr 14 16:37 NarX.txt
The above command represents these following information:
The 'namei' command is used to check the file path through layer of folder's path. Here's the command to execute it within Terminal:
Here, we've taken 'path' as " root@anonymous-VirtualBox:~# " and file name as "hoops"
namei -l /path/to/your/file3. The 'stat' Command
Unlike 'ls -l' command, the "stat" command is used to pin point the file location. Here's how you can do it:
We're taking file name as "hoops"
stat hoops
Output:
File: example.txt Size: 2210 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 1288496 Links: 1 Access: 2024-11-18 10:50:56.000000000 +0000 Modify: 2024-11-18 10:50:56.000000000 +0000 Change: 2024-11-18 10:50:56.000000000 +0000 Birth: -How to Change Permissions in Linux
The command you use to change the security permissions on files is called "chmod", which stands for "change mode" because the nine security characters are collectively called the security "mode" of the file. You can modify permissions using symbolic notation or octal notation.
1. Symbolic NotationSymbolic notation allows you to add, remove, or set permissions for specific users. Let's understand this using different example below:
Example 1: To Change File Permission in Linux
If you want to give "execute" permission to the world ("other") for file "xyz.txt", you will start by typing.
chmod o
Now you would type a '+' to say that you are "adding" permission.
chmod o+
Then you would type an 'x' to say that you are adding "execute" permission.
chmod o+x
Finally, specify which file you are changing.
chmod o+x xyz.txt
You can see the change in the picture below.
You can also change multiple permissions at once. For example, if you want to take all permissions away from everyone, you would type.
chmod ugo-rwx xyz.txt
The code above revokes all the read(r), write(w), and execute(x) permission from all user(u), group(g), and others(o) for the file xyz.txt which results in this.
Example 2:
The code adds read(r) and write(w) permission to both user(u) and group(g) and revoke execute(x) permission from others(o) for the file abc.mp4.
chmod ug+rw,o-x abc.mp4
Something like this:
chmod ug=rx,o+r abc.c
Assigns read(r) and execute(x) permission to both user(u) and group(g) and add read permission to others for the file abc.c.
There can be numerous combinations of file permissions you can invoke revoke and assign. You can try some on your Linux system.
2. Octal Notations Permissions in LinuxThe octal notation is used to represent file permission in Linux by using three user group by denoting 3 digits i.e.
Here's how to permissions are mapped:
Permissions for owner, group, and others are represented by a three-digit octal value. The sum of permissions for each group gives the corresponding number.
Reference:
chmod o
Now you would type a '+' to say that you are "adding" permission.
chmod o+
Then you would type an 'x' to say that you are adding "execute" permission.
chmod o+x
Finally, specify which file you are changing.
chmod o+x xyz.txt
You can see the change in the picture below.
You can also change multiple permissions at once. For example, if you want to take all permissions away from everyone, you would type.
chmod ugo-rwx xyz.txt
The code above revokes all the read(r), write(w), and execute(x) permission from all user(u), group(g), and others(o) for the file xyz.txt which results in this.
Octal Notations PermissionsExample:
The code adds read(r) and write(w) permission to both user(u) and group(g) and revoke execute(x) permission from others(o) for the file abc.mp4.
chmod ug+rw,o-x abc.mp4
Something like this:
chmod ug=rx,o+r abc.c
assigns read(r) and execute(x) permission to both user(u) and group(g) and add read permission to others for the file abc.c.
There can be numerous combinations of file permissions you can invoke revoke and assign. You can try some on your Linux system.
You can also use octal notations like this.
octal notations
Using the octal notations table instead of 'r', 'w', and 'x'. Each digit octal notation can be used for either of the group 'u', 'g', or'o'.
So, the following work is the same.
chmod ugo+rwx [file_name] chmod 777 [file_name]
Both of them provide full read write and execute permission (code=7) to all the group.
The same is the case with this.
chmod u=r,g=wx,o=rx [file_name] chmod 435 [file_name]
Both the codes give read (code=4) user permission, write and execute (code=3) for the group and read and execute (code=5) for others.
And even this...
chmod 775 [file_name] chmod ug+rwx,o=rx [file_name]
Both the commands give all permissions (code=7) to the user and group, read and execute (code=5) for others.
The combination for the permissions are r,w,x, and -. Let's understand this briefly in elaborative way:
For example: "rw- r-x r--"Besides usual methods, Linux also offers special permission types to have more complex control over files.
1. The 'setuid' CommandThe SET User ID permission allows user to execute programs with the previledges of its owner. Below is the example for the same:
chmod u+s program2. The 'setgid' Command
The Set Group ID permission allows files to run under fule's group permissions (or ensures the files created in a directory inherits the group of the directory). Here's the command for the same:
chmod g+s directoryname3. The 'sticky bit' Command
This allows the user (only owner) to delete or rename files within the directory (regardless of other user's permissions). Here's a command for the same:
chmod +t directorynameHow to Set File Permissions for a Specific User
To set permissions for a specific user or group:
1. By using chownUse chown to change file ownsership:
chown user:group file.txt2. By using chmod
Use chmod to modify permissions:
chmod 755 file.txt
File and Directory Permissions in Operating System (OS) | Linux Tutorial
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4