Understanding File & Directory Permissions in Linux

Kartik ChKartik Ch
4 min read

In Linux-based operating systems, file and directory permissions are essential for managing who can access files and what actions they can perform on them. This blog will walk through how to create a directory, a file, set permissions, and verify them using the ls -l command.

What Are File & Directory Permissions?

Imagine you have a folder on your computer, and inside that folder is a file. You don’t want everyone to have access to that file. In such a case, permissions allow you to control who can read, write and access the file.

  • Read - To see the file and its contents

  • Write - To edit/update the file

  • Execute - To run the file like a program

Linux-based operating systems (like Ubuntu, CentOS, etc.) use a special permission system to manage who can do what with files and folders. Think of permissions as rules you set for who gets to see or change things in your digital workspace.

Breaking Down Permissions

Permissions are usually shown in a simple line of text.

For example: -rw-r--r--

Here’s what each part means:

  • The first character represents the type of file. For a regular file, it’s a -.

  • The next three characters (rw-) show the owner's permissions. r means read, w means write, and x means execute. In our example, the owner can read and write but cannot execute/run the file.

  • The next three characters (r--) show the permissions for the group. The group members can only read the file.

  • The last three characters (r--) show the permissions for everyone else ("others"). These people can also only read the file.

Why Do Permissions Matter?

By setting permissions, you can control what each of these groups- Owner, Group and Others can do with the file. For example:

  • You might let your team read your notes but not change them.

  • You might want to allow everyone to see your file but only let you make changes.

Task: Create a Folder and File with Specific Permissions

Let’s take a practical look at how to create a folder and a file, then set permissions for them.

Step 1: Create the Folder and File

  1. First, create a new folder. This folder will be called devops_workspace.

    command: mkdir devops_workspace

  2. To go into the folder use cd.

    command: cd devops_workspace

  3. Create a file called project_notes.txt inside the folder

    command: vim project_notes.txt

Step 2: Set Permissions for the File

Set the permissions for project_notes.txt

We use a command called chmod to set the permissions. The command for this would look like:

Command: chmod 240 project_notes.txt

Here’s what that number means:

  • 2 for the owner means they can write (change the file).

  • 4 for the group means they can only read the file.

  • 0 for others means they have no access to the file.

Step 3: Verify the Permissions

Check to make sure the permissions are set correctly. We use the ls -l command to see the permissions of the file.

  1. Run the command:

    • Command: ls -l project_notes.txt

You’ll see something like this:

--w-r----- 1 user group 0 Feb 13 12:34 project_notes.txt

Let’s break it down:

  • --w-r-----: This shows the permissions. The owner has write permission (-w-), the group has read-only permission (r--), and others have no access (---).

  • user: The file’s owner (you).

  • group: The group associated with the file (your work group).

  • 0: The file’s size (this file is empty, so it’s 0).

  • Feb 13 12:34: The date and time the file was last modified.

  • project_notes.txt: The name of the file.

Conclusion

We have covered the basics of file and directory permissions for Owners, Groups and Others. By understanding and using permissions, you can ensure that your files are safe and only accessible to the people who need them. So, the next time you create a file, think about who should be able to see it and what actions they can take

0
Subscribe to my newsletter

Read articles from Kartik Ch directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kartik Ch
Kartik Ch