What are file and directory permissions in Linux?
Overview
In Linux, the file permissions help to access, write, and execute the files. These permissions restrict the other users from performing an unauthorized activity, and thus they maintain security.
Permissions
There are three principal permissions in Linux.
Read
The read permission provides the capability to read/view the file's contents. We execute this permission with the letter r.
Write
The write permission provides the ability to modify the file, i.e., add or remove contents from the file. We execute this permission with the letter w.
Execute
The execute authorization offers the ability to run the file. For example, you'll need this permission to run a shell script. The letter x describes this permission.
User classes
The Linux permissions model has three user categories. Every user class has its reading, writing, and executing permissions set. Here're the three categories:
- Owner: The owner category provides info about an owner's actions.
- Group: The group describes a group member's actions associated with the file/directory.
- Other: Similarly, the other category describes actions that the other member can perform.
Checking file permissions
We use the ls command to check the file permissions with an extension l indicating the long list format. Here's a syntax and an example.
ls -l
root@educative:/# ls -ltotal 45drwxr-xr-x 1 root root 1024 Dec 31 2018 bindrwxr-xr-x 2 root root 1024 Apr 12 2016 bootdrwxr-xr-x 5 root root 340 May 11 20:18 devdrwxr-xr-x 1 root root 4096 May 11 20:18 etc-rw-r--r-- 1 root root 6 May 11 20:18 file.txtdrwxr-xr-x 2 root root 1024 Apr 12 2016 homedrwxr-xr-x 1 root root 1024 Dec 31 2018 libdrwxr-xr-x 2 root root 1024 Nov 13 2018 lib64drwxr-xr-x 2 root root 1024 Nov 13 2018 mediadrwxr-xr-x 2 root root 1024 Nov 13 2018 mnt-rw-r--r-- 1 root root 11488 Dec 31 2018 nodesource_setup.shdrwxr-xr-x 2 root root 1024 Nov 13 2018 optdr-xr-xr-x 122 root root 0 May 11 20:18 procdrwx------ 1 root root 4096 May 11 20:18 rootdrwxr-xr-x 1 root root 1024 Nov 13 2018 rundrwxr-xr-x 1 root root 1024 Dec 31 2018 sbindrwxr-xr-x 2 root root 1024 Nov 13 2018 srvdr-xr-xr-x 13 root root 0 May 11 20:18 sysdrwxrwxrwt 1 root root 4096 May 11 20:18 tmpdrwxrwxrwx 2 root root 4096 May 11 20:18 usercodedrwxr-xr-x 1 root root 1024 Nov 13 2018 usrdrwxr-xr-x 1 root root 1024 Nov 13 2018 var
Explanation
The ls -l command returned us information about 45 contents within the directory. The first column in the results indicates the permissions for the owner, group, and other categories.
- Line 5: This represents the file (file.txt).
-rw-r–-r-- 1 root root 6 May 11 20:18 file.txt
- The first character
-indicates that this is a file. - The following three characters
rw-indicate that the owner can read and write the file. - The following three characters
r--indicate that the group members only have read permission. - Finally, the last three characters
r--indicate that the other members will only have read permission.