The change of mode or chmod command lets you change the access mode of files in Linux. This lets you decide who can access and run files. We will be using chmod to change the permissions of files and directories in Ubuntu 20.04.
To view the permissions for a file, use:
ls –l
What does all this mean?
The first character identifies if it’s a file (-) or a directory (d).
Next is the permissions. The first three characters show the owner’s permissions, the next three the group’s permissions, and the last three others’ permission. The table below explains the user types.
User | Description |
Owner | this is the user who owns the file |
Group | these are users who are part of a user group |
Others | these are users other than the owner and group members |
Let’s look at the user permissions in Linux. There are three basic permissions; read (r), write (w), and execute (x). Read means the user can only read or view the file. Write allows users to edit or delete a file. Execute allows users to execute the file. As you can see in the image above, the permissions are either represented as characters or -. The “–“ (dash) means the users don’t have permission.
The syntax of chmod is:
chmod {users}{operator}{permission} {filename}
Operators let you specify the permissions. We have three operators:
+: Adds the permission
-: Removes the permission
=: Lets you specify the exact permission
chmod has two modes, the symbolic and numeric modes. We will cover both.
Symbolic Mode
Let’s look at the file “hello.c”. The user has all three permissions.
If we want to change the permissions so that only the owner can read and write the file, while the group and others have read permission, we will use:
chmod u=rw,og=r hello.c
To check if the permissions updated, use,
ls –l {filename}
If you want to add the execute permission for the owner, we use:
chmod u+x hello.c
If we view the permissions now, we can see the execute permission for the owner.
If we want to set the execute permission for all, we use:
chmod a+x hello.c
If we view the permissions now, we can see the execute permission for all users.
Numeric Mode
You can use a three-digit number to give permissions using chmod. Here is how it works, the leftmost digit represents the permission for the owner, the middle one is for group members, and the rightmost is for others.
The permissions are as follows:
Read = 4
Write = 2
Execute = 1
No permissions = 0
The table below summarizes the permissions.
7 | All permissions | 4+2+1 |
6 | Read and write | 4+2+0 |
5 | Read and execute | 4+0+1 |
4 | Read only | 4+0+0 |
3 | Write and execute | 0+2+1 |
2 | Write only | 0+2+0 |
1 | Execute only | 0+0+1 |
0 | No permission | 0+0+0 |
Let’s use this method to set permission for ABC.txt.
If we want the owner to have all permissions and group and others to have read permissions, we will use:
chmod 744 ABC.txt
To check if the permissions updated, use,
If you want to recursively change the permissions of all the files within a directory, use:
chmod –R {permissions} {filename}
This command gives the owner all three permissions and groups and others no permissions.
If you want to know more about chmod, use:
man chmod
This will take you to the manual that has all the details on this command.
We covered both the symbolic and numeric mode of the chmod command in this article.