Linux Chmod Command Tutorial with Examples To Change Permission of Files and Folders
chmod command is used to change access permission of files and directories in Linux operating systems.
chmod stands for change mode. Access permissions specifies whether a user account or group can read, write or execute given file and directory.
chmod command is like below.
chmod OPTIONS PERMISSIONS FILE
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 exists 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.
In Linux there are three access modes. Access modes specifies 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
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 User 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 Group Permission
We can change a file or directory permission with
g option. In this example we will remove 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 Multiple Roles 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 Permission Recursively
Changing file and directory permissions one by one is a trivial task. We can use
-r option to take effect all sub directories recursively. In the following example we change all given directory named
test and its sub directories permissions.
$ chmod -r g-r test
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
- between role and permission is used to remove permission for 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 files 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 rerence file name. In this example we will use
run file permissions as reference to apply into
$ chmod --reference=run run2
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 6
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 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