{"id":6897,"date":"2021-06-09T13:18:40","date_gmt":"2021-06-09T13:18:40","guid":{"rendered":"https:\/\/linuxways.net\/?p=6897"},"modified":"2021-06-09T13:18:40","modified_gmt":"2021-06-09T13:18:40","slug":"chmod-command-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/chmod-command-in-linux\/","title":{"rendered":"Chmod Command in Linux"},"content":{"rendered":"<p>The change of mode or chmod command lets you change the access mode of files in Linux. This lets you decide who can access and run files. We will be using chmod to change the permissions of files and directories in Ubuntu 20.04.<\/p>\n<p>To view the permissions for a file, use:<\/p>\n<pre>ls \u2013l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"527\" class=\"wp-image-6898\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-160.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-160.png 703w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-160-300x225.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/p>\n<p>What does all this mean?<\/p>\n<p>The first character identifies if it\u2019s a file (-) or a directory (d).<\/p>\n<p>Next is the permissions. The first three characters show the owner\u2019s permissions, the next three the group\u2019s permissions, and the last three others\u2019 permission. The table below explains the user types.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>User<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Owner<\/td>\n<td>this is the user who owns the file<\/td>\n<\/tr>\n<tr>\n<td>Group<\/td>\n<td>these are users who are part of a user group<\/td>\n<\/tr>\n<tr>\n<td>Others<\/td>\n<td>these are users other than the owner and group members<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let\u2019s look at the user permissions in Linux. There are three basic permissions; read (r), write (w), and execute (x). Read means the user can only read or view the file. Write allows users to edit or delete a file. Execute allows users to execute the file. As you can see in the image above, the permissions are either represented as characters or -. The \u201c\u2013\u201c (dash) means the users don\u2019t have permission.<\/p>\n<p>The syntax of chmod is:<\/p>\n<p>chmod {users}{operator}{permission} {filename}<\/p>\n<p>Operators let you specify the permissions. We have three operators:<\/p>\n<p>+: Adds the permission<\/p>\n<p>-: Removes the permission<\/p>\n<p>=: Lets you specify the exact permission<\/p>\n<p>chmod has two modes, the symbolic and numeric modes. We will cover both.<\/p>\n<h2>Symbolic Mode<\/h2>\n<p>Let\u2019s look at the file \u201chello.c\u201d. The user has all three permissions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"509\" height=\"26\" class=\"wp-image-6899\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-161.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-161.png 509w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-161-300x15.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-161-501x26.png 501w\" sizes=\"auto, (max-width: 509px) 100vw, 509px\" \/><\/p>\n<p>If we want to change the permissions so that only the owner can read and write the file, while the group and others have read permission, we will use:<\/p>\n<pre>chmod u=rw,og=r hello.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"497\" height=\"28\" class=\"wp-image-6900\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-162.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-162.png 497w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-162-300x17.png 300w\" sizes=\"auto, (max-width: 497px) 100vw, 497px\" \/><\/p>\n<p>To check if the permissions updated, use,<\/p>\n<pre>ls \u2013l {filename}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"41\" class=\"wp-image-6901\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-163.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-163.png 500w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-163-300x25.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>If you want to add the execute permission for the owner, we use:<\/p>\n<pre>chmod u+x hello.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"454\" height=\"22\" class=\"wp-image-6902\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-164.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-164.png 454w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-164-300x15.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/p>\n<p>If we view the permissions now, we can see the execute permission for the owner.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"497\" height=\"44\" class=\"wp-image-6903\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-165.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-165.png 497w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-165-300x27.png 300w\" sizes=\"auto, (max-width: 497px) 100vw, 497px\" \/><\/p>\n<p>If we want to set the execute permission for all, we use:<\/p>\n<pre>chmod a+x hello.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"21\" class=\"wp-image-6904\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-166.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-166.png 444w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-166-300x14.png 300w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/p>\n<p>If we view the permissions now, we can see the execute permission for all users.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"483\" height=\"45\" class=\"wp-image-6905\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-167.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-167.png 483w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-167-300x28.png 300w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/p>\n<h2>Numeric Mode<\/h2>\n<p>You can use a three-digit number to give permissions using chmod. Here is how it works, the leftmost digit represents the permission for the owner, the middle one is for group members, and the rightmost is for others.<\/p>\n<p>The permissions are as follows:<\/p>\n<p>Read = 4<\/p>\n<p>Write = 2<\/p>\n<p>Execute = 1<\/p>\n<p>No permissions = 0<\/p>\n<p>The table below summarizes the permissions.<\/p>\n<table>\n<tbody>\n<tr>\n<td>7<\/td>\n<td>All permissions<\/td>\n<td>4+2+1<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>Read and write<\/td>\n<td>4+2+0<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>Read and execute<\/td>\n<td>4+0+1<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Read only<\/td>\n<td>4+0+0<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Write and execute<\/td>\n<td>0+2+1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Write only<\/td>\n<td>0+2+0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Execute only<\/td>\n<td>0+0+1<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>No permission<\/td>\n<td>0+0+0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let\u2019s use this method to set permission for ABC.txt.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"40\" class=\"wp-image-6906\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-168.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-168.png 496w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-168-300x24.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/p>\n<p>If we want the owner to have all permissions and group and others to have read permissions, we will use:<\/p>\n<pre>chmod 744 ABC.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"437\" height=\"27\" class=\"wp-image-6907\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-169.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-169.png 437w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-169-300x19.png 300w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/p>\n<p>To check if the permissions updated, use,<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"46\" class=\"wp-image-6908\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-170.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-170.png 491w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-170-300x28.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/p>\n<p>If you want to recursively change the permissions of all the files within a directory, use:<\/p>\n<pre>chmod \u2013R {permissions} {filename}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"481\" height=\"23\" class=\"wp-image-6909\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-171.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-171.png 481w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-171-300x14.png 300w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><\/p>\n<p>This command gives the owner all three permissions and groups and others no permissions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"540\" height=\"94\" class=\"wp-image-6910\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-172.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-172.png 540w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-172-300x52.png 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/p>\n<p>If you want to know more about chmod, use:<\/p>\n<pre>man chmod<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"25\" class=\"wp-image-6911\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-173.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-173.png 372w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-173-300x20.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/p>\n<p>This will take you to the manual that has all the details on this command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"573\" class=\"wp-image-6912\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-174.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-174.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-174-300x237.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>We covered both the symbolic and numeric mode of the chmod command in this article.<\/p>","protected":false},"excerpt":{"rendered":"<p>The change of mode or chmod command lets you change the access mode of files in Linux. This lets you decide who can access and run files. We&hellip;<\/p>","protected":false},"author":1,"featured_media":7095,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[360,100],"class_list":["post-6897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-chmod-command","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=6897"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6897\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7095"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=6897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=6897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=6897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}