Beginners Guide for Chmod Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The chmod command is used to modify the access permissions of files and directories. It can modify the read, write, and executable permissions, which can help you control shell script execution or specific file modifications.

In this article, you will learn how to change referenced file or directory permissions using the chmod command in Linux (with practical examples).

Tutorial Details

DescriptionChmod (Change Mode)
Difficulty LevelModerate
Root or Sudo PrivilegesYes (For non-file owners)
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteschmod
Internet RequiredNo

The chmod command is used with different options, references, operators, and modes that can help you assign different permissions to your files.

The following are all valid options supported by the chmod command:

OptionsDescription
-c, --changeIt will report when the changes are identical to the verbose option.
-f, --silent, --quietIt will suppress the most error messages.
-v, --verboseIt will display the diagnostic for every processed file.
--no-preserve-rootIt is used to avoid special treatment of the backslash symbol (‘/’) (the default).
--preserve-rootIt will fail to recursively operate on backslashes (‘/’).
--reference=RFILE:It is used to specify the RFILE’s mode instead of MODE values.
-R, --recursiveIt is used to change files and directories recursively.

The following is a list of references that are used to distinguish between users, groups, others, and all.

ReferenceClassDescription
uownerOwner of the file or directory.
ggroupUser who are the members of the file group.
oothersUsers who are not the file owner or not members of the file group.
aallAll of the above.

The operators are used to add or remove access permissions to the file or directory.

OperatorsDescription
+It will add the specified mode to the selected file.
-It will remove the specified mode from the selected file.
=It will add the specified mode and remove the unmentioned mode.

The modes are the access permissions that can be given to a file or directory or taken away from them.

ModesDescription
rPermission to read the file.
wPermission to modify or delete the file.
xPermission to execute the file.

Instead of specifying the references, operators, and modes to modify the access permission for a file or directory, you can use the numerical shorthand.

The first digit represents the file owner (u), the second digit represents the group members (g), and the third digit represents the others (o).

The following is a list of all values and their permissions.

ValuesPermissions
0No permission
1Execute permission
2Write permission
4Read permission

Setting Permissions using the References, Operators, and Modes

The following is an example of modifying file or directory permissions using references, operators, and modes.

For now, we will perform all the permissions modifications on “file.txt“.

Check the following output to find its current permission.

$ ls -l file.txt 

Output:

Checking the current file permissions

Right now, the “file.txt” owner and group have read and write permission, and others have read permission.

The following command will give the “file.txt” owner read, write, and executable permissions, and members of the group and other users will have the read permission.

$ chmod u=rwx,go=r file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 1

The following command will give the “file.txt” owner and member of the group permissions, and other users will have no permissions.

$ chmod ug=rwx,o= file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 2

The following command will remove all permissions from “file.txt” for the owner, members of the group, and other users.

$ chmod ugo= file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 3

The following command will only give the owner access permission to read, write, and modify the file.

$ chmod u+rwx file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 4

The following command will remove the executable permission for the owner and give read and write permission to other members of the group and other users.

$ chmod u-x,go+rw file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 5

Setting Permissions using Numerical Shorthand

The following command will give the owner only read permission and no permission for members of the group or other users.

$ chmod 400 file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 6

The following command will give the owner read, write, and executable permission but no permission to other members of the group or other users.

$ chmod 700 file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 7

The following command will remove the executable permission from the owner and give read permission to members of the group and other users.

$ chmod 644 file.txt
$ ls -l file.txt 

Output:

Modifying the file permissions - example 8

The following command will give the owner all permissions and read and write permissions to members of the group and other users.

$ chmod 766 file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 9

The following command will remove all the permissions for the owner and only give read permission to members of the group and other users.

$ chmod 044 file.txt 
$ ls -l file.txt 

Output:

Modifying the file permissions - example 10

I think this is a good enough example to make it clear how and when to use them.

If you struggle while using or understanding them, feel free to ask for help in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.