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.
- Owner
- Group
- 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
command.
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
Explanation:
r
readw
writex
execute
–rw-rw-r–
Red: Determines the type if file -
, if directory d
, symbolic link l
.
Orange: Determines the permissions for Owner.
Green: Determines the permissions for Group.
Blue: Determines the permissions for Others.
Numeric Method
Each operations read, write, execute has their own numbers.
r
read = 4w
write = 2x
execute = 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.
Number Permission Type Symbol 0 No Permission — 1 Execute –x 2 Write -w- 3 Execute + Write -wx 4 Read r– 5 Read + Execute r-x 6 Read +Write rw- 7 Read + Write +Execute rwx
To set up the setuid
, setgid
, and 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 754
.
You can check the file’s permissions in the numeric notation using the stat
command.
stat -c '%a' filename.txt
Output
754
Chmod Command
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
Conclusion
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.