Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Have you ever used Linux commands? If yes, you may know that we can also manage the accessibility of a file using a simple chmod command in Linux. The chmod command is an essential tool for managing file and directory permissions. Well, If you don’t have an idea about this command, don’t worry; we have your back!
Let us dive into the world of chmod and gain a better understanding of how to manage file permissions in Linux. Being a tech enthusiast, you know the importance of accessibility to a file system. The chmod command in Linux is used to modify the access mode of a file. Change mode is abbreviated as the chmod command in Linux. This means that every file and directory has a set of permissions to enable the read, write, and execution of the file.
The permissions defined by chmod command in Linux are divided into three categories.
‘r’: The read permission is denoted by the letter r.
‘w’: The write permission is denoted by the letter w.
‘x’: The execute permission is denoted by the letter x.
These letters combine to form a special permission for a specific group of users. Let's have a look at the syntax and options used for the chmod command in Linux.
Syntax Chmod command in Linux?
The syntax used by chmod command in Linux are as follows:
chmod [options] [mode] [File_name]
The parameters used here are kinda self-explanatory, although we are going to see each parameter in detail.
Chmod Command Options
The options used in chmod syntax are the options we have that we can do on the file. It means they are a set of functions provided by the chmod command to help us with the functionality.
Options
Description
-v, --verbose
It will output a diagnostic for every file processed.
--help
It displays help and exit.
-c, --changes
It is much more verbose but reports only when a change is made.
-R, --recursive
It applies the permission change to all files and directories recursively.
-c, --reference=RFile
It uses RFile's mode instead of MODE values.
--version
It outputs the version information and exit.
-f, --silent, --quiet
It suppresses most error messages.
Modes for Chmod Command in Linux
Now, the next parameter used in the chmod command in Linux is the modes. When discussing modes of chmod, you should know that they are the permission commands of the chmod commands. They are actually used in setting permissions to new files and directories.
The modes for chmod command in Linux can be specified in two ways, the symbolic mode and the octal or numeric mode. We’ll discuss both of the modes in detail.
1. Symbolic Mode
Coming to the mode in the chmod command is the symbolic mode. It is the most commonly used approach for establishing file permissions. To set or command what we are going to do with permissions, we must use both letters and operations here.
Below operators can be used in symbolic mode:
Operators
Definition
“+”
It is used for adding permissions.
“-”
It is used for removing permissions.
“=”
It is used for setting the permissions to specified values.
Below letters that can be used with symbolic mode:
Letters
Definition
“r”
It is used to give read permission.
“w”
It is used to give write permission.
“x”
It is used to give execute permission.
Below References can be used in symbolic mode:
Reference
Class
u
It is the reference for the owner.
g
It is the reference for the group.
o
It is the reference for others.
a
It is the reference for all(u,g,o).
Examples of Symbolic Mode:
Firstly, let’s check the list of all files that we have in our system. For this, we have created a single file file.txt in our system. You can also check all the text files in your system with this command:
ls -l *.txt
Let’s look at some important examples of symbolic mode in chmod command in Linux:
Adding read, write, and execute permission to the file owner. The letter ‘u’ stands for ‘owner’ of the file. The letter ‘r’ is for read, ‘w’ is for write, and ‘x’ is for execute. The + operator is used to add permission.
chmod u+rwx [file_name]
The file permission after running the above command will look like this:
2. Removing the written permission for the group or others. The letter ‘g’ stands for group, whereas the letter ‘o’ and ‘w’ stands for other and group, respectively. The ‘-’ operator is used to remove permission.
chmod go-w [file_name]
The file permission after running the above command will look like:
3. Adding the read-and-write permission for the owner and read-only permission for the group and others. Here the letter ‘u’ stands for the owner, and ‘+’ adds the permission to read and write. For the next term, we are adding read permission for the group and others.
chmod u+rw,go+r [file_name]
The file permission after running the above command will look like this:
2. Octal Mode
Coming to the first mode is the octalor numericmode. It can also be used to specify permissions. We specify permission using a three-digit number in this mode. All the digit specifies a different set of permissions:
The first digit specifies the Owner's permission.
The second digit specifies the Group permission.
The permission for Others is specified by the third digit.
Value
Permission
First Digit
It shows the owner's permission.
Second Digit
It shows the group's permission.
Third Digit
It shows others permission.
Examples of Octal Mode
Here are some examples of using the octal mode with the chmod command:
chmod 777 file.txt The chmod 777 command gives all permissions to everyone for the file.
chmod 644 folder/ The command 644 gives the owner read and write permissions. Also, gives read-only permissions to everyone else for the folder.
chmod 755 script.sh The chmod 755 command gives the owner full permissions. It gives read and executes permissions to everyone else for the script.
Frequently Asked Questions
What is the use of chmod 777 command in Linux?
The chmod 777 command in Linux is used to give all users (owner, group, and others) full permissions (read, write, and execute) to a file or directory.
Is chmod 3 or 4 numbers?
`chmod` can use both three or four numbers. 3-number modes set permissions for the user, group, and others. 4-number modes add a leading digit for special permissions like the setuid, setgid, or sticky bits.
What does chmod 755 file?
chmod 755 file means granting the file's owner full permissions (read, write, and execute), and read and execute permissions to the group and others.
What is 400 in chmod?
In `chmod`, 400 sets file permissions so that the owner has read-only access, while the group and others have no permissions. It's represented as `r--------`, meaning only the owner can read the file.
Conclusion
This article briefly discussed the chmod Command in Linux. We have discussed the accessibility of files using the chmod command. We also covered its syntax and modes available in chmod command. You can check out our other blogs to enhance your knowledge: