×


Chmod Command in Ubuntu 20.04 - How it Works ?

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 Ibmi Media, 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:

  • 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.


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:

  • +: Adds the permission.
  • -: Removes the permission.
  • =: Lets you specify the exact permission.


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:

  • 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.

-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.


[Need assistance in fixing file Permissions issues in any Linux Distribution? We can help you. ]


Conclusion

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:

  • chmod +rwx filename to add permissions.
  • chmod -rwx directoryname to remove permissions.
  • chmod +x filename to allow executable permissions.
  • chmod -wx filename to take out write and executable permissions.



We can use the -l (long format) option to have ls list the file permissions for files and directories.

$ ls -l


chmod Numerical Shorthand:

  • 0: (000) No permission.
  • 1: (001) Execute permission.
  • 2: (010) Write permission.
  • 3: (011) Write and execute permissions.
  • 4: (100) Read permission.
  • 5: (101) Read and execute permissions.
  • 6: (110) Read and write permissions.
  • 7: (111) Read, write, and execute permissions.