Chmod, Chown, and Chgrp commands in Linux
Hey Folks,
Let’s discuss about the Chown and Chmod commands in Linux.
Before we discuss about the Chown and Chmod commands, let’s discuss about the file permissions in Linux.
## File Permissions in Linux
In Linux, every file and directory has three permissions:
1. Read
2. Write
3. Execute
These permissions are given to three entities:
1. Owner
2. Group
3. Others
The owner is the user who created the file or directory. The group is the group to which the file or directory belongs. Others are all the other users on the system.
The permissions of a file or directory can be viewed by using the following command:
ls -l
The output of the above command is shown below:
-rw-r — r — 1 root root 0 Jan 1 00:00 file.txt
drwxr-xr-x 2 root root 4096 Jan 1 00:00 directory
## What is Chmod?
Chmod is a command in Linux that is used to change the permissions of a file or directory. The permissions of a file or directory are changed by using the following syntax:
chmod [OPTIONS] PERMISSIONS FILE(s)
Now let’s discuss about the Chmod command.
Let’s understand how this value is calculated.
| Permission | Value |
| — — — — — | — — — |
| r | 4 |
| w | 2 |
| x | 1 |
Few other things to note:
u — user (file or directory owner, creator)
g — group (the group to which the file or directory belongs)
o — other (other than user and group)
a — all (user, group, other) (all, anyone can access files and directories.)
Example 1: Give read, write and execute permissions to the owner, read and execute permissions to the group and read and execute permissions to others
chmod 755 file.txt
The above command is equivalent to the following command:
chmod u=rwx,g=rx,o=rx file.txt
Example 2: Give read, write and execute permissions to the owner, read and execute permissions to the group and read and execute permissions to others
chmod 755 directory
The above command is equivalent to the following command:
chmod u=rwx,g=rx,o=rx directory
Example 3: Give read, write and execute permissions to the owner, read and execute permissions to the group and read and execute permissions to others
chmod -R 755 directory
The above command is equivalent to the following command:
chmod -R u=rwx,g=rx,o=rx directory
## What is Chown?
Chown is a command in Linux that is used to change the ownership of a file or directory. The ownership of a file or directory is changed by using the following syntax:
chown [OPTIONS] USER[:GROUP] FILE(s)
Example 1: Change the ownership of a file
chown root:root file.txt
The above command gives the ownership of the file to the root user and the root group.
Example 2: Change the ownership of a directory
chown root:root directory
The above command gives the ownership of the directory to the root user and the root group.
Example 3: Change the ownership of a directory and all its subdirectories
chown -R root:root directory
The above command gives the ownership of the directory and all its subdirectories to the root user and the root group.
You must have noticed that the Chown and Chmod commands are very similar. The only difference is that the Chown command is used to change the ownership of a file or directory and the Chmod command is used to change the permissions of a file or directory.
## What is Chgrp?
There is a third command called Chgrp that is used to change the group of a file or directory. The Chgrp command is used to change the group of a file or directory by using the following syntax:
chgrp [OPTIONS] GROUP FILE(s)
Example 1: Change the group of a file
chgrp root file.txt
The above command gives the group of the file to the root group.
Example 2: Change the group of a directory
chgrp root directory
The above command gives the group of the directory to the root group.
Example 3: Change the group of a directory and all its subdirectories
chgrp -R root directory
The above command gives the group of the directory and all its subdirectories to the root group.