Developer Tools

Permission Denied on Your Own File When the Group Can Read It

By Raja JahangirPublished December 1, 20264 min read
Infographic: a laptop with a dark terminal and a red padlock on a document, a card on the owner, group and other classes, and four steps to fix a 040 file with chmod

Key takeaways

  • Unix puts your process in exactly one class per file, owner, group or other, and checks only that class bits for access.
  • A file set to 040 can be read by its group but not by its owner, because the owner bits are checked alone.
  • Fix it with chmod u+r or a full value like 640, and check the octal value in a chmod calculator first.
  • The same one-class rule applies to directories, web server users and cron jobs, so check the class that actually runs.

If you own a file and ls -l shows ----r-----, you get "Permission denied" when you open it, even though everyone in the file's group can read it. Unix does not add up the three permission sets. It puts your process in exactly one class for each file, owner, group or other, and checks only that class's bits. You own the file, so only the owner bits count, and they are empty. Give the owner read access with chmod u+r file and it opens.

This guide explains the rule, shows how to read a permission string with our chmod calculator, and lists the places where the rule catches people out.

How Unix picks which permission bits apply

The POSIX standard, which Linux, macOS and the BSDs follow, defines three classes of process for every file:

  1. Owner class. Your process is in it if its effective user ID matches the user ID of the file.
  2. Group class. Your process is in it only if it is not in the owner class, and its effective group ID or one of its supplementary group IDs matches the file's group.
  3. Other class. Your process is in it only if it is in neither of the first two.

The order matters. Being the owner takes you out of the group class completely, even when you are also a member of the file's group. The system then checks the three owner bits and nothing else. There is no "use whichever class gives the most access" step.

The same logic works one level down. A group member is judged only by the group bits, so a file with no group permissions but read for others locks out the group while letting strangers read it. Here is how four permission strings play out:

ls -l shows Octal Owner Group member Anyone else
----r----- 040 Denied Read Denied
-------r-- 004 Denied Denied Read
-rw----r-- 604 Read, write Denied Read
-r--r----- 440 Read Read Denied

The root user is the exception: it is not limited by the read and write bits in the usual way, which is why a command run with sudo can read a file that its owner cannot.

Table of four permission strings, 040, 004, 604 and 440, saying who can read each file, beside a photo of a laptop with a dark terminal window

Reading the problem with the chmod calculator

The chmod calculator turns checkboxes into the octal value, the symbolic string and a plain-English summary, which makes a lopsided permission set easy to spot.

Untick every box except Group, Read. The calculator shows:

  • Octal: 040 (chmod 040)
  • Symbolic: ---r-----
  • What this means: Owner: no permissions. Group: read. Others: no permissions.

The symbolic string has nine characters, while ls -l prints ten. The extra first character in ls -l is the file type (- for a regular file, d for a directory), so ----r----- in your terminal is ---r----- in the calculator.

Now also tick Owner, Read. The result changes to 440, r--r-----, and the summary reads Owner: read. Group: read. Others: no permissions. That is the smallest change that lets you, the owner, read the file while keeping the group's access.

How to fix it

  1. Add the missing owner bit. chmod u+r report.txt adds read for the owner and leaves the group and others bits alone. A file at 040 becomes 440.
  2. Or set the whole value. If you also need to edit the file, chmod 640 report.txt gives the owner read and write and the group read. Work out other combinations in the chmod calculator before you type them.
  3. Check the result with ls -l and open the file again.

You can run chmod on a file you own even when its owner bits are all empty. Changing the mode needs ownership, not read permission, so you are never locked out of fixing your own file.

The same rule applies to directories

For a directory, read lets you list the names inside, write lets you create and delete entries, and execute lets you enter the directory and reach the files in it. The class rule is identical: if you own a directory set to 070, you cannot even cd into it, while the rest of your group can. Shared project folders are where this usually shows up, often after someone copies a permission value meant for a group-only workspace.

Where this catches people out

  • Web servers. The server process runs as its own user, so it falls into the group or other class for your files. If those bits lack read, the server answers 403 Forbidden. The HTTP status codes reference explains what each response means.
  • Scheduled scripts. A script run by cron needs the execute bit for the class the cron job runs as. If the job runs as the owner and the owner bits lack x, it fails with "Permission denied" even when the group bits allow execution. Plan the timing itself with the cron expression generator.
  • Private keys. The common 600 value (rw-------) is the opposite case: the owner has full use, and the group and others have nothing.

Sources

Frequently asked questions