X

How To Change File Permissions In Linux Using ‘chmod’ Command?

An organization is the most salient aspect developed by a human being. Likewise, in the hosting field, you need specific commands for accessing, altering and executing certain operations.

These operations include setting file permissions in Linux for preventing any kind of internal anarchy.

How To Change File Permissions In Linux Using 'chmod' Command
How To Change File Permissions In Linux Using ‘chmod’ Command

The highly productive Linux system offers various levels of permission to ensure that the user has enough ways to interact with files and directories. Users can simply modify file permissions using the chmod (change mode) command.

chmod

The name speaks for itself.

Even though you understand the meaning behind using this command, it is important that you know everything regarding how can you use chmod and what does it allow you to change.

Permission can either be granted or be rejected, it’s a one-way lane. However, users demand permission for either reading (r), writing (w) or executing (x) the file. Permissions will vary on the basis of these three aspects.

Similarly, for directories, r will refer to reading the contents present in the directory, w will refer to writing into the directory by the creation of subdirectories or files and x will refer to entering into the directory.

The permission levels are going to be defined separately for the owner (u), the person to whom the specific file or the directory belongs, the group (g) to which the owner (u) belongs, the other (o) individuals or just all (a) individuals at a time.

ls

ls is another command that is briefly related to the files and directories of a system. The command is used for obtaining a long format listing. It is used as follows:

1
 $ ls -l my_file

After executing the following command line, you’ll obtain this result:

1
 -rwxr-x--x 1 Bradley corp 729 Apr 01 01:01 my_file

Each symbol in the output result has a unique meaning to itself.

  • “-” states that ‘my_file’ is indeed a file present in the directory, for directories the first character is ‘d’.
  • “rwx” states that the owner of the file has permission to read, write and execute the file.
  • “r-x” states that the members present in the same group as the ower can read and execute the file but cannot write it.
  • “–x” states that other users can just execute the file but cannot read or write to it.

Execution of chmod Command

You can simply use the chmod command like this:

1
 $ chmod 771 my_file

The output result will be:

1
 $ chmod g+w my_file

Even though chmod has the meaning as that of ls command, it has a certain individual ability that separates it from the latter command.

The chmod receives commands to change the permissions of the file either by the use of letters or by the use of numbers. Here,

  • Read (r) permission is given the value of 4
  • Write (w) permission is given the value of 2
  • Execute (x) permission is given the value of 1

In the ls command example, Bradley, the owner of the file has been given the permission to perform read, write and execute the operation with the my_file file as the first number 7 stands for (4+2+1) all the three operations.

The corp group has the permission to read and write (4+1) the my_file file and other users can only execute (1) the file.

However, while executing the chmod command, we changed the permission from 751 to 771 which now gives further benefits to the corp group.

After the execution of chmod command, now the owner Bradley can read, write and execute the file (4+2+1), the group corp can also read write and execute the file (4+2+1) and other users can only execute (1) the file.

Likewise, you change permissions given to the owner, group and other users. You can refer to this table for obtaining more information regarding the change in permissions with the use of numbering:

Additional Uses Of chmod Command

The other few uses of chmod are:

-c: The command is used to report whenever a certain is made.

-f: The command is used to suppress error messages.

-v: The command is used to output diagnostics for every directory and file that has been processed.

-R: The command is used to recursively apply the same permissions to a certain directory in addition to all the files and subdirectories in that particular directory and its various subdirectories.

For an instance,

1
 $ chmod -R a+rwx /mydirec

This command states that everyone (a) has the permission to read, write and execute permission for ‘mydirec’ directory and all the files and subdirectories present in it.

If you have any queries regarding the issue, feel free to ask it in the comments section. We’ll get back to you as soon as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *