Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 9

Understanding File Permissions: rwx and Octal

Recommended resources

Introduction

Welcome to the second module of your course, "File Permissions and SELinux Fundamentals." In the last module, you mastered the essential commands for navigating your Fedora system and analyzing its disk usage. You now know how to find any file or directory and see how much space it consumes.

But what happens once you find a file? Who is allowed to read it, change it, or run it? This is where file permissions come in, and they are the bedrock of security and organization on any multi-user system like Linux. As a web developer, understanding permissions is not just a sysadmin task; it's essential for securing your application's code, data, and configuration files when you deploy them on a server.

Your goal for this lesson is to learn how to:

  • Interpret file permission strings (rwx) and their octal equivalents.

We'll start by decoding the cryptic-looking string of letters you see when you run ls -l and then learn the numerical shorthand that system administrators frequently use.


Decoding the Permission String

You're already familiar with the ls command. To see file permissions, we use the long listing format with the -l flag.

Open your terminal and run ls -l in your home directory. You'll see output similar to this:

-rw-r--r--. 1 myuser myuser  8431 Jun 5 10:20 my_django_project.zip
drwxr-xr-x. 5 myuser myuser  4096 Jun 5 11:00 my_website/

Let's focus on that first column of characters, like -rw-r--r-- and drwxr-xr-x. This 10-character string tells you everything about the item's type and its basic permissions.

Linux File Permission String Breakdown
This diagram illustrates how the permission string is structured. It's composed of the file type, followed by three sets of permissions: for the owner, the group, and all other users.

As the diagram shows, the string is broken into four parts:

  1. File Type (1st character): A - indicates a regular file, and a d indicates a directory.
  2. Owner/User Permissions (characters 2-4): What the user who owns the file can do.
  3. Group Permissions (characters 5-7): What members of the group that owns the file can do.
  4. Other Permissions (characters 8-10): What everyone else on the system can do.

Each of the three permission sets consists of the letters r, w, and x, which stand for read, write, and execute. A hyphen - means that permission is not granted.

The meaning of these permissions changes slightly depending on whether you're dealing with a file or a directory. The following video provides an excellent, detailed explanation of these concepts.

Linux Crash Course - Understanding File & Directory Permissions

Watch this segment from Learn Linux TV to get a thorough understanding of how to read the permission string and what r, w, and x mean for both files and directories.

Watch from 01:07 to 12:43. Pay close attention to the breakdown of the permission string into four groups and the different meanings of 'execute' for a file versus a directory.

To summarize the key points from the video:

Permission For a File For a Directory
r (read) View the contents of the file. List the contents of the directory (i.e., see the names of the files inside).
w (write) Modify or delete the content of the file. Create, delete, or rename files within the directory.
x (execute) Run the file as a script or program. Enter (i.e., cd into) the directory.

Notice the critical difference for directories: you need execute (x) permission to cd into a directory, and you need read (r) permission to list its contents with ls.


Do It Yourself: Interpreting Permissions

Let's practice this with some real files on your system. The /etc directory, which holds most of your system's configuration files, is a great place to explore.

  1. Open your terminal.
  2. Navigate to the /etc directory:
    cd /etc
    
  3. List the contents with their permissions:
    ls -l
    
  4. Find the entry for the hostname file and the dnf directory.
  5. In a text editor or on a piece of paper, analyze each one and answer these questions:
    • What are the full permission strings for hostname and dnf?
    • Who is the owner and what is the group for each?
    • For hostname: Can the owner change the file? Can members of the group? Can a regular user (other)?
    • For dnf: Can the owner list its contents? Can they cd into it? What about members of the group?

This exercise will help solidify your ability to read and understand what you're seeing.


From Symbols to Numbers: Octal Permissions

While the rwx format is descriptive, you will very frequently see permissions represented as a three-digit number, like 755 or 644. This is called octal notation, and it's a compact way to express the exact same information. It's essential to know how to translate between the two.

The conversion is based on assigning a numeric value to each permission:

  • r (read) = 4
  • w (write) = 2
  • x (execute) = 1
  • - (no permission) = 0

To get the value for a permission set (like the owner's), you simply add the numbers for the granted permissions.

  • rwx = 4 + 2 + 1 = 7
  • rw- = 4 + 2 + 0 = 6
  • r-x = 4 + 0 + 1 = 5
  • r-- = 4 + 0 + 0 = 4

A three-digit octal number represents the permissions for [owner][group][other].

For example, a file with rwxr-xr-x permissions would be 755:

  • Owner: rwx = 7
  • Group: r-x = 5
  • Other: r-x = 5
Linux File Permission Interpretation and Octal Conversion
This image visualizes the calculation for the permission string `rwxrw-r-x`. The owner's `rwx` permissions sum to 7, the group's `rw-` permissions sum to 6, and the others' `r-x` permissions sum to 5, resulting in the octal code 765.

To deepen your understanding, let's turn back to the resources.

5.3 Lesson 1 - Linux Professional Institute – Learning

This article from the Linux Professional Institute provides a clear, text-based explanation of the numeric values and how they are combined.

Read the sections titled "Understanding Permissions" and "Numeric Mode". The first part explains the meaning of each permission and introduces their octal values. The "Numeric Mode" section shows how these are combined into a 3-digit number.

The article "Linux Permissions Explained in Simple Terms" also has a fantastic, detailed walkthrough with a helpful conversion table.

Linux Permissions Explained in Simple Terms

This article offers another great explanation of octal calculation, including a cheat sheet table that is very useful for quick reference.

Read the section titled "How to Calculate Permissions in Octal Values". This guide breaks down the process step-by-step and provides a handy table mapping all symbolic permissions to their octal values.


Do It Yourself: Conversion Practice

Now it's time to test your conversion skills. This is a core competency, so take a moment to work through these.

Task 1: Symbolic to Octal

Convert the following rwx permission strings into their three-digit octal equivalents.

  • rwxr-xr-- -> ?
  • rw-rw---- -> ?
  • rwx------ -> ?

Task 2: Octal to Symbolic

Convert the following octal permissions into their rwx string equivalents.

  • 755 -> ?
  • 644 -> ?
  • 700 -> ?

Click to see the answers

Task 1 Answers:

  • rwxr-xr-- -> 754 (Owner: 4+2+1=7, Group: 4+0+1=5, Other: 4+0+0=4)
  • rw-rw---- -> 660 (Owner: 4+2+0=6, Group: 4+2+0=6, Other: 0+0+0=0)
  • rwx------ -> 700 (Owner: 4+2+1=7, Group: 0, Other: 0)

Task 2 Answers:

  • 755 -> rwxr-xr-x
  • 644 -> rw-r--r--
  • 700 -> rwx------

Conclusion

Excellent work! You've just taken your first and most important step into the world of Linux security. Understanding who can access what is a fundamental skill that underpins almost everything else you will do as a system administrator and developer.

Key Takeaways:

  • File permissions are viewed with ls -l.
  • The 10-character permission string is divided into file type, owner permissions, group permissions, and other permissions.
  • The permissions are r (read), w (write), and x (execute), and their meaning depends on whether it's a file or a directory.
  • Permissions can also be represented by a three-digit octal number, where r=4, w=2, and x=1.
  • A common permission set for directories is 755 (rwxr-xr-x), and for files is 644 (rw-r--r--).

In the next lesson, we will make this knowledge active. You'll learn how to use the chmod command to change file permissions, using both the symbolic (u+x) and octal (755) methods you've learned about today.