<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="/rss/stylesheet/" type="text/xsl"?>
<rss xmlns:content='http://purl.org/rss/1.0/modules/content/' xmlns:taxo='http://purl.org/rss/1.0/modules/taxonomy/' xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:itunes='http://www.itunes.com/dtds/podcast-1.0.dtd' xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:atom='http://www.w3.org/2005/Atom' xmlns:podbridge='http://www.podbridge.com/podbridge-ad.dtd' version='2.0'>
<channel>
  <title>Jaryl Chng&apos;s Knowledge Base</title>
  <language>en-us</language>
  <generator>microfeed.org</generator>
  <itunes:type>episodic</itunes:type>
  <itunes:explicit>false</itunes:explicit>
  <atom:link rel="self" href="https://kb-jarylchng-com.pages.dev/rss/" type="application/rss+xml"/>
  <link>https://kb.jarylchng.com</link>
  <description>
    <![CDATA[<p>Welcome to the index page of my knowledge base, if you haven't done so, do visit my website at <a href="https://jarylchng.com" rel="noopener noreferrer" target="_blank">https://jarylchng.com</a>.</p><p>I will mainly use this site to document stuff, most of which will likely be in the public domain.</p>]]>
  </description>
  <itunes:author>Jaryl Chng</itunes:author>
  <itunes:image href="https://kb-static.jarylchng.com/kb-jarylchng-com/production/images/channel-c68f1f55f856ab833b4365991609dbec.png"/>
  <image>
    <title>Jaryl Chng&apos;s Knowledge Base</title>
    <url>https://kb-static.jarylchng.com/kb-jarylchng-com/production/images/channel-c68f1f55f856ab833b4365991609dbec.png</url>
    <link>https://kb.jarylchng.com</link>
  </image>
  <copyright>©2024</copyright>
  <itunes:category text="Technology"/>
  <item>
    <title>Linux - Default File and Directory Permissions</title>
    <guid>11i9Zvy40NI</guid>
    <pubDate>Sat, 06 Apr 2024 16:14:04 GMT</pubDate>
    <itunes:explicit>false</itunes:explicit>
    <description>
      <![CDATA[<p>Just yesterday night while I was configuring Grav, I realised some of the permissions were not setting right and I was unable to modify files created by Grav via a privileged user with SFTP.</p><p>Looking into it, I checked the properties via ls -l and sure enough:</p><pre class="ql-syntax" spellcheck="false">ls -l /-snip-grav-parent-directory-/
    total -snip-
    drwxr--r-x 13 -snip-grav-user- -snip-grav-group- 4096 Aug  6 08:00 -snip-grav-folder-
</pre><p>Since the files were owned by the Grav user, while my user was in the Grav group. I was unable to edit the files due to the absense of write permissions for the group clause.</p><h2>Simple chmod</h2><p>At first, running a simple recursive chmod everytime I met the issue was fine as normally my web files were not created by the Grav user as I mainly host static pages before:</p><pre class="ql-syntax" spellcheck="false">chmod -R g+w /-snip-grav-directory-/
</pre><h2>Default groups and permissions</h2><p>But there was two problems, the first problem is that new files created by my privileged user had the group not set to the Grav user and thus not allowing Grav to modify it too. I didn't wish to add the grav user to any groups so I had to resort to recursively adding the <em>setgid</em> bit on the directory:</p><pre class="ql-syntax" spellcheck="false">$ chmod -R g+s /-snip-grav-directory-/
</pre><p>That solves the first problem by having new files automatically set its' group to the parent directory whenever they are created.</p><p>The other problem was there was no default permissions and new files were created were not allowed to be modified by my privileged user. That's where this nifty tool comes in, <a href="https://wiki.archlinux.org/index.php/Access_Control_Lists" rel="noopener noreferrer" target="_blank">acl (Access Control Lists)</a> which extends the basic chmod, chown and chgrp commands.</p><p>This tool requires you to add acl your filesystem mount option to always apply if it's not already set, you can check it by running this and seeing if acl is returned:</p><pre class="ql-syntax" spellcheck="false">tune2fs -l /dev/sdXY | grep "Default mount options:"
</pre><p>Most filesystems turn it on by default, but if it does not, proceed to add it into /etc/fstab.</p><p>After that it's just a simple command as this to set default permissions for the directory and the files</p><pre class="ql-syntax" spellcheck="false">setfacl -R -d -m g::rwX -snip-grav-directory-
</pre><ul><li>setfacl - set acl command</li><li>-R - recursive</li><li>-d - target <strong>d</strong>efault permissions</li><li>-m - <strong>m</strong>odify</li><li>g::rwX - target <strong>g</strong>roup with <strong>r</strong>ead <strong>w</strong>rite and only directories should be set with e<strong>X</strong>ecutable</li></ul><p>A confirmation with getfacl:</p><pre class="ql-syntax" spellcheck="false">getfacl -snip-grav-directory-
  getfacl -snip-grav-directory-
  # file: -snip-grav-directory-
  # owner: -snip-grav-user-
  # group: -snip-grav-group-
  # flags: -s-
  user::rwx
  group::rwx
  other::r-x
  default:user::rwx
  default:group::rwx
  default:other::r-x
</pre><p>Once you see # flags: -s- and default:group::rwx, you are all set and future new directories and files inside will be set with the default permissions automatically.</p><p>You may want to run chmod -R g+X the directory after the above step to apply the permissions to the existing files.</p><h2>Pointers from mistakes I made</h2><ul><li>Do not forget to set the executable permission for directories, if it is missing the user will not be able to access it, note the pointer below when needing to fix this.</li><li>Do not ever run chmod -R g+x as it would set all files executables too which is a security risk, instead use chmod -R g+X with capital X which only sets directories with executable, same thing for acl.</li></ul><h2>Further reading and references</h2><ul><li><a href="https://wiki.archlinux.org/index.php/Access_Control_Lists" rel="noopener noreferrer" target="_blank">Archlinux Wikipedia article on ACL</a></li><li><a href="https://wiki.archlinux.org/index.php/File_permissions_and_attributes" rel="noopener noreferrer" target="_blank">Archlinux Wikipedia article on file permissions and attributes</a></li></ul>]]>
    </description>
    <link>https://kb.jarylchng.com/i/linux-default-file-and-directory-permissions-11i9Zvy40NI/</link>
    <itunes:episodeType>full</itunes:episodeType>
  </item>
</channel>
</rss>