BEST LINUX CHMOD COMMAND WITH EXAMPLES
Introduction
In this article we are going to learn what is Linux chmod command and how to use it. chmod command is used in Linux related operating system to Change file mode bits. In simple language you can change the permissions of files and directories in Linux using chmod command. There are two methods by which we can change the permission’s :
1. Alphabetical
2. Numerical
To change permission using 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 method’s with all tricks.
Alphabetical Way :
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 Filename
Numerical Way :
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 :
elinuxbook@ubuntu:~$ ls -l file.txt
-rw-rw-r-- 1 elinuxbook elinuxbook 0 Feb 20 06:16 file.txt
And use the below command to check permission of a directory :
elinuxbook@ubuntu:~$ ls -ld data/
drwxrwxr-x 2 elinuxbook elinuxbook 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 quiet 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 in to 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 :
Important symbols :
– : 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.
Now let’s have a look at some most important Linux chmod command with examples :
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.
elinuxbook@ubuntu:~$ chmod u+rw file.txt elinuxbook@ubuntu:~$ ls -l file.txt -rw------- 1 elinuxbook elinuxbook 0 Feb 20 06:16 file.txt
By Numerical Way use the below command :
elinuxbook@ubuntu:~$ chmod 600 file.txt
Also Read :
- EXPLAINING SPECIAL LINUX PERMISSIONS ( SUID | SGID | STICKY BIT )
- COMPLETE UNIX COMMANDS AND BASIC LINUX COMMANDS WITH EXAMPLES FOR BEGINNERS
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.
elinuxbook@ubuntu:~$ chmod u+rwx data/ elinuxbook@ubuntu:~$ ls -ld data/ drwx------ 2 elinuxbook elinuxbook 4096 Feb 20 06:18 data/
Numerical Way :
elinuxbook@ubuntu:~$ 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.
elinuxbook@ubuntu:~$ chmod u-rw file.txt elinuxbook@ubuntu:~$ ls -l file.txt ----rw-r-- 1 elinuxbook elinuxbook 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.
Method : 1
elinuxbook@ubuntu:~$ chmod a+rwx file.txt elinuxbook@ubuntu:~$ ls -l file.txt -rwxrwxrwx 1 elinuxbook elinuxbook 0 Feb 20 06:44 file.txt
Method : 2
You can also use the below Linux chmod command to do the same.
elinuxbook@ubuntu:~$ chmod ugo+rwx file.txt elinuxbook@ubuntu:~$ ls -l file.txt -rwxrwxrwx 1 elinuxbook elinuxbook 0 Feb 20 06:44 file.txt
Numerical Way :
elinuxbook@ubuntu:~$ 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.
elinuxbook@ubuntu:~$ chmod u-x,g+x,o+x file.txt elinuxbook@ubuntu:~$ ls -l file.txt -rw-rwxrwx 1 elinuxbook elinuxbook 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–“.
elinuxbook@ubuntu:~$ ls -l a1.txt -rw-r--r-- 1 elinuxbook elinuxbook 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.
elinuxbook@ubuntu:~$ chmod --reference=a1.txt a2.txt elinuxbook@ubuntu:~$ ls -l a2.txt -rw-r--r-- 1 elinuxbook elinuxbook 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.
elinuxbook@ubuntu:~$ chmod -R ugo+rwx data/
Numerical Way :
elinuxbook@ubuntu:~$ 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.
elinuxbook@ubuntu:~$ 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.
elinuxbook@ubuntu:~$ 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.
elinuxbook@ubuntu:~$ chmod -f 755 file.txt
For chmod command Help
For chmod command more options and arguments you can use the below command.
elinuxbook@ubuntu:~$ 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.
elinuxbook@ubuntu:~$ 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.
We have tried our best to include all possible Linux chmod command with examples. If something missed out you can comment below and we will include that on our article.
If you found this article useful then Like us, Share this post on your preferred Social media, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.