How to Change File Permissions in Linux With chmod Command With Example

File permission in Linux is a set of rules that provide access to files and directories to a specific user group. The permission ensures that the files and directories are only accessible to authorized users and protects sensitive information from unauthorized access.

chmod command in Linux is used to change the file permission. chmod command supports both numeric and symbolic notation to manage permissions in Linux. chmod command manages permission for owner, group, and user separately.

In this blog post, we will learn how to manage file permission with chmod command in Linux.

Understanding File Permission in Linux

There are three types of permission in Linux, read, write, and execute. And each permission has a distinct definition

PermissionSymbolDescription
ReadrAllows users to read, view, and download the files.
WritewAllows users to write content, create, change, append, or delete files.
ExecutexAllows users to execute the files, open an application, or run a script.
No permission-Restrict users from reading, writing, or executing files or directories.

Basic Syntax of the chmod Command

The basic syntax of the chmod command is

chmod [OPTION] MODE[,MODE] FILE
  • OPTION is a set of flags used to modify the behavior of the chmod command.
  • MODE The permission mode you want to assign
  • FILE The files that you want to change the permission

Here is a reference table of useful chmod command option

OptionDescription
-vProvides details information on what is happening behind chmod
-cThis option is alike verbose but provides details only when the change is made
-Rchange the permission of a directory and its entire content.
--helpProvide a short description of each supported option

Note: This is not a complete list. You can find the complete option with man chmod command.

Change File Permission With Symbolic Notation

Symbolic notation represents file permission using characters and symbols. Symbolic notation provides easy to understand representation than numeric notation.

Symbolic file permission consists of four characters. Each character represents permission to the owner, group, others, and all respectively. Here is a reference table to represent the owner, group, and user in numeric notation.

UserSymbol
Owneru
Groupsg
Otherso
All usera

There are several symbols used in symbolic notation to manage file permissions. Each symbol represents a different operation. Here is a reference symbol table with a short description of their operation.

SymbolDescription
+Add permission to specified user.
-Remove permission from specified users.
=Sets the specified permissions, replacing any existing permissions.

Assigning Symbolic Permissions with chmod Command

You can use chmod command to assign file permission using symbolic notation. Assigning and modifying file permissions using symbolic notation is intuitive and easier to use than numeric notation. The basic syntax of chmod command with symbolic notation

chmod user+permission file
  • user: User presents to whom permission would apply. It may be the owner (u), group (g), or others (o).
  • permission: The mode of permission to apply. It can be read (r), write (w), execute(x), or multiple permission (rwx)
  • +: Add permission to specified user. It also can be remove permission (-) or set specific permission (=)
  • file: The file you want to set or modify permission

Examples of Changing File Permission With Symbolic Notation

Example 1: To set read, write, and execute permission to owner and read permission to group and others.

chmod u+rwx,go+r myfile.txt

You can use ls -l command to see the permission for files and directories.

chmod command example

Example 2: Suppose you want to modify the permission, remove execute permission from the owner, and add write permission to the group while keeping other user permission unchanged.

chmod u-x,g+w myfile.txt
chmod command example 2

Example 3: If the file contains sensitive information, you may want to restrict access for groups and others. In this case, you can remove the permission from them.

chmod og-rw myfile.txt
chmod command example 3

Example 4: Now you have changed your mind and want to change to only read permission to all users, including owners, groups, and others.

chmod a=r myfile.txt
chmod command example 4

Example 5: Suppose you have a folder with multiple files inside the folder. Now you want to change the permission to the folder and its content. In this case, if you change permission to the folder as in previous examples. You will only change permission to the folder, while files inside the folder will be unaffected.

To change the permission of a directory and its content, you need to use -R the recursive option along with symbolic or numeric notation.

chmod -R a=w folder
chmod command example 5

Change File Permission With Numeric Notation

chmod command supports numeric notation to change file permission. chmod command takes three numeric digits to change the permission. where

  • 1st digit represents permission to the owner,
  • 2nd digit represents permission to groups and
  • 3rd digit represents permission to others.
Chmod command numeric notation

Numeric number 0 to 7 is used to modify the file permission, where each numeric number represent a specific permission. Here is a reference table of each numeric number and associated permission mode

Permission ValueBinary
(rwx)
Permission Description
0000No permissions
1001Execute permission only
2010Write permission only
3011Write and execute permissions
4100Read permission only
5101Read and execute permissions
6110Read and write permissions
7111Read, write, and execute permissions

For example, here is a chmod example that sets permission to 775 to file

chmod 775 file

As you guess, this numeric notation 775 sets permission to read, write, and execute permission to the owner. It also sets read and execute permission to groups and other users.

Examples of Changing File Permission With Numeric Notation

Example 1: To set read, write, and execute permission to owner and read permission to group and others, you have to use numeric notation 744.

chmod 744 myfile.txt
  • 7: Represent read, write and execute permission of the owner
  • 4: Represent read-only permission to group and user
chmod command example 6

Example 2: Suppose you want to modify the permission, remove execute permission from the owner, and add write permission to the group while keeping others’ user permission unchanged. In this case, you have to use the numeric notation 666.

chmod 666 myfile.txt
  • 6: Represent read and write permission to owner, group, and users.
chmod command example 7

Example 3: If the file contains sensitive information, you may want to restrict access for groups and others. In this case, you can remove the permission from them. In this case, you have to use the chmod 600.

chmod 600 myfile.txt
  • 6: Represent read and write permission to the owner.
  • 0: Represent no permission to groups and users.
chmod command example 8

Example 4: Like symbolic notation, if you want to change permission to a directory and the files inside the directory, you have to use the recursive option -R and chmod command.

chmod -R 777 folder

Conclusion

chmod command is a handy command line tool to manage file permission in a Linux system. Both symbolic notation and numeric notation serve the same purpose. While symbolic notation is, intuitive numeric notation is concise. I will recommend symbolic notation as it is easy to understand.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

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