DEV Community

kai wen ng
kai wen ng

Posted on

Stop Using chmod 777: Understanding Linux File Permissions

Are you one of those people who runs:

chmod 777 -R /folder
Enter fullscreen mode Exit fullscreen mode

every time you encounter a Linux permission issue?
I am. 😂

It works. The error disappears. Problem solved... right?

Not exactly.

Giving everyone full permission is usually a sign that we are fixing the symptom instead of understanding the actual problem.

Let's take a look at how Linux file permissions really work.


The Three Permission Types

Every file and directory in Linux has three basic permissions:

Permission Symbol Value Meaning
Read r 4 View contents
Write w 2 Modify contents
Execute x 1 Execute/access

The numeric values are used when setting permissions with chmod.

For example:

rwx = 4 + 2 + 1 = 7
rw- = 4 + 2 + 0 = 6
r-x = 4 + 0 + 1 = 5
Enter fullscreen mode Exit fullscreen mode

The Three User Categories

Linux permissions are divided into three groups:

u → user (file owner)
g → group (users belonging to the file group)
o → other (everyone else)
Enter fullscreen mode Exit fullscreen mode

When you run:

ls -l
Enter fullscreen mode Exit fullscreen mode

you might see:

-rwxrw-r-x
Enter fullscreen mode Exit fullscreen mode

Let's break it down.

- rwx rw- r-x
│ │   │   │
│ │   │   └── Other users
│ │   └────── Group
│ └────────── Owner
└──────────── File type
Enter fullscreen mode Exit fullscreen mode

The first character represents the file type:

-  regular file
d  directory
Enter fullscreen mode Exit fullscreen mode

Understanding Numeric Permissions

Example:

-rwxrw-r-x
Enter fullscreen mode Exit fullscreen mode

Owner:

rwx = 4 + 2 + 1 = 7
Enter fullscreen mode Exit fullscreen mode

Group:

rw- = 4 + 2 + 0 = 6
Enter fullscreen mode Exit fullscreen mode

Other:

r-x = 4 + 0 + 1 = 5
Enter fullscreen mode Exit fullscreen mode

Therefore:

chmod 765 file
Enter fullscreen mode Exit fullscreen mode

means:

Owner   → rwx
Group   → rw-
Other   → r-x
Enter fullscreen mode Exit fullscreen mode

Changing Permissions with chmod

There are two common ways to use chmod.

Numeric mode

Example:

chmod 755 script.sh
Enter fullscreen mode Exit fullscreen mode

Meaning:

Owner → rwx (7)
Group → r-x (5)
Other → r-x (5)
Enter fullscreen mode Exit fullscreen mode

Result:

-rwxr-xr-x
Enter fullscreen mode Exit fullscreen mode

2. Symbolic mode

Instead of calculating numbers, we can directly modify permissions.
Remove read permission from others:

chmod o-r file
Enter fullscreen mode Exit fullscreen mode

Before:

-rwxr--r--
Enter fullscreen mode Exit fullscreen mode

After:

-rwxr-----
Enter fullscreen mode Exit fullscreen mode

Add execute permission for the owner:

chmod u+x file
Enter fullscreen mode Exit fullscreen mode

Before:

-rw-r--r--
Enter fullscreen mode Exit fullscreen mode

After:

-rwxr--r--
Enter fullscreen mode Exit fullscreen mode

If no target is specified:

chmod +x file
Enter fullscreen mode Exit fullscreen mode

it applies to all permission groups.
Before:

-rw-r--r--
Enter fullscreen mode Exit fullscreen mode

After:

-rwxr-xr-x
Enter fullscreen mode Exit fullscreen mode

Directory Permissions Are Different

One important detail:
Directories do not behave exactly like files.

For directories:

Permission Meaning
r List files inside
w Create/delete files
x Enter/access directory

For example:

chmod 755 folder
Enter fullscreen mode Exit fullscreen mode

allows:

  • Owner: full access
  • Others: enter and read files

Without the execute permission:

chmod 644 folder
Enter fullscreen mode Exit fullscreen mode

you may see the directory name, but you cannot access files inside.

Why chmod 777 Is Usually a Bad Idea

chmod 777 gives:

Owner  → rwx
Group  → rwx
Other  → rwx
Enter fullscreen mode Exit fullscreen mode

Meaning every user and process on the system can modify the files.

For example:

chmod 777 uploads/
Enter fullscreen mode Exit fullscreen mode

might temporarily fix an application issue, but it can introduce security problems.

A better approach is usually to fix ownership:

chown user:group uploads/
chmod 755 uploads/
Enter fullscreen mode Exit fullscreen mode

The correct permission depends on who actually needs access.


Next Time You See "Permission Denied"

Before running:

chmod 777 -R /folder
Enter fullscreen mode Exit fullscreen mode

try checking:

ls -l
Enter fullscreen mode Exit fullscreen mode

Then ask:

  • Who owns this file?
  • Which group owns it?
  • Does the user have the required permission?
  • Is the directory missing the execute permission?

Linux permissions are not complicated.

Once you understand:

  • three permission bits (rwx)
  • three ownership groups (ugo)
  • numeric permissions (755, 644, etc.)

most permission issues become much easier to debug.

Use the permission you need, not the maximum permission available.

Top comments (0)