If you want to manage permission of files or directories in Linux, all you can do using the chmod command.
Through this command, you can ensure a specific person has certain rights to access, modification, and other things.
This article will guide you through the concept of chmod how you can implement it in your system.
Table of Contents
Chmod stands for change file mode bits.
In a Linux every file has given three properties
r – Read
w – Write
x – Execute
All files are associate with certain permission and attributes to
u – Owner
g – Group
o – Others
Through chmod, you can change the file permission, but if you want to change ownership, you have to use chown or chgrp commands in the terminal.
So, now we will show you some commands with examples. Through this, you will get a better idea.
First of all, if you want to check the file-permission of all files in the current directory.
This command will show all the permission of the file under the directories. If you want to know the permission of a specific file, then type
ls -l nameoffile.txt
You can see the output. I’ll explain to you everything that does mean from left to right direction.
- – This means file type (if d then directory)
- rw – the user has read and write permissions
- rw – group permissions
- r – others permissions
- — access method
- shen owner
- shen group
This output says the user can read and write files. Even groups can do the same, but others have permission to read.
Certain Permission on files
Read permission:- When the character is “-” then the file is not allowed to read. If the character is “r,“ then you can read the file.
Write permission:- When the character is “-” then the file is not allowed to do changes. If the character is “w,” then you can make changes in the file.
Execute permission:– When the character is “-” then the file will not execute. If the character is “x,“ then you can execute the file.
The chmod command allows a user to grant or revoke the read, write, and execute using various methods like numeric mode, symbolic, or reference file.
To change permission is the only right of the root user and owner of the file to change the permission of root user file
sudo chmod -R 775 nameoffile.txt
Symbolic (Text) Method
In symbolic method to set permission to file first we use “u g o a”.
- u – File Owner
- g – Group
- o – Other
- a – All user
After define symbolic link we use operator to define permission like ” – + = “
- – revoke permissions
- + assigned permissions
- = Change the permission
Then define the permission what you want to give like “r, w, x”.
Now I’ll list you certain examples so you will get better idea
Before removing write permission to group
Now I’ll remove the write permission from group to use this command type
$ chmod g=r nameoffile.txt
Now, we will show you how we can change file permission using the numeric method.
We can leverage the number to change all types of permission through this method.
Every operation assigned with certain number
- read = 4
- write = 2
- execute = 1
- no permission define = 0
Total 4 digits are used all the digit used to add and get some of the permissions like usually file owner has permission read(4) + write(2) + execute(1) = 7
Similarly for Groups and Others
- Groups: read(4) + write(0) + execute(1) = 5
- Others: read(4) + write(0) + write(0) = 4
According to your need you can define permission.I’ll show you demo of it
The above image show every user has all the permission. Now I’ll remove the permission from the group and others
$ chmod 700 nameoffile.txt
I have defined 700 indicate that users should have all the permission, but the group and others should not access it.
Commonly Used Permissions
I’ll list some commonly used permissions which is list down
chmod 644:- When you set this permission to file or directories, it means you the only owner to read and write, and Other users can just read, but no user can execute the file.
chmod 655:- In chmod 655 owner can just read and write but no execute. The only change is that other users can perform both read and write, but they cannot modify files.
chmod 775:- This type of permission is mostly used where you want the owner to have all the permission to read, write, and execute. Other users just have read permissions not to write or change files. For example, Web-server file permission is 775.
chmod 777:- When you set the permission 777, in this case, you allow permission to everyone read, write, and execute. With this permission, anyone can make changes or copy files.
It is not advisable to set this kind of permission to the web-server or any certain files.
Through recursively, method user can modify the permission of all the files at once without doing one by one.
Before Chmod – R 777 showcase apply
To use this user just need to type
$ chmod - R 777 /showcase
Reference file method
You can set the same file permission of specified file as of reference given through the reference file method.
$ chmod --reference nameoffile.txt demo.txt
How to change permission through GUI?
To change permission through GUI, you need to open File Explorer, select any file, press the right-click button, and then go to properties.
After this switch to the permission tab in this, you will see Owner, Group, and Others have certain permission,
Just change the permission as per your requirement.
This is the pretty simple way to change permission using GUI.
Through the chmod command, you can grant permission to all users using several method references, numeric, symbolic.
In the reference method, you need to reference a particular file to have the same permissions set.
In the case of numeric you have to assign numeric value each number get to sum up like for read (4) + write(2) + execute(1)
Through a symbolic way, you have to specify user, then it is upon you to use (+, –, = ) permissions using attributes r, w, x.
Whenever you are grant permission to file or directories. Please care what you type. Otherwise, you have to face certain issues.
Please leave a comment So, I can explain to you any other commands or queries.
A man with a tech effusive, who has explored some of the amazing technology stuff and exploring more, While moving towards, I had a chance to work on Android Development, Linux, AWS, DevOps with several Open source tools.
One of my life mottos “Always be lifelong Students.”