In Linux operating system, the chmod command is used to change the access mode of a file.
chmod is an abbreviation of change mode.
Basically, 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.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related chmod queries on Linux System.
In this context, we shall look into how to use chmod to change the permissions of files and directories in Ubuntu 20.04.
1. Viewing system file Permissions
To view the permissions for a file, use the command:
$ ls –l
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:
2. 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.
The permissions are either represented as characters or -. The "–" (dash) means the users don't have permission.
3. Chmod syntax
The syntax of chmod is:
$ chmod {users}{operator}{permission} {filename}
Operators let you specify the permissions.
We have three operators:
chmod has two modes, the symbolic and numeric mode.
i. Symbolic Mode
Let's look at the file "hello.c". The user has all three permissions.
-rwxr - xr-- 1 john john 20 May 21 20:20 hello.c
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.
ii. 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:
The table below summarizes the permissions:
Let's use this method to set permission for ABC.txt.
-rwxr - xr-- 1 john john 20 May 21 20:20 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,
$ ls -l ABC.txt
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.
This article covers both the symbolic and numeric mode of the chmod command. Control who can access files, search directories, and run scripts using the Linux's chmod command. This command helps modifies Linux file permissions.
In Linux, who can do what to a file or directory is controlled through sets of permissions. There are three sets of permissions. One set for the owner of the file, another set for the members of the file's group, and a final set for everyone else.
To change directory permissions in Linux, use the following:
We can use the -l (long format) option to have ls list the file permissions for files and directories.
$ ls -l
chmod Numerical Shorthand: