How to use Chmod command in Linux Explained with Examples

Linux file permission secures files and directories from unauthorized access. When we create a new file or directory, Linux automatically assigns a default file permission. The chmod command allows us to change and customize the default file permission based on our requirements. This tutorial explains this process through examples.

Linux file permission categorizes users into three types: owner, group, and other. Each type gets three permissions: read, write, and execute. These permissions have different meanings for files and directories. The following table lists the actions a user can perform with these permissions.

Permission FileDirectory
ReadView the file dataList the directory contents
WriteEdit the file contentsCreate a new file or directory
ExecuteRun the fileNavigate the directory

Each user type gets these permissions independently. That means for three user types, there will be a total of nine permissions. The file permission maps the permissions with the user types in the following sequences.

Owner (Read, Write, Execute), Group (Read, Write, Execute) and Other (Read, Write, Execute)

file permission

Viewing the current file permission

The ls -l command shows the current file permission of the specified file. If we provide a directory, it lists all contents of that directory line-by-line. It lists each content in a separate line. The first field of each line shows the file permission.

ls command

The chmod command

The chmod command allows us to change the file permission. It uses the following syntax.

#chmod [new permission] [file/directory]

Specifying new permissions is tricky and complex. It uses characters and numbers to denote permissions and user types. The following table lists these characters and numbers.

User type Permission type
NameCharacterNameCharacterNumber
OwneruReadr4
GroupgWritew2
OtheroExecutex1
  • The letter a represents all users.
  • User types do not have numeric values.
  • A hyphen represents no permission.
Modifying/updating file permissions

We can use either characters or numbers to specify the permission. The following syntax assigns the new file permission to the specified file or directory using characters.

#chmod [user type] [+/-] [permission type] [file/directory]
  • The plus (+) sign adds the permission.
  • The minus (-) sign removes the permission.

Example

Login with the root account.
Create a test directory and change the current directory path to it.
Create a test file and check the default file permission it gets.

#mkdir /testdir
#cd /testdir
#touch testfile
#ls -l testfile

default file permission

Run the following commands and verify the new file permission.

Command Description
chmod u+x testfile Add the execute permission for the owner
chmod g+wx testfile Add the write and execute permissions for the group
chmod o+wx testfile Add the write and execute permissions for others
chmod o-wx testfile Remove the write and execute permissions for others
chmod g-wx testfile Remove the write and execute permissions for the group
chmod o-x testfile Remove the execute permission for the owner

the chmod command examples

We can specify multiple permissions with a single command. Use a comma to separate them. For example, the following command first adds the write permission for the group and then removes the execute permission for others.

#chmod g+w,o-x test-file

Linux file permissions

The following command adds the read, write and execute permissions for the owner, the read and write permissions for the group, and the execute permission for others.

#chmod u+rwx,g+rw,o+x test-file

changing multiple permissions

If we omit the user type, the specified permission applies to all user types. For example, the following command adds the read permission to the owner, group, and others.

#chmod +r test-file

This method works with the read and execute permissions but not with the write permission. It does not add the write permission to the group and others. For example, the following command adds the write permission only to the owner.

#chmod +w test-file

no write permission

To apply the same permission to all user types, use the user type a (all). The following command removes all permissions from all user types.

#chmod a-rwx testfile

The following command adds all permissions to all user types.

#chmod a+rwx testfile

changing permission for all

Using numbers for the file permissions

The chmod command uses a different approach for numbers. It assigns three places in the sequence. The first place represents the owner. The second place shows the group. The third place denotes others. For example, the following command adds the write permission to the owner, the read permission to the group, and the execute permission to others.

#chmod 241 testfile

You can add or remove related numbers to assign or remove multiple permissions. The following table lists the numbers we can use.

Number Permissions
0 No permission
1 Execute
2 Write
3 (1+2) Write and execute
4 Read
5 (1+4) Read and execute
6 (2+4) Read and write
7 (1+2+4) Read, write and execute

If we omit the number in any field, it automatically adds a zero in that field, starting from the left. The following command omits the owner and group fields.

#chmod 7 testfile

The chmod command reads the above command as follows:

#chmod 007 testfile

Examples

Command Description
#chmod 751 testfile Owner (Read, Write, Execute), Group (Read, Execute), Others (Execute)
#chmod 000 testfile Owner (No permission), Group (No permission), Others (No permission)
#chmod 777 testfile Owner (Read, Write, Execute), Group (Read, Write, Execute), Others (Read, Write, Execute)
#chmod 666 testfile Owner (Read, Write), Group (Read, Write), Others (Read, Write)
#chmod 653 testfile Owner (Read, Write), Group (Read, Execute), Others (Write, Execute)

using numbers for permissions

This tutorial is part of the tutorial " Linux file permission Explained with Examples.". Other parts of this tutorial are as follows:

Chapter 1  Linux File Permission Explained in Easy Language
Chapter 2  How to use chmod command in Linux Explained with Examples
Chapter 3  How to change default umask permission in Linux
Chapter 4  SUID, SGID, and Sticky Bit Explained
Chapter 5  How to set immutable bit with chattr command

Conclusion

File permission defines how a user can access a file or directory. The chmod command allows us to manage the file permissions of the file or directory. It accepts new file permission in two formats: letters and numbers. This tutorial explained both through various examples.

ComputerNetworkingNotes Linux Tutorials How to use Chmod command in Linux Explained with Examples

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com