File permissions, ownerships control the access level of files and system process specific to users. This makes sure that the authorized users and process can only access the specific files and directories.
In this guide you are going to learn how to assign permission access rights to different classes of users.
File Permission Basics
The file permissions are applied over three different types of users.
- Others (Everyone else)
The permissions can be assigned to the above users using the
chmod command. You can assign three types of permissions with any combinations which are read, write and execute.
To check the permission of a file you can use the
ls -l filename.txt Output -rw-rw-r-- 1 user group 3793218 Sep 11 17:57 filename.txt ls -l directory Output drwxr-xr-x 5 user group 4096 Sep 15 05:18 sub-directory
Red: Determines the type if file
-, if directory
d, symbolic link
Orange: Determines the permissions for Owner.
Green: Determines the permissions for Group.
Blue: Determines the permissions for Others.
Each operations read, write, execute has their own numbers.
rread = 4
wwrite = 2
xexecute = 1
- no permission = 0
For example, to give read, write and execute permission to the file’s owner, read and execute permissions to the file’s group and only read permissions to all other users, you would do the following:
Owner: rwx = 4+2+1 = 7
Group: r-x = 4+0+1 = 5
Others: r-x = 4+0+0 = 4
Using the method above, we come up to the number
754, which represents the desired permissions.
|3||Execute + Write||-wx|
|5||Read + Execute||r-x|
|7||Read + Write +Execute||rwx|
To set up the
sticky bit flags, you can use four digits number.
When the 4 digits number is used, the first digit has the following meaning:
- setuid = 4
- setgid = 2
- sticky = 1
- no changes = 0
The next three digits have the same meaning as when you are using the 3 digits number.
If the first digit is 0 it can be omitted, and the mode can be represented with 3 digits. The numeric mode
0754 is the same as
You can check the file’s permissions in the numeric notation using the
stat -c '%a' filename.txt Output 754
Here are some examples of how to use the chmod command in numeric mode:
Give the file’s owner read and write permissions and only read permissions to group members and all other users:
chmod 644 directory
Give the file’s owner read, write and execute permissions, read and execute permissions to group members and no permissions to all other users:
chmod 750 directory
Give read, write, and execute permissions, and a sticky bit to a given directory:
chmod 1777 directory
Recursively set read, write, and execute permissions to the file owner and no permissions for all other users on a given directory:
chmod -R 700 directory
Now you have understood how to assign permissions in Linux based systems..
Thanks for your time. If you face any problem or any feedback, please leave a comment below.