The Linux command to change permissions on a file or directory is chmod, which we like to read as change file mode. chmod has two operating modes:
- symbolic mode
- numeric/octal mode
To change permission using the Linux chmod command we have to follow some syntax and rules. As we discussed above we can change permission using Numerical and Alphabetical way, here I have explained both methods with all tricks.
To use this method you have to remember below rules and alphabets for proper use. To change permission of user you have to use the alphabet “u“. Like that for Group “g” and for others it’s “o“.
Like User, Group and Others we have to remember some alphabets to give Read, Write and Execute permission Like for Read permission you have to use the alphabet “r“, for Write “w” and for Execute it’s “x“.
So the formula for assigning permissions using chmod command is shown below :
- u – User
- g – Group
- o – Others
- r – Read
- w – Write
- x – Execute
For Example if you want to give Read & Write permission to User/Owner and Read permission to Group & Others using Alphabetical way then the command would be:
# chmod u+rw,g+r,o+r file_name
To use this method you have to remember below Rules and Numbers for proper use.
- 4 – To give Read Permission
- 2 – To give Write Permission
- 1 – To give Execute Permission
For Example, if you want to give Read & Write permission to User/Owner and Read permission to Group & Others using Numeric way then the command calculation would be:
4+2 = 6 : This will add Read & Write permission to User/Owner.
4 : This will add Read permission to Group.
4 : This will add Read permission to Other.
So the command would be like:
# chmod 644 filename
You can check permission of any file using below command :
# ls -l file.txt -rw-rw-r-- 1 geeklab geeklab 0 Feb 20 06:16 file.txt
And use the below command to check permission of a directory :
# ls -ld data/ drwxrwxr-x 2 geeklab geeklab 4096 Feb 20 06:18 data/
Now here the question is – how to Identify what permissions been assigned to a file or a directory. It’s quite simple. Above I have given two examples. Now let’s identify the permission of file.txt. If you notice the properties of the file starts with “-rw-rw-r–“. Here we can identify what permission assigned to file.txt.
The permission section is divided into 10 bits out of which
“–” : First bit is responsible to identify whether it’s an file, directory or a Symbolic Link. Refer the symbols below:
- – : Permission assigned to a File
- d : Permission assigned to a Directory
- l : Permission assigned to a Symbolic Link
- 2nd, 3rd, 4th bit’s are for permission of Owner.
- 5th, 6th, 7th bit’s are for permission of Group.
- 8th, 9th, 10th bit’s are for permission of Others.
To Give permission to any file/directory you have to use “+” Symbol. To remove permission from any file/directory you have to use “–” Symbol.
Examples of Using chmod command
Assign permission to a File
Here I have a file named file.txt. Let’s give Read and Write permission to User/Owner using chmod command.
$ chmod u+rw file.txt
$ ls -l file.txt -rw------- 1 geeklab geeklab 0 Feb 20 06:16 file.txt
By Numerical Way use the below command :
$ chmod 600 file.txt
Assign Permission to a Directory
The below command can be used to give permission to a directory. Here I have a directory named data. So let’s give Full Permission (Read, Write, Execute) to User/Owner. Refer the command below.
$ chmod u+rwx data/
$ ls -ld data/ drwx------ 2 geeklab geeklab 4096 Feb 20 06:18 data/
$ chmod 700 data/
Remove permission from a file/directory
Remove permission from a file/directory using below Linux chmod command. Here I am removing Read and Write permission from User/Owner.
$ chmod u-rw file.txt
$ ls -l file.txt ----rw-r-- 1 geeklab geeklab 0 Feb 20 07:18 file.txt
Give permission to Everyone
You can use the below command to give permission to everyone. Here I am assigning Full Permission to everyone to access file.txt. There are two methods to do the same.
$ chmod a+rwx file.txt
$ ls -l file.txt -rwxrwxrwx 1 geeklab geeklab 0 Feb 20 06:44 file.txt
You can also use the below Linux chmod command to do the same.
$ chmod ugo+rwx file.txt
$ ls -l file.txt -rwxrwxrwx 1 geeklab geeklab 0 Feb 20 06:44 file.txt
$ chmod 777 file.txt
Assign different permissions to User, Group & Others by using Single Linux chmod Command
Here I am Removing Execute permission from User/Owner and Adding Execute permission to Group & Others by using single command.
$ chmod u-x,g+x,o+x file.txt
$ ls -l file.txt -rw-rwxrwx 1 geeklab geeklab 0 Feb 20 09:40 file.txt
Assign permission to a file by taking reference of any other file
You can copy permission from one file to another. Here I have a file named a1.txt whose permission is something like “-rw-r–r–“.
$ ls -l a1.txt -rw-r--r-- 1 geeklab geeklab 0 Feb 20 07:21 a1.txt
Now I want to assign the same permission to my another file named a2.txt. You can do so using Linux chmod command with argument –reference. Refer the command below.
$ chmod --reference=a1.txt a2.txt
$ ls -l a2.txt -rw-r--r-- 1 geeklab geeklab 0 Feb 20 07:22 a2.txt
Assign Permission Recursively
I have a directory named data, in which I have so many files and I want to give permission to all of them at once instead of manually one by one. To so you can use the Linux chmod command with argument -R. This will help you to give permission Recursively.
$ chmod -R ugo+rwx data/
$ chmod -R 777 data/
Assign permission with Verbose output
You can get output after assigning permission to any files/directories by using Linux chmod command with argument -v. Refer the command below.
$ chmod -v 777 file.txt mode of 'file.txt' changed from 0664 (rw-rw-r--) to 0777 (rwxrwxrwx)
Assign permission with output (This command will give output only if there is any changes)
chmod command with argument -c also do’s the same thing as Verbose output (i.e. -v). But it will show the output only if there is any changes in permission. If you are assigning same permission then it won’t show any output.
$ chmod -c 755 file.txt mode of 'file.txt' changed from 0777 (rwxrwxrwx) to 0755 (rwxr-xr-x)
Assigning Permission by ignoring/solving errors
To avoid any erroes or to rectify any errors during assigning permission you can use Linux chmod command with argument -f. Refer the command below.
$ chmod -f 755 file.txt
For chmod command Help
For chmod command more options and arguments you can use the below command.
$ chmod --help Usage: chmod [OPTION]... MODE[,MODE]... FILE... or: chmod [OPTION]... OCTAL-MODE FILE... or: chmod [OPTION]... --reference=RFILE FILE... Change the mode of each FILE to MODE. With --reference, change the mode of each FILE to that of RFILE. -c, --changes like verbose but report only when a change is made -f, --silent, --quiet suppress most error messages -v, --verbose output a diagnostic for every file processed --no-preserve-root do not treat '/' specially (the default) --preserve-root fail to operate recursively on '/' --reference=RFILE use RFILE's mode instead of MODE values -R, --recursive change files and directories recursively --help display this help and exit --version output version information and exit Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'. GNU coreutils online help: [http://www.gnu.org/software/coreutils/] Full documentation at: [http://www.gnu.org/software/coreutils/chmod] or available locally via: info '(coreutils) chmod invocation'
Check chmod command version
You can use the Linux chmod command with argument –version to check the version of installed chmod command version.
$ chmod --version chmod (GNU coreutils) 8.25 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later [http://gnu.org/licenses/gpl.html]. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David MacKenzie and Jim Meyering.