<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Python.Coding</title>
    <description>The latest articles on DEV Community by Python.Coding (@pythoncoding1).</description>
    <link>https://dev.to/pythoncoding1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4123354%2F10149aab-befb-4ae9-815f-d8915706baea.png</url>
      <title>DEV Community: Python.Coding</title>
      <link>https://dev.to/pythoncoding1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pythoncoding1"/>
    <language>en</language>
    <item>
      <title>How to Manage Linux File Permissions for Web Servers with chmod and chown</title>
      <dc:creator>Python.Coding</dc:creator>
      <pubDate>Wed, 16 Sep 2026 17:03:12 +0000</pubDate>
      <link>https://dev.to/pythoncoding1/how-to-manage-linux-file-permissions-for-web-servers-with-chmod-and-chown-2hi4</link>
      <guid>https://dev.to/pythoncoding1/how-to-manage-linux-file-permissions-for-web-servers-with-chmod-and-chown-2hi4</guid>
      <description>&lt;p&gt;Incorrect file permissions on Linux web servers are one of the most common causes of &lt;code&gt;403 Forbidden&lt;/code&gt; errors and server security vulnerabilities. If your web server process (such as NGINX or Apache) cannot read your website files, it cannot serve them to users. Conversely, if file permissions are set too loosely, unauthorized users can modify your code.&lt;/p&gt;

&lt;p&gt;In this hands-on tutorial, you will learn how to configure proper user ownership and file permissions for a Linux web directory using &lt;code&gt;chown&lt;/code&gt; and &lt;code&gt;chmod&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To follow this guide, you will need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A system running Linux (such as Arch Linux, Ubuntu, or Debian).&lt;/li&gt;
&lt;li&gt;Basic familiarity with command-line terminal navigation.&lt;/li&gt;
&lt;li&gt;Root or &lt;code&gt;sudo&lt;/code&gt; administrative privileges.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 1: Understand Ownership and the Web User
&lt;/h2&gt;

&lt;p&gt;On Linux, every file and directory is assigned an &lt;strong&gt;owner&lt;/strong&gt; and a &lt;strong&gt;group&lt;/strong&gt;. Web servers like Apache and NGINX usually run under a dedicated system user (such as &lt;code&gt;www-data&lt;/code&gt; or &lt;code&gt;nginx&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;To check the current ownership of your web directory (typically &lt;code&gt;/var/www/html&lt;/code&gt;), open your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-ld&lt;/span&gt; /var/www/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Output&lt;/strong&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drwxr-xr-x 2 root root 4096 Sep 16 10:00 /var/www/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the owner is set to &lt;code&gt;root&lt;/code&gt;, your regular user account won't be able to edit website files without using &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Set Group Ownership with chown
&lt;/h2&gt;

&lt;p&gt;To allow your user account to edit files while giving the web server read access, assign your regular user as the owner and the web server group (&lt;code&gt;www-data&lt;/code&gt;) as the group owner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo chown&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="nv"&gt;$USER&lt;/span&gt;:www-data /var/www/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-R&lt;/code&gt;: Applies the ownership change recursively to all files and subdirectories.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$USER&lt;/code&gt;: Environment variable representing your logged-in username.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;www-data&lt;/code&gt;: The web server group name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Verify the update:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-ld&lt;/span&gt; /var/www/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Output&lt;/strong&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drwxr-xr-x 2 luky www-data 4096 Sep 16 10:05 /var/www/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 3: Configure Secure File and Directory Permissions with chmod
&lt;/h2&gt;

&lt;p&gt;Linux permissions determine what the Owner (&lt;strong&gt;u&lt;/strong&gt;), Group (&lt;strong&gt;g&lt;/strong&gt;), and Others (&lt;strong&gt;o&lt;/strong&gt;) can do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Read (4)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write (2)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execute (1)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For web directories, standard security best practices dictate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Directories:&lt;/strong&gt; &lt;code&gt;755&lt;/code&gt; (&lt;code&gt;rwxr-xr-x&lt;/code&gt;) so the web server can enter and read folders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Files:&lt;/strong&gt; &lt;code&gt;644&lt;/code&gt; (&lt;code&gt;rw-r--r--&lt;/code&gt;) so files can be read, but executable code cannot be run unnecessarily.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Run these two commands to apply secure permissions across all folders and files separately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Set directory permissions to 755&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;find /var/www/html &lt;span class="nt"&gt;-type&lt;/span&gt; d &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;chmod &lt;/span&gt;755 &lt;span class="o"&gt;{}&lt;/span&gt; +

&lt;span class="c"&gt;# Set file permissions to 644&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;find /var/www/html &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;chmod &lt;/span&gt;644 &lt;span class="o"&gt;{}&lt;/span&gt; +
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 4: Verify the New Permissions Setup
&lt;/h2&gt;

&lt;p&gt;Create a sample &lt;code&gt;index.html&lt;/code&gt; file inside &lt;code&gt;/var/www/html&lt;/code&gt; to test write access:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;h1&amp;gt;Linux Permission Test Successful&amp;lt;/h1&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /var/www/html/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check the file details:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; /var/www/html/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Result:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-rw-r--r-- 1 luky www-data 43 Sep 16 10:10 /var/www/html/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your regular user can now edit website contents cleanly without &lt;code&gt;sudo&lt;/code&gt;, and your web server process retains read access while maintaining a secure permission boundary.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By using &lt;code&gt;chown&lt;/code&gt; to structure group ownership and combining &lt;code&gt;find&lt;/code&gt; with &lt;code&gt;chmod&lt;/code&gt; to separate directory and file permissions, you establish a secure, maintainable Linux web environment.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>webdev</category>
      <category>sysadmin</category>
    </item>
  </channel>
</rss>
