chmod command is used to change access permission of files and directories in Linux operating systems.
chmod stands for change mode. Access permissions specify whether a user account or group can read, write, or execute a given file and directory.
chmod Command Syntax
chmod command is like below.
chmod OPTIONS PERMISSIONS FILE
Linux File and Folder Access Roles
While managing access permission for files and directories we should specify the roles. Permissions are set and bind to the given roles. In Linux following roles exist for access permissions.
ownerspecifies the file or directory owners. It can be listed with
groupspecifies the file or directory group owners. Files and directories group owners can be listed with
otherspecifies all user accounts and groups which means all other world. While giving privilege to this role be cautious.
Linux File and Folder Access Modes
In Linux, there are three access modes. Access modes specify the way given user or group access to the file or directory.
readmode specifies reading file or directory.
writemode specifies writing and changing file or directory
executemode specifies executing file
List File and Folder Access Permissions
Before starting to change permissions we need to list current roles and permissions about files and directories. We will use
ls -lcommand to list permissions. In this example we will list all current working directory file and folders permissions.
$ ls -l
We can see that owner user and group is
ismail and the first column like
--rw-r--r-- shows user, group and other privileges.
-is user or group bit
rw-is owner permissions. Here
r--is group permission. Only
r--is other permission. Only
Change File and Folder User Access Permission
Now those provided information to understand file and directory permissions is enough. We will change single directory permission in this example. We will change user permission of file named
ping.txt . We will use
u to specify user.
+ means add permission.
x mean execute permission.
$ chmod u+x ping.txt
Change File and Folder Group Access Permission
We can change a file or directory permission with
g option. In this example, we will remove the group read permission of file
$ chmod g-r ping.txt
gused to specify group
-is used to remove given permission
ris used to specify read permission
Change File and Folder Multiple Group and User Access Permissions
We can change in a single chmod command multiple roles permissions. We will just delimit them with a command.
$ chmod g-r,u+x ping.txt
Change File and Folder Access Permission Recursively
Changing file and directory permissions one by one is a trivial task. We can use
-r option to take effect all subdirectories recursively. In the following example, we change all given directory named test and its subdirectories permissions.
$ chmod -r g-r test
Add File and Folder Access Permission
We have already seen but I want to emphasize add permission operation.
+ between role and permission used to add permission to the given role. In this example we add permission to the user to execute ping.txt
$ chmod u+x ping.txt
Remove File and Folder Access Permission
- between role and permission is used to remove permission for the given role. In this example we remove users execute permission from file ping.txt
$ chmod u-x ping.txt
Copy Permissions From Other File
If we have already set some file permissions we can use this file as a reference point for permission. We can copy given file permissions to the specified file. We will use
--reference option and the reference file name. In this example, we will use run file permissions as a reference to apply to
$ chmod --reference=run run2
Using Numeric Access Permissions
Up to now we have user
o for roles and
r for permissions. There is another presentation for these. We will use three digits for
o role specification like 540 . Here
5 is for user ,
5 is for group
So if we want to give
w we will sum their numeric values which is
Here some examples
x wequal to 3
r xequal to 5
Using Numeric Values For Chmod
We can use previously explained numeric values for
chmod . We will only provide related value to the command. In this example we only want the user to read , write, and execute permissions and others not.
$ chmod 700 executable
Give All Permissions To All Roles
We can give all permission to all roles which means user, group and others can read, write and execute. This is very insecure and dangerous action. We will use
$ chmod 777 file