{"id":139478,"date":"2022-04-01T12:51:32","date_gmt":"2022-04-01T16:51:32","guid":{"rendered":"http:\/\/www.bu.edu\/tech\/?page_id=139478"},"modified":"2022-04-01T12:52:56","modified_gmt":"2022-04-01T16:52:56","slug":"chmod","status":"publish","type":"page","link":"https:\/\/www.bu.edu\/tech\/support\/research\/system-usage\/using-file-system\/file-permissions\/chmod\/","title":{"rendered":"Change Mode (chmod) Command"},"content":{"rendered":"<p>The Linux command <code><span class=\"command\">chmod<\/span><\/code> allows you to control exactly who is able to read, edit, or run your files. Chmod is<strong> <\/strong>an abbreviation for <strong>change mode<\/strong>;<strong> <\/strong>if you ever need to say it out loud, just pronounce it exactly as it looks:<em> ch&#8217;-mod<\/em>.<\/p>\n<h3>How does chmod work?<\/h3>\n<p>To use chmod, you need to know about <em>access modes<\/em>. Each file on a Linux system has nine access modes (or settings) that determine exactly who can do what to the file. Chmod is the command that lets you change these settings.<\/p>\n<p><strong>There are three classes of people:<\/strong><\/p>\n<dl>\n<dt><strong>user (u)<br \/>\n<\/strong><\/dt>\n<dd>the person who created the file<\/dd>\n<dt><strong>group (g)<br \/>\n<\/strong><\/dt>\n<dd>people in a selected group<\/dd>\n<dt><strong>other (o)<br \/>\n<\/strong><\/dt>\n<dd>everyone else on the system<\/dd>\n<\/dl>\n<p><strong>For each class of people there are three classes of permissions:<\/strong><\/p>\n<dl>\n<dt><em>read (r)<br \/>\n<\/em><\/dt>\n<dd>ability to see the contents of the file<\/dd>\n<dt><em>write (w)<br \/>\n<\/em><\/dt>\n<dd>ability to change the contents of the file<\/dd>\n<dt><em>execute (x)<br \/>\n<\/em><\/dt>\n<dd>ability to execute the contents of the file<\/dd>\n<\/dl>\n<h3><strong>Viewing file and directory permissions<\/strong><\/h3>\n<p><strong>Use the Linux command, &#8216;<code><span class=\"command\">ls -l<\/span><\/code>&#8216; (that is the letter l, not the number 1) to see a listing of files and their permissions.<br \/>\n<\/strong><\/p>\n<pre>          % <strong>ls -l<\/strong>\r\n\r\n          total 161\r\n\r\n          -rw-r--r--  1 fred   49487 Jan 26 12:36 all\r\n\r\n          -rw-r--r--  1 fred    3235 Jan 26 16:46 cs320\r\n\r\n          -rw-------  1 fred      64 Jan 26 20:14 diary\r\n\r\n          %<\/pre>\n<p><strong>How to interpret the permissions<\/strong><br \/>\nHere is a sample directory listing, showing the permission fields and the people associated with each permission:<\/p>\n<pre>          :&lt;------------special flag to indicate type of file\r\n\r\n          :                  [d : directory, - : file]\r\n\r\n          : u&lt;--------------three permissions for USER\r\n\r\n          :|||\r\n\r\n          :||| g&lt;--------------three permissions for GROUP\r\n\r\n          :|||:::\r\n\r\n          :|||::: o&lt;---------------three permissions for OTHER\r\n\r\n          :|||:::|||\r\n\r\n          drwxrwxrwx  2 fred         1024 Jan 26 12:26 stuff\r\n\r\n          -rw-------  2 fred         1024 Jan 26 12:26 more-stuff\r\n\r\n          -rwxr-xr-x  2 fred         1024 Jan 26 12:26 yet-more<\/pre>\n<p>A hyphen (-) indicates that the permission is disabled. An enabled permission is shown by the appropriate letter, &#8216;r&#8217; &#8216;w&#8217; or &#8216;x.&#8217; In the example above, <strong>stuff<\/strong> is a directory (more on that below), the file <strong>more-stuff<\/strong> may be <em>read<\/em> and <em>changed<\/em> only by the owner (Fred), and the file <strong>yet-more<\/strong> may be <em>read<\/em>, <em>changed<\/em> and <em>executed<\/em> by Fred, and <em>read<\/em> and <em>executed<\/em> by everyone else.<\/p>\n<h4>Directory permissions<\/h4>\n<p>The permission fields for directories are interpreted a little differently than those for a file. The three fields (<strong>user<\/strong>, <strong>group<\/strong>, <strong>other<\/strong>) remain the same as those for a file but the three permissions mean:<\/p>\n<pre>          r(ead):   can look for a file name in this directory\r\n\r\n          w(rite):  can create or delete files in this directory\r\n\r\n          x(ecute): can search into this directory<\/pre>\n<p>In other words, directory permissions protect files rather than the contents of files. For example, if someone only has <em>execute<\/em> permission on a directory, he or she <em>can<\/em> list or run a file in that directory but <em>cannot<\/em> get a listing of all the files in that directory. For that they would have to know the exact names in advance. <em> <\/em><\/p>\n<p><em>It is necessary to have execute permission on a directory to change (chdir) to it.<\/em><\/p>\n<h3><strong>Controlling access with chmod<\/strong><\/h3>\n<p>In order to control the access users may have to your file or directory, use the &#8216;change mode&#8217; program, chmod.<\/p>\n<p>The chmod command allows changing of permissions using the letters u, g, and o (user, group, and others) and r, w, and x (read, write, and execute). For example, to turn off others&#8217; write permission you can issue the command:<\/p>\n<pre>          chmod o-w filename<\/pre>\n<p>(you might translate &#8220;o-w&#8221; as &#8220;for others, take away write permission.&#8221;)<\/p>\n<p>To turn write permission back on you would say:<\/p>\n<pre>          chmod o+w filename<\/pre>\n<p>(similarly, &#8220;for others, add write permission.&#8221;)<\/p>\n<p>You can group changes together with commas. For example, in order to make a file readable by the public but writable by your group, you might use the command:<\/p>\n<pre>          chmod g+rw,o+r filename<\/pre>\n<p>To remove write permission from your group later on, you could issue the command:<\/p>\n<pre>          chmod g-w filename<\/pre>\n<p>Another way to achieve the same result would be to use the command<\/p>\n<pre>          chmod g=r filename<\/pre>\n<p>The = operator assigns the permission <em>explicitly<\/em> so that all other settings for that category (owner, group, or others) will be reset. For example, g=r would remove all permission from the group except <em>read<\/em>, and explicitly set <em>read<\/em> if not set already.<\/p>\n<p><strong>Some tips on permissions and privacy:<\/strong><\/p>\n<ul>\n<li>It is unlikely that you would ever want to give the <strong>public<\/strong> <em>write<\/em> permission to your files or directories.<\/li>\n<li> If you wish to grant access to a directory to <strong>others<\/strong>, without risking changes to the directory&#8217;s files, give &#8216;r&#8217; AND &#8216;x&#8217; permissions. The execute flag is important for access along with the read flag.<\/li>\n<li>If you wish to keep the directory private, then remove permissions from the &#8216;<strong>other<\/strong>&#8216; fields.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> it is possible to delete a file in a directory, even without having read or write access to that file, merely by having write access to the file&#8217;s directory.<\/p>\n<p><strong>Changing access to multiple files<\/strong><br \/>\nThe chmod -R option allows you to recursively descend through directory arguments, setting the mode for each file as specified. When symbolic links are encountered, their mode is not changed and they are not traversed.<\/p>\n<p><strong>Using octal values to change access<\/strong><br \/>\nYou can also use numbers (octal values) instead of letters to set the permissions. The following table shows some commonly used settings.<\/p>\n<pre>                          Private         Public\r\n\r\n          Directory         700             755\r\n\r\n          Text file         600             644\r\n\r\n     To change the mode of the protection fields, use the command\r\n\r\n          % chmod 700 directory\r\n\r\n          % ls -ld directory\r\n\r\n          drwx------  2 fred         1024 Jan  6 18:32 directory\r\n\r\n          % chmod 755 directory\r\n\r\n          % ls -ld directory\r\n\r\n          drwxr-xr-x  2 fred         1024 Jan  6 18:32 directory\r\n\r\n          % chmod 600 filename\r\n\r\n          % ls -l filename\r\n\r\n          drw-------  2 fred         3234 Jan  6 18:34 filename\r\n\r\n          % chmod 644 filename\r\n\r\n          % ls -l filename\r\n\r\n          drwxr-xr-x  2 fred         3234 Jan  6 18:34 filename<\/pre>\n<p><strong>Can you lock yourself out?<\/strong><br \/>\nYou <em>can<\/em> chmod a file so that you can&#8217;t read, write, or execute it even though you own it. There may be circumstances in which you intentionally turn off write permission to yourself, so that you don&#8217;t accidentally change the file &#8212; or you may do it by mistake. You can never get yourself into very much trouble with this feature, but some people get worried when the system refuses to let them read or write their own files. If you should find yourself in this situation, just use<span> <span>chmod<\/span> again, changing the permissions to whatever settings you prefer. <\/span><\/p>\n<h3><strong>References<\/strong><\/h3>\n<p>For further information, see the <code><span class=\"command\">chmod<\/span><\/code>, <code><span class=\"command\">ls<\/span><\/code>, and <code><span class=\"command\">umask<\/span><\/code> online manual pages or one of the many general books on using the Linux file system. Research Computing Services offers <a href=\"https:\/\/www.bu.edu\/tech\/about\/training\/classroom\/rcs-tutorials\/\">tutorials<\/a> on Linux and other subjects at the start of the Fall, Spring, and Summer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Linux command chmod allows you to control exactly who is able to read, edit, or run your files. Chmod is an abbreviation for change mode; if you ever need to say it out loud, just pronounce it exactly as it looks: ch&#8217;-mod. How does chmod work? To use chmod, you need to know about&#8230;<\/p>\n","protected":false},"author":1692,"featured_media":0,"parent":137939,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/139478"}],"collection":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/users\/1692"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/comments?post=139478"}],"version-history":[{"count":1,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/139478\/revisions"}],"predecessor-version":[{"id":139479,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/139478\/revisions\/139479"}],"up":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/137939"}],"wp:attachment":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/media?parent=139478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}