# Understanding Linux Permissions: A Complete Guide with Examples

<div><strong>Source</strong>: <a href='https://www.tuanh.net/blog/devops/understanding-linux-permissions-a-complete-guide-with-examples'>Understanding Linux Permissions: A Complete Guide with Examples</a></div><div class="line-post"> 
 <h2 class="title-post">1. What Are Linux Permissions?</h2> 
</div>
<div class="line-post">
  Linux file permissions determine who can perform actions on files and directories, such as reading, writing, and executing. Each file or directory in Linux has associated permission sets for the file owner, a group, and others. These permissions are fundamental in protecting sensitive information and preventing unauthorized access. 
</div>
<div class="images-post"> 
 <img width="80%" class="element_955121-image" src="https://resource.tuanh.net/article/image/8c89a75b-868c-4302-9669-99bbd2b71df2.png" alt="Image"> 
</div>
<div class="line-post">
  Permissions in Linux are represented by a combination of letters and numbers. The three types of permissions are: 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.1 Read (r)</h3> 
</div>
<div class="line-post"> 
 <ul> 
  <li>Allows a user to open and read the contents of a file.</li> 
  <li>On directories, it allows listing the directory's contents.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.2 Write (w)</h3> 
</div>
<div class="line-post"> 
 <ul> 
  <li>Allows a user to modify or delete a file.</li> 
  <li>On directories, it allows the user to create or delete files within the directory.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.3 Execute (x)</h3> 
</div>
<div class="line-post"> 
 <ul> 
  <li>Allows a user to execute a file, such as a script or binary program.</li> 
  <li>On directories, it allows entering the directory or accessing its files.</li> 
 </ul> 
</div>
<div class="line-post">
  Each file in Linux has three sets of these permissions: 
</div>
<div class="line-post"> 
 <ul> 
  <li><strong>Owner</strong>: Permissions for the user who owns the file.</li> 
  <li><strong>Group</strong>: Permissions for the group that owns the file.</li> 
  <li><strong>Others</strong>: Permissions for all other users.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h2 class="title-post">2. Understanding Permission Notation</h2> 
</div>
<div class="line-post">
  Permissions are displayed in a symbolic or numeric format when you list files using the ls -l command. Here’s an example: 
</div>
<pre class="code">$ ls -l<br>-rwxr-xr--</pre>
<div class="line-post">
  This output can be broken down into: 
</div>
<div class="line-post"> 
 <ul> 
  <li>-rwxr-xr-- is the permission string.</li> 
  <li>The first character (-) indicates the file type (- for files, d for directories).</li> 
  <li>The next three characters (rwx) show the owner’s permissions: read, write, and execute.</li> 
  <li>The middle set of characters (r-x) represents the group’s permissions.</li> 
  <li>The last set (r--) shows the permissions for others.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.1 Numeric Representation</h3> 
</div>
<div class="line-post">
  Permissions can also be represented numerically using octal notation. Each permission type has a corresponding value: 
</div>
<div class="line-post"> 
 <ul> 
  <li>Read: 4</li> 
  <li>Write: 2</li> 
  <li>Execute: 1</li> 
 </ul> 
</div>
<div class="line-post">
  The sum of these numbers gives the final permission: 
</div>
<div class="line-post"> 
 <ul> 
  <li>rwx = 4 + 2 + 1 = 7</li> 
  <li>r-x = 4 + 0 + 1 = 5</li> 
  <li>r-- = 4 + 0 + 0 = 4</li> 
 </ul> 
</div>
<div class="line-post"> <strong>Example:</strong> 
</div>
<div class="images-post"> 
 <img width="80%" class="element_084264-image" src="https://resource.tuanh.net/article/image/2fe3e983-0e26-4508-870d-1b28b615650d.png" alt="Image"> 
</div>
<div class="line-post">
  Using the same example from above (rwxr-xr--), the numeric permission is 754. 
</div>
<div class="line-post"> 
 <h2 class="title-post">3. How to Change Permissions with chmod</h2> 
</div>
<div class="line-post">
  You can change file permissions using the chmod command. chmod modifies file permissions either symbolically or numerically. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.1 Symbolic Mode</h3> 
</div>
<div class="line-post">
  To modify permissions symbolically, use chmod with the permission set (u for owner, g for group, o for others) and the action (+, -, or =). 
</div>
<div class="line-post"> <strong>Example:</strong> 
</div>
<pre class="code">$ chmod u+x myscript.sh</pre>
<div class="line-post">
  This command adds execute permission for the owner of the file myscript.sh. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.2 Numeric Mode</h3> 
</div>
<div class="line-post">
  Numeric mode changes permissions based on the octal values discussed earlier. For example, to set a file's permissions to rwxr-xr--, you would use: 
</div>
<pre class="code">$ chmod 754 myscript.sh</pre>
<div class="line-post"> 
 <h3 class="sub1">3.3 Demo Code: Changing Directory Permissions</h3> 
</div>
<div class="line-post">
  Consider a directory project/ where you want the owner to have full permissions, but the group and others should only be able to read and execute. 
</div>
<pre class="code">$ chmod 755 project/</pre>
<div class="line-post">
  After running this command, if you check the permissions using ls -ld project/, you’ll see: 
</div>
<pre class="code">drwxr-xr-x 2 user user 4096 Sep 26 14:10 project/</pre>
<div class="line-post">
  This output shows that the owner has full access (rwx), while the group and others have read and execute permissions (r-x). 
</div>
<div class="line-post"> 
 <h2 class="title-post">4. Practical Use Cases for Permissions</h2> 
</div>
<div class="line-post">
  Understanding and using permissions effectively is essential in different scenarios, especially in shared environments and when setting up servers. 
</div>
<div class="line-post"> 
 <h3 class="sub1">4.1 Securing Sensitive Files</h3> 
</div>
<div class="line-post">
  Let’s say you have a configuration file, config.txt, that should only be accessible by the file owner (you). You can restrict access as follows: 
</div>
<pre class="code">$ chmod 600 config.txt</pre>
<div class="line-post">
  The permissions 600 mean that the owner can read and write (rw-), but the group and others have no permissions (---). 
</div>
<div class="line-post"> 
 <h3 class="sub1">4.2 Managing Group Collaboration</h3> 
</div>
<div class="line-post">
  If you're working in a shared project directory, and you want all group members to be able to modify the files but not other users, you can use: 
</div>
<pre class="code">$ chmod 770 shared_dir/</pre>
<div class="line-post">
  The 770 permission allows the owner and group to read, write, and execute, but prevents others from accessing the directory. 
</div>
<div class="line-post"> 
 <h2 class="title-post">5. Changing Ownership with chown</h2> 
</div>
<div class="line-post">
  In addition to permissions, ownership of files and directories can be changed using the chown command. The chown command modifies the file owner and group. 
</div>
<pre class="code">$ sudo chown newuser:newgroup file.txt</pre>
<div class="line-post">
  This changes the owner of file.txt to newuser and the group to newgroup. 
</div>
<div class="line-post"> 
 <h3 class="sub1">5.1 Demo Code: Changing Directory Ownership</h3> 
</div>
<div class="line-post">
  To change the ownership of a directory project/, run the following command: 
</div>
<pre class="code">$ sudo chown -R user:group project/</pre>
<div class="line-post">
  The -R option applies the ownership change recursively to all files and directories inside project/. 
</div>
<div class="line-post"> 
 <h2 class="title-post">6. Advanced Permission Techniques: Setuid, Setgid, and Sticky Bit</h2> 
</div>
<div class="line-post">
  Linux also has advanced permission settings that provide more control over file and directory access. 
</div>
<div class="line-post"> 
 <h3 class="sub1">6.1 Setuid (Set User ID)</h3> 
</div>
<div class="line-post">
  Setuid allows a file to be executed with the permissions of its owner, rather than the user running the file. This is useful for programs that need elevated privileges. 
</div>
<pre class="code">$ chmod u+s script.sh</pre>
<div class="line-post"> 
 <h3 class="sub1">6.2 Setgid (Set Group ID)</h3> 
</div>
<div class="line-post">
  Setgid ensures that files created in a directory inherit the group ownership of the directory, rather than the user creating them. 
</div>
<pre class="code">$ chmod g+s directory/</pre>
<div class="line-post"> 
 <h3 class="sub1">6.3 Sticky Bit</h3> 
</div>
<div class="line-post">
  The sticky bit is commonly used on shared directories like /tmp to ensure that users can only delete their own files, even if others have write access. 
</div>
<pre class="code">$ chmod +t /shared_dir/</pre>
<div class="line-post"> 
 <h2 class="title-post">7. Conclusion</h2> 
</div>
<div class="line-post">
  Linux permissions are a vital tool for system administration, enabling you to control access to files and directories securely. Understanding the symbolic and numeric representations, along with how to use chmod and chown, will allow you to confidently manage permissions. Advanced techniques like setuid, setgid, and sticky bit further enhance control. Mastering these concepts ensures that your Linux environment remains safe and efficient. 
</div>
<div class="line-post">
  Have questions or run into any issues? Feel free to leave a comment below! 
</div><p><strong>Read more at </strong>: <a href='https://www.tuanh.net/blog/devops/understanding-linux-permissions-a-complete-guide-with-examples'>Understanding Linux Permissions: A Complete Guide with Examples</a></p>
