Linux File Permissions Made Easy: A Beginner's Guide

Introduction

In this article, I will discuss file permissions in Linux and some basic commands for manipulating them, I will aim to make it beginner-friendly, as I consider myself a beginner too. File permission is a permission or control an individual (A user in Linux) has over a particular file or folder. There are three main types of permissions:

Types of Permissions:

Read (r): As the name implies, this permission allows the user to view/read the content of a file and list directory content.

Write (w): This permission gives the user the ability to modify a file or create and delete files within a directory

Execute (x): This allows the file to be executable by the user this is usually applicable to program files (python, c, java etc.) and also gives access to a directory meaning the directory can be opened if the user has execute permission.

Notice how we have been mentioning “file” and “directory” in the explanation of how these permissions work. This is because in Linux, you either have a File or Folder (also called a directory). Don’t mix things up. We have many types of files, but they are all called files. For this article, we are only talking about “files and directories.” We won’t be talking about types of files.

User Categories:

In Linux users are categorized into three groups for permission management:

Owner: This is the person who owns the file this is usually the file creator however this can be changed

Group: Users who belong to a specific group with some certain permission to a file or directory

Others: All other users on the system.

Permission Representations

File permissions are represented with a string of 10 characters and each character has a meaning and the format is:

- rwx rw- r--

The first character (- or d) indicates whether it is a file (-) or a directory (d).

The next nine characters are split into three sets, each set representing permissions for the owner, group, and others, respectively.

Example:

Let's examine the image below, which shows the file named test.txt with the following permissions.

- | rwx | rw- | r--

The first character in the image (-) shows that this is a regular file, while the next three characters rwx This indicates that the Owner of the file has permission to read, write and execute the file, the next three characters rw- This indicates that any user who shares the same group with the “testuser” who is the owner of the file can read and write to the file but can’t execute the file and the last three characters r-- indicate that every other user can only read the file.

ls -l is the command you can use to list files and directories with details information, including permission

Numeric representation of file permission

Although the file system may appear as a random string of characters, it has a numerical meaning.

BinaryOctalStringPermissions
0000 (0+0+0)- - -No permission
0011 (0+0+1)- - xExecute only
0102 (0+2+0)- w -Write only
0113 (0+2+1)- w xWrite + Execute
1004 (4+0+0)r - -Read only
1015 (4+0+1)r - xRead + Execute
1106 (4+2+0)r w -Read + Write
1117 (4+2+1)r w xRead + Write + Execute

The table above is the Binary and Octal representation of each permission. each string represents the permission for the Owner, Group and Others. This might confuse you a little bit especially if you are not so good at binary during school days but one thing that helped me understand it easily.

An easier way to remember is knowing that “r = 4, w = 2 and x = 1” also remember that r always comes first followed by w finally x with this, you can easily sum up the value you want for each user take a look at the image below as an example of how rwx|rw-|r-x was calculated.

Knowing the numeric combination makes it easy to change the permission for files and directories.

Changing File permission

Linux provides three main commands for handling file permission changes:

chmod: can be used to change permission.

chown: used to change the File ownership.

chgrp: used to change the group ownership.

Using chmod Command

This is the primary command for changing permissions. I will briefly show how to use it easily, keeping it simple and concise. I recommend this is used alongside the numeric representation explained above try calculating the numeric value of the permission you need for each category and use it with the chmod command.

Example:

You need to set a permission where the Owner (r w -), group (- w -), other (r - -). Our expected output will be something like this rw--w-r--

With the numeric representation above we can easily do the math (r w - : 4 + 2 + 0 owner = 6), (- w - : 0 + 2 + 0 group = 2) and (r - -: 4 + 0 + 0 others = 4), we have a combination of 624. we could easily use the chmod command to set the permission change

As shown in the image above our file originally had the permission set to rwx|rw-|r-- = 764 we then change the permission using the numeric value of 624 which sets it to our newly preferred permission set (rw-|-w-|r--). I find this approach very understandable and I believe you should find it easy as well however there are other ways this could be done such as using the alphabetical value I find this approach easy as well.

We can specify which category of permission to modify with chmod using the following symbolic representation.

u = owner, g = group, o = others

chmod u+rw filename  # Adds read and write for the owner
chmod g-w filename   # Removes write permission for the group
chmod o+x filename   # Adds execute permission for others

chmod image

The image above demonstrates the use of the chmod command with symbolic notation. We used u+rw to grant read and write permissions to the file owner, followed by u-r to revoke the read permission. While this method looks easier, I personally prefer the numeric approach as it is more concise and is an ideal choice for tech enthusiasts who value simplicity😉.

Summary and Collusion

Understanding Linux file permissions is an essential step toward mastering Linux. How do you achieve this? By learning the basics of how to grant read, write, and execute permissions which is the focus of this article. While file permissions can be a complex topic, grasping these fundamentals is enough to get you started. With commands like chmod, chown, and chgrp, you can effectively manage file access. As you continue to practice and explore, these will lay the foundation for more advanced Linux administration challenges ahead.

0
Subscribe to my newsletter

Read articles from ibukun adewale odusanya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ibukun adewale odusanya
ibukun adewale odusanya