Linux File Permissions

Linux File Permissions

The Linux security mechanism is fundamentally based on file permissions. They decide who and how a system's users may access its files and folders.

How to Check Permissions

  • You can use the ls command with option -al to get detailed information of the files present in the directory.

Understanding the Permissions

  • At the top it shows the Total which means the total number of files and directories are present.

First dash(-)/Character

  • The first represents the type of file. For ex- if there is mentioned (d) that means it is a directory.

  • There are many other options that can be put on the first dash or place.

  • The (.) represents that it is a regular file.

  • The ( l ) represents the Symbolic link.

  • The ( c ) represents the Character Special file. Access to an input/output device using a file. Character special files include, but are not limited to, terminal files, NULL files, file descriptor files, and system console files.

  • The ( b ) represents the Block Special file. It offers physical devices with buffered access and some level of abstraction from their details.

Owner Permissions

  • The first three characters are the file permissions that are granted to the owner of the file.

  • The ( r ) represents the read permissions of the file.

  • The ( w ) represents the write permissions of the file. This permission allows us to edit the file.

  • The ( x ) represents the execute permission. Without this permission we can't execute/run the file in our system.

Group Permissions

  • The second three characters are the file permissions that are granted to the Other user groups.

  • The ( r ) represents the read permissions of the file.

  • The ( w ) represents the write permissions of the file. This permission allows us to edit the file.

  • The ( x ) represents the execute permission. Without this permission, we can't execute/run the file in our system.

Other User Permissions

  • The third three characters are the file permissions that are granted to the Other other people.

  • The ( r ) represents the read permissions of the file.

  • The ( - ) represents the file does not have the write permissions of the file. This permission allows us to edit the file.

  • The ( x ) represents the execute permission. Without this permission, we can't execute/run the file in our system.

Changing Permissions

  • The command which is used to change the permissions of the file is (chmod).

  • The Files permissions can only be changed by the owner of the file or superuser.

  • There are two methods to change the file permissions.

  1. Octal method.

  2. Symbolic method.

Octal Method

  • Here you have predefined octal values which you can use to make permission changes for the file.

  • The ( 0 ) means ( --- ).

  • The ( 1 ) means ( --x ).

  • The ( 2 ) means ( -w- ).

  • The ( 3 ) means ( -wx ).

  • The ( 4 ) means ( r-- ).

  • The ( 5 ) means ( r-x ).

  • The ( 6 ) means ( rw- ).

  • The ( 7 ) means ( rwx ).

  • Ex - (chmod 400 abc.txt), here in this command you can see that 4 is used for the read permission for the owner and the first 0 means there is no permission for the group and the second 0 means there are no permissions for the other users.

Symbolic Method

  • Here you can change the file permissions with the help of (wrx) symbols.

  • To specify what is affected, a combination of the characters u, g, o, and a is used.

  • The (u) means user/owner.

  • The (g) means group.

  • The (o) means Other.

  • The (a) means All Users.

  • Ex - (chmod u+rw abc.txt), here u means the user/owner this command shows that we are adding the read and write permissions for the owner.

Changing the owner of the file

  • The command to change the owner of the file is (chown).

  • The command (chown [owner][:[group]] file...)

  • We will understand this with the help of a example.

  • Ex - (chown nidhish abc.txt), here the new owner is nidhish so this command will change the current owner of the file to the nidhish.

  • Ex 2 - (chown nidhish:sagar abc.txt), here it will change the owner of the file to nidhish and group owner of the file to sagar.

ACL (Access Control List)

  • Regular users can share their files and folders with other users and groups only when they choose to do so by using the Access Control List (ACL) functionality.

  • A user can provide others access to files and directories using ACLS without leaving the filesystem elements wide open or necessitating that the root user modifies the user or group assigned to them.

  • The setfacl command is used to add ACLs to a file, while the getfacl command is used to view the ACLs that have been set on a file.

  • Using the setfacl command, you can modify permissions (-m) or remove ACL permissions (-x).

  • setfacl -m u:username:rwx filename.

Bonus Points

  • The highlighted part in the image below shows the size of the file.

  • The highlighted part in the image below shows the Modification date and time of the file.