Bootstrap FreeKB - Linux Commands - chmod (change a file or directory standard permissions)
Linux Commands - chmod (change a file or directory standard permissions)

Updated:   |  Linux Commands articles

The chmod command can be used to change the permission of files and directories in Linux. Files and directories in Linux have three standard permissions, read, write, and execute. Each of these permissions can be represented by a certain letter or certain number. As an example, if a file has r-x permission, the file has read and execute permission, but does not have write permission.

Permission Character Number
Read r 4
Write w 2
Execute x 1
No permission - 0

 

Permissions are also divided into three groups, user, group, and other. The first group is user, the second group is group, and the third group is other.

 

Let's take an example where file1 has rw-rw-rw- permission.

[john.doe@server1 ~]# ls -l
-rw-rw-rw-. 3 john.doe john.doe 4096 Jun 26 08:21 file1

 


Changing permissions using numbers

The chmod command followed by the new permission and then followed by the file name can be used to change the permissions of the file. In this example, the file is changed to 777, which is rwxrwxrwx.

[john.doe@server1 ~]# chmod 777 file1

[john.doe@server1 ~]# ls -l
-rwxrwxrwx. 3 john.doe john.doe 4096 Jun 26 08:21 file1

 


Changing permissions using the equals (=) character

The equals character can be used to change the permission for user, group, or other. In this example, the other permission is set to read only for file1.

The u character is used for user, the g character is used for group, and the o character is used for other.

[john.doe@server1 ~]# chmod o=r file1

[john.doe@server1 ~]# ls -l
-rwxrwxr--. 3 john.doe john.doe 4096 Jun 26 08:21 file1

 

To change the permissions for two or three groups, separate each group with a comma. In this example, the user will have read write and execute, group will have read and write, and other will have read only.

[john.doe@server1 ~]# chmod u=rwx,g=rw,o=r file1

[john.doe@server1 ~]# ls -l
-rwxrwxr--. 3 john.doe john.doe 4096 Jun 26 08:21 file1

 


Changing permissions using plus (+) or minus (-) symbols

Plus and minus symbols can also be used to add or remove permission from a file. In this example, the -w option is used to remove the write permission from the file.

[john.doe@server1 ~]# chmod -w file1

[john.doe@server1 ~]# ls -l
-r-xr-xr-x. 3 john.doe john.doe 4096 Jun 26 08:21 file1

 

In the prior example, the write permission was removed from user, group, and other, because user or group or other was not specified. If user or group or other is specified, then the permission is only added or removed from the specified. In this example, the write permission is only added to the user.

[john.doe@server1 ~]# chmod u+w file1

[john.doe@server1 ~]# ls -l
-rwxr-xr-x. 3 john.doe john.doe 4096 Jun 26 08:21 file1

 


Recursive

The -R or --recursive option can be used to change the permission for all of the files and directories at and below a certain directory. In this example, the execute permission is added to every file and directory at and below /var/www/html.

[john.doe@server1 ~]# chmod -R +x /var/www/html

[john.doe@server1 ~]# ls -l /var/www/html
-rwxr-xr-x. 3 john.doe john.doe 4096 Jun 26 08:21 index.html
-rwxr-xr-x. 3 john.doe john.doe 4096 Jun 26 08:21 stage.html
-rwxr-xr-x. 3 john.doe john.doe 4096 Jun 26 08:21 stage2.html
. . .

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 49a7c5 in the box below so that we can be sure you are a human.