Hi Friends,
In this blog, we are going to discuss file permission in Linux.
Let's start the discussion. To understand file permission we need to create a file and directory.
For creating a directory there is a command mkdir and for creating an empty file touch command we need to use.
mkdir <directory name> ==> For creating Directory.
touch <filname> ==> For creating File
Here, I have created a devops directory and devtxt.txt text file.
How to see directory permission in Linux?
ls -ld [directory name]
eg. ls -ld devops
How to see file permission in Linux?
ls -l [filename]
eg. ls -l devtxt.txt
Now, We have understood how to see permission in Linux. Let's try to understand the permission with the help of the below image.
The first column represents the file type. Let's see the symbol and file types:
Symbol | File Type |
d | Directory |
- | Normal File |
b | Block file |
c | Character File |
l | Link Files |
Now Let's discuss permission types.
Read ==> r
If read permission is given to the file it means we can view the content of the file only. If read permission is given to the directory then we can view the content of the directory but we can't change it.
Write ==>w
Write permission means we can view and modify the contents within the file or a directory.
Execute ==> X
Execute permission means we can execute the file,like shell script.we can enter into the directory using cd.
No Permissoin ==> -
How to change the Permission of files or directories?
In Linux, if we want to change the permission of files or directories need to use chmod command.
we can change the permission of files and directories by two methods. symbolic method and Absolute method.
Symbolic method(ugo)
u means User
g means Group
o means Other
eg., A manager told me to add execute permission to user, add write permission for the group and remove read permission for others then we can use like below:
$chmod u+x,g+w,o-r test1.txt
Absolute method
In the case of the absolute method we use numbers to assign permission to a file or directory.
4 => Read
2 => Write
1 => Execute
eg., $chmod 632 test1.txt
Before permission changed.
After Giving Permission
How to change the ownership of a file or a directory?
We can change the ownership of a file using chown command in linux.
chown means change the owner. This activity only root user can perform.
#chown root devtxt.txt
Now root is the owner of devtxt.txt
How to change the group permission of a file or a directory?
To change the group of a file or a directory in Linux. We use chgrp command.This activity also only root user can perform.
#chgrp root devtxt.txt
What is Access control List(ACL)?
As we already discussed about permission in linux then why we need ACL in Linux?.
Let's understand with example.
Suppose there are two users working together.Lest's assume User A and User B
User A can edit the file and execute it but User B can't edit the file.He can read only.
So we can change file permission by using chmod command but then anyone can change the file content.Here ACL comes into picture where it allows to give specific set of permission to file or a directory without changing base ownership and permission.The command which is used in ACL are :setfacl and getfacl
getfacl is used to view the permission in a proper structured format.
setfacl is used to set a permission to a paricular user or a group for a file.
How to add permission for a User?
setfacl -m u:user:rwx devtxt.txt
-m for modification.
How to add permission for a Group?
setfacl -m g:group:rwx devtxt.txt
How to remove Permission?
setfacl -x u:user:rwx devtxt.txt
How to remove all permission?
setfacl -b devtxt.txt
How to give permission to user about all files and directory?
setfacl -Rm u:username:rwx foldername/
Thank You for reading this blog
Parimal Pradhan
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe
#devops #90daysofdevops