in

LinuxLinux

Chmod Command in Linux: File Permission Settings, Syntax & Examples!

The most powerful Linux Command CHMOD!

CHMOD Command in Linux

C

HMOD Command in Linux: chomd is one of the most powerful and important command in Linux. This CHMOD command is mainly used to set, edit and remove the file permission to the user, user group and others. This CHMOD can be expanded as “Change Mode“, the name itself it explains that it is mainly used to change the permission mode of a file. You can use this command to restrict the file by access from others.

Jump To:

CHMOD Command Syntax:

The basic syntax of CHMOD command in Linux is,

chmod options permissions file_name

If you didn’t specify any certain options along with chmod command and then chmod modifies the permissions of the file specified by file_name to the permissions specified by permissions.

permissions define the file permissions of the user, member of the groups and others. You can define the permission in two ways and they are:

  • Alphanumeric characters
  • Octal number representation

The basic alphanumeric symbol syntax of the file permissions is,

chmod u=rwx,g=rx,o=r filename

 From the above syntax:

  • U stands for User (file owner)
  • G stands for the member to the Group
  • O stands for the others
  • “=” assign the permissions to the user, group members, and others
  • r stands for “Read”
  • w stands for “Write”
  • x stands for “Execute”
  • “,” which separates the classes of permissions.

How it Works:

The equivalent octal numeric symbol syntax of the file permission is,

chmod 754 filename

The above syntax also represents the same file permission but in octal numeric representation.

From the above syntax,

Check this too  10+ Ping Command in Linux: Ping Command Syntax & Examples

The digits 7,5,4 is the sum of “4, 2, 1, 0“. Each digit “7, 5, 4” represents the file permission settings of User, members of the group.

  • 4 stands for “Read”
  • 2 stands for “Write”
  • 1 stand for “Execute”
  • 0 stand for “Nothing”

So the number 7 (4+2+1+0) gives read, write and execute permissions to the user, the number 5 (4+1+0) gives read & execute permissions to the members of the group and finally, the number 4 (4+0) gives read permission to the others outside to the file and groups.

Chmod Command Options Table:

You can find the list of options that are available in CHMOD command, follow the instructions below:

  • Open the Terminal
  • Enter the command ” chmod –help

The following chmod options table will appear on the screen.

[email protected]:~$ chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
  or:  chmod [OPTION]... OCTAL-MODE FILE...
  or:  chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.

Chmod Options table:

-c, --changeslike verbose but report only when a change is made
-f, --silent, --quietsuppress most error messages
-v, --verboseoutput a diagnostic for every file processed
--no-preserve-rootdo not treat ‘/’ specially (the default)
–preserve-rootfail to operate recursively on ‘/’
--reference=RFILEuse RFILE’s mode instead of MODE values
-R, --recursivechange files and directories recursively
--helpdisplay this help and exit
--versionoutput version information and exit
Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/chmod>
or available locally via: info '(coreutils) chmod invocation'
[email protected]:~$ 

View File Permissions:

Once you assigned the file permission, you can verify it by view the permissions of the files. To do this, we recommend you to use the ls command in Linux.

Check this too  Wget Command in Linux: Wget Command Syntax, Options & Examples!

Use the ls command to show the file permissions:

$ls -l file.txt

The output of the above command is,

[email protected]:~$ ls -l example.txt
-rw-r--r-- 1 tecrobust tecrobust 34 Aug 17 16:48 example.txt
[email protected]:~$

The complete meaning of the above output is,

  • stands for a regular file. If you get the output as “d” and then it means a directory or if you get the output as “l” and then it means a regular symbolic link.
  • rw- stands for the file have read and write permission for the user. The first 3 character (rw-) represents the file permissions of the user. The user doesn’t have the permission to execute the file.
  • r– This 3 character represents the file permission for the members of the group. In this scenario, The member of groups has the read permission only. They don’t have the permission to write and execute the file.
  • r– This 3 character represents the file permission for the others. Other can read the file and have no further permissions. They don’t have the permission to write and execute the file.
  • stand for the number of hard links to this file.
  • tecrobust stands for the files owner name
  • tecrobust stands for the group where the file belongs
  • 34 stands for the size of the file
  • Aug 17 16:48 stands for the file’s date and time
  • example.txt represents the name of the file.

Chmod Command Examples:

Some essential chmod commands examples,

To set the permission of file “tecrobust.html” to the owner can read and write; the group can read-only; others can read-only.

Check this too  How to Change User Password in Ubuntu 18.04 LTS & 19.04 Disco Dingo
$chmod 644 tecrobust.html

To change the file permission for the owner of the image “flower.png” & so that the owner may read and write the file. Do not change the permissions for the group, or for others.

$chmod u=rw flower.png

To set the file permission of “chrome.deb” to “read, write, and execute by owner” and “read and execute by the group and everyone else”.

$chmod 755 chrome.deb

To set the file to read and write by everyone,

$chmod 666 example.txt

To set and unset the Set-User-ID of a file,

$chmod u+s sample.txt
The above command sets the set user id to the file example.txt.
$chmod u-s sample.txt
The above command unsets the set user id to the file example.txt.

Verdict:

That’s it. You have reached the conclusion section. As we said earlier CHMOD is one of the most useful commands in Linux. Try to learn the options of the chmod command and use the chmod command to set the permissions to your files. Set the file permission to the user, member of the group and others with chmod command. You can view the permission with the help of ls command. If you have any queries related to this topic and then feel free to comment us below or drop us the question in our Forum.

Article Review
  • Article Explanation
  • Commands Provided
  • Chmod Options Table
  • Chmod Example
4.9

Summary

For more articles, Kindly visit our Homepage

Leave a Reply

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

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@] gmail.com
Sub: Message to Julian

cmake ubuntu

Cmake Ubuntu- How To Install Cmake on Ubuntu 18.04 LTS & 19.04

Grep Command in Linux

Grep Command in Linux: Syntax, Options & Grep Examples Commands!