Permission Denied Even After chmod 777? 4 Hidden Causes

If you have ever encountered a Permission denied error in Linux, run chmod 777 in frustration, and still been blocked, the problem is often caused by another access-control restriction. Traditional rwx file permission bits are only one layer of Linux access control. In this hands-on lab, you will reproduce, diagnose, and fix four overlooked Linux access restrictions that chmod 777 cannot resolve—including one that blocks even root—using real commands and least-privilege solutions.

Follow along with the full live Ubuntu terminal demonstration: Watch on YouTube.

Lab Prerequisites & One-Click Setup Script

Lab Environment Notice: Run this lab on a test Linux VM (such as Ubuntu or Debian with /lab located on an ext4 filesystem, or another filesystem that supports the immutable (i) attribute). The demonstration uses ext4. You also need sufficient privileges to mount filesystems and manage the immutable attribute. Do not run experimental mount or permission changes on a production server.

1. Create and Run the Lab Setup Script

Save the script below as setup-lab.sh and execute it as root (or with sudo). It creates the demo user (alex), the shared group (labshare), and all four broken scenarios inside /lab.

This setup script targets Ubuntu and Debian. Other distributions may require different package installation commands.

Use a clean test VM. The setup script refuses to overwrite existing lab paths or reuse existing lab identities. If setup fails partway through, inspect the remaining lab resources before retrying.

#!/bin/bash
set -euo pipefail

if [[ $EUID -ne 0 ]]; then
  echo "Please run this script as root (e.g., sudo bash setup-lab.sh)."
  exit 1
fi

# Refuse to overwrite existing lab data or reuse existing accounts.
if [[ -e /lab || -L /lab ]]; then
  echo "Error: /lab already exists. Inspect it before running setup."
  exit 1
fi

if getent passwd alex >/dev/null ||
   getent group labshare >/dev/null ||
   getent group alex >/dev/null ||
   [[ -e /home/alex || -L /home/alex ]]; then
  echo "Error: a lab account, group, or home directory already exists."
  echo "Use a clean test VM or inspect the existing identities."
  exit 1
fi

umask 022

# Install ACL tools if missing (Ubuntu/Debian).
if ! command -v setfacl >/dev/null 2>&1 ||
   ! command -v getfacl >/dev/null 2>&1; then
  apt-get update
  apt-get install -y acl
fi

# Create and identify the dedicated lab directory.
mkdir -m 755 /lab
chown root:root /lab
printf '%s\n' 'CNN-LINUX-PERMISSIONS-LAB-v1' > /lab/.cnn-lab-marker
chmod 600 /lab/.cnn-lab-marker
mkdir -p /lab/vault /lab/readonly_dir /lab/usb

# Create dedicated lab identities.
groupadd labshare
useradd -m -U -d /home/alex -s /bin/bash alex
usermod -aG labshare alex

# Trap 1A: 777 file inside a parent directory missing execute (+x)
echo "Top secret data" > /lab/vault/secret.txt
chown root:labshare /lab/vault /lab/vault/secret.txt
chmod 777 /lab/vault/secret.txt
chmod 666 /lab/vault

# Trap 1B: 777 file inside a parent directory missing write (+w)
touch /lab/readonly_dir/trash.txt
chown root:labshare /lab/readonly_dir /lab/readonly_dir/trash.txt
chmod 777 /lab/readonly_dir/trash.txt
chmod 555 /lab/readonly_dir

# Trap 2: 777 file blocked by a named-user ACL entry for alex
echo "Team notes" > /lab/shared.txt
chown root:root /lab/shared.txt
chmod 777 /lab/shared.txt
setfacl -m u:alex:---,g:labshare:r-- /lab/shared.txt

# Trap 3: 777 script on a tmpfs filesystem mounted with noexec
mount -t tmpfs -o size=10M,mode=0755,rw,nosuid,nodev,noexec tmpfs /lab/usb
printf '#!/bin/bash\necho "Script ran successfully!"\n' > /lab/usb/run.sh
chmod 777 /lab/usb/run.sh

# Trap 4: 777 file locked with the immutable (+i) attribute
echo "enabled=true" > /lab/config.conf
chown root:root /lab/config.conf
chmod 777 /lab/config.conf
chattr +i /lab/config.conf

echo "Lab environment ready under /lab."

Run the setup script:

sudo bash setup-lab.sh

2. Open Two Terminal Sessions Side by Side

  • Terminal 1 (Regular User alex): sudo -iu alex
  • Terminal 2 (Administrator root): sudo -i

The command outputs below are representative examples. Timestamps, file sizes, inode-related details, and displayed mount options may vary depending on your Linux distribution, filesystem, and system configuration.

Trap 1: The Parent Directory Wall (namei -l)

Linux interprets r, w, and x differently on directories than on regular files:

PermissionMeaning on a Regular FileMeaning on a Directory
r (Read)Read the file's contentsList the filenames inside the directory
w (Write)Modify the file's contentsCreate, rename, or delete entries inside the directory (requires +x)
x (Execute)Run the file as a programTraverse ("pass through") the directory to access items inside it

Think of every directory along a path as a door. If a regular user lacks execute (x) permission on any directory along the path, the user cannot reach the file behind it—even if the file itself has 777 permissions.

Part A: Missing Execute (+x) on a Parent Directory

1. Reproduce the error

As root, confirm /lab/vault/secret.txt has 777 permissions:

ls -l /lab/vault/secret.txt
-rwxrwxrwx 1 root labshare 16 Nov 28 09:23 /lab/vault/secret.txt

As alex, attempt to read the file:

cat /lab/vault/secret.txt
cat: /lab/vault/secret.txt: Permission denied

2. Diagnose with namei -l

Instead of manually running ls -ld on every parent folder, inspect the entire path at once:

namei -l /lab/vault/secret.txt
f: /lab/vault/secret.txt
drwxr-xr-x root root     /
drwxr-xr-x root root     lab
drw-rw-rw- root labshare vault
                         secret.txt - Permission denied

Notice vault has drw-rw-rw- (666)—read and write are set, but execute (x) is missing for everyone in the ordinary permission bits.

3. Apply a least-privilege fix and verify

Because both /lab/vault and secret.txt belong to the labshare group (which alex is a member of), tighten the file to 640 and grant directory traversal with 750 as root:

chmod 640 /lab/vault/secret.txt
chmod 750 /lab/vault
ls -ld /lab/vault && ls -l /lab/vault/secret.txt
drwxr-x--- 2 root labshare 4096 Nov 28 09:23 /lab/vault
-rw-r----- 1 root labshare   16 Nov 28 09:23 /lab/vault/secret.txt

Switch back to alex and read the file again:

cat /lab/vault/secret.txt
Top secret data

Takeaway: Restoring execute permission on the parent directory resolved the access problem. The file could then be read with the more restrictive 640 permissions because Alex belongs to the authorized labshare group.

Part B: Why You Cannot Delete a 777 File

Exam & Interview Trap Deleting Requires Parent Directory Write Permission

Deleting a file does not modify the file's contents—it removes an entry from its parent directory. For a regular user, removing a file generally requires write (w) and execute (x) permissions on the parent directory (subject to additional rules such as the sticky bit), regardless of whether the file itself is 777.

1. Reproduce and diagnose as alex

ls -l /lab/readonly_dir/trash.txt
rm -f /lab/readonly_dir/trash.txt
-rwxrwxrwx 1 root labshare 0 Nov 28 09:23 /lab/readonly_dir/trash.txt
rm: cannot remove '/lab/readonly_dir/trash.txt': Permission denied

Inspect the full path with namei -l:

namei -l /lab/readonly_dir/trash.txt
f: /lab/readonly_dir/trash.txt
drwxr-xr-x root root     /
drwxr-xr-x root root     lab
dr-xr-xr-x root labshare readonly_dir
-rwxrwxrwx root labshare trash.txt

The readonly_dir directory is 555 (dr-xr-xr-x)—traversal works, but the ordinary permission bits grant write (w) access to nobody.

2. Fix as root and verify as alex

Grant write and execute access to root and the labshare group:

chmod 770 /lab/readonly_dir

As alex, remove the file using -v to confirm deletion:

rm -v /lab/readonly_dir/trash.txt
removed '/lab/readonly_dir/trash.txt'

Trap 2: Hidden Access Control Lists (ACLs) (getfacl -p)

When standard owner, group, and other bits are not flexible enough, Linux supports Access Control Lists (ACLs) to assign permissions to specific named users or additional groups.

1. Spot the Clue (+) and Reproduce the Error

As root, apply 777 permissions again, just as in the opening demonstration:

chmod 777 /lab/shared.txt

As alex, check /lab/shared.txt and try reading it:

ls -l /lab/shared.txt
cat /lab/shared.txt
-rwxrwxrwx+ 1 root root 11 Nov 28 09:30 /lab/shared.txt
cat: /lab/shared.txt: Permission denied

Look closely at the trailing + at the end of -rwxrwxrwx+. In this demonstration, that plus sign indicates an extended ACL is attached to the file.

2. Inspect the ACL with getfacl -p

Run getfacl -p (the -p flag preserves the leading / in the absolute path):

getfacl -p /lab/shared.txt
# file: /lab/shared.txt
# owner: root
# group: root
user::rwx
user:alex:---
group::rwx
group:labshare:r--
mask::rwx
other::rwx

Why alex is blocked: Even though alex belongs to labshare (r--) and other has rwx, Linux does not combine every matching entry. It matches the specific named-user entry user:alex:--- and denies access without falling back to group or other rules.

How Linux Calculates Effective ACL Permissions

The ACL mask defines the maximum effective permissions available to named users, the owning group, and additional named groups.

For ACL entries subject to the mask, effective permissions are calculated by intersecting the permissions in the entry with the permissions in the ACL mask (a bitwise AND operation).

Effective permissions = ACL entry AND ACL mask

Consider an illustrative example in which an ACL entry grants read, write, and execute permissions, but the mask allows only reading:

ACL entry:  rwx
ACL mask:   r--
            ---
Effective:  r--

Only permissions present in both the ACL entry and the mask survive. In this example, write and execute permissions are removed from the effective permissions.

In our current lab, the mask is rwx. The following table shows the effective permissions of the relevant ACL entries:

ACL EntryACL MaskEffective Permissions
user:alex:---rwx---
group:labshare:r--rwxr--
group::rwxrwxrwx

The ACL mask can limit permissions, but it cannot grant permissions that are missing from an ACL entry. This is why Alex's effective permissions remain --- even when the mask is rwx.

When an extended ACL is present, chmod 777 updates the owner permissions, the ACL mask, and the permissions for others. It does not remove the named-user or named-group ACL entries. Therefore, user:alex:--- continues to restrict Alex's access.

3. Apply a Targeted Fix and Observe the ACL Mask

Rather than wiping the entire ACL (which could delete intentional rules like group:labshare:r--), remove only the blocking entry for alex and tighten standard permissions to 640 as root:

setfacl -x u:alex /lab/shared.txt
chmod 640 /lab/shared.txt
getfacl -p /lab/shared.txt
# file: /lab/shared.txt
# owner: root
# group: root
user::rw-
group::rwx                      #effective:r--
group:labshare:r--
mask::r--
other::---

Key ACL Detail (How the Mask Works): When an extended ACL exists on a file, the group permission bits shown by ls -l actually represent the ACL mask, not just the owning group. Running chmod 640 lowered mask:: to r--, which caps the effective permissions of named users, the owning group (group::rwx #effective:r--), and named groups (group:labshare:r--). The mask does not limit the file owner (user::) or other::.

After the correction, the effective permissions of the remaining group entries are:

ACL EntryACL MaskEffective Permissions
group::rwxr--r--
group:labshare:r--r--r--

4. Verify as alex

cat /lab/shared.txt
ls -l /lab/shared.txt
Team notes
-rw-r-----+ 1 root root 11 Nov 28 09:30 /lab/shared.txt

Notice the + remains because group:labshare:r-- was safely preserved, giving alex read access through group membership without leaving the file world-readable.

Trap 3: Filesystem Mount Restrictions (findmnt -T)

Even when file permissions, parent directories, and ACLs are all valid, filesystem mount options can block execution or writing across an entire mount point. This is common on /tmp, secondary drives, and removable USB media.

1. Reproduce and Diagnose as alex

Check /lab/usb/run.sh and execute it directly:

ls -l /lab/usb/run.sh
/lab/usb/run.sh
-rwxrwxrwx 1 root root 44 Nov 28 09:40 /lab/usb/run.sh
-bash: /lab/usb/run.sh: Permission denied

Use findmnt -T to inspect the mount point containing the script:

findmnt -T /lab/usb/run.sh
TARGET   SOURCE FSTYPE OPTIONS
/lab/usb tmpfs  tmpfs  rw,nosuid,nodev,noexec,relatime,size=10240k,inode64

To inspect only the mount options without the other columns, use:

findmnt -n -o OPTIONS -T /lab/usb/run.sh

The noexec flag instructs the Linux kernel to deny direct execution of binaries and scripts from /lab/usb, regardless of chmod 777.

2. Why bash /lab/usb/run.sh Still Works

Try passing the script to bash explicitly as alex:

bash /lab/usb/run.sh
Script ran successfully!
  • Direct execution (/lab/usb/run.sh): Asks the kernel to execute a file residing on a noexec mount → Blocked.
  • Explicit interpreter (bash /lab/usb/run.sh): Executes the Bash program from a different mount that permits execution, and Bash simply reads /lab/usb/run.sh as text → Allowed. This is also why noexec is a defense-in-depth layer rather than a standalone security boundary.

3. Remount Safely in the Lab and Verify

As root, remount /lab/usb with exec while preserving the existing rw, nosuid, and nodev options:

mount -o remount,rw,nosuid,nodev,exec /lab/usb
findmnt -T /lab/usb/run.sh
TARGET   SOURCE FSTYPE OPTIONS
/lab/usb tmpfs  tmpfs  rw,nosuid,nodev,relatime,size=10240k,inode64

Because exec is the default behavior, it may not appear explicitly in the visible options list once noexec is removed.

Test direct execution again as alex:

/lab/usb/run.sh
Script ran successfully!

Two Other Mount Restrictions to Remember:

  • Read-only (ro) mounts: If findmnt -T shows ro, modifying regular files on that read-only mount fails regardless of chmod. You may encounter a Read-only file system error.
  • FAT32 / exFAT drives: Windows-compatible filesystems do not store native Unix ownership or permission bits like ext4; apparent permissions are largely controlled by the filesystem driver and mount options.

Trap 4: Immutable Files Blocking Even root (lsattr & chattr)

Some Linux filesystems (such as ext4) support filesystem inode attributes (file flags) that ls -l does not display. One important troubleshooting culprit is the immutable (i) attribute.

1. Reproduce as root

Confirm you are root, inspect /lab/config.conf, and try editing or deleting it:

whoami
ls -l /lab/config.conf
echo "updated=true" >> /lab/config.conf
rm -f /lab/config.conf
root
-rwxrwxrwx 1 root root 13 Nov 28 09:47 /lab/config.conf
-bash: /lab/config.conf: Operation not permitted
rm: cannot remove '/lab/config.conf': Operation not permitted

Even rm -f fails because -f only suppresses prompts and missing-file errors—it cannot bypass kernel-enforced filesystem attributes.

2. Diagnose with lsattr

lsattr /lab/config.conf
----i---------e------- /lab/config.conf

The lowercase i stands for immutable. While active, nobody—not even root—can overwrite, append to, rename, or delete the file (though reading still works if the necessary read and access permissions are available).

3. Unlock, Modify, Verify, and Harden

Production Caution: Before removing +i on a real server, always investigate why it was set—administrators often use it to protect files like /etc/resolv.conf or critical configs from automated overwrites.

An appropriately privileged process needs the CAP_LINUX_IMMUTABLE capability to clear the immutable flag before making an authorized change.

Remove the immutable attribute, verify with lsattr, append the new line, and tighten permissions from 777 to 644:

chattr -i /lab/config.conf
lsattr /lab/config.conf
echo "updated=true" >> /lab/config.conf
cat /lab/config.conf
chmod 644 /lab/config.conf
ls -l /lab/config.conf
--------------e------- /lab/config.conf
enabled=true
updated=true
-rw-r--r-- 1 root root 26 Nov 28 09:47 /lab/config.conf

Bonus: Two More Permission Traps to Remember

1. When chmod 777 Causes the Error (SSH Private Keys)

Some programs enforce their own application-specific security checks and refuse files with overly open permissions. For a typical user-owned private key, OpenSSH rejects the key if its permission bits allow access by the group or others, displaying an UNPROTECTED PRIVATE KEY FILE warning. Fix user-owned private keys by tightening them to 600:

chmod 600 ~/.ssh/id_ed25519
2. Mandatory Access Control (SELinux & AppArmor)

If parent directories, ACLs, mount options, and file attributes all look normal—yet access is still denied—a Mandatory Access Control policy may be enforcing restrictions. SELinux is commonly enabled on Fedora, RHEL, and Rocky Linux, while AppArmor is commonly used on Ubuntu. Always inspect denial logs and policies rather than disabling the security module.

Quick-Reference Summary

Before running a diagnostic command, identify the user performing the operation and determine whether the operation involves reading, writing, executing, or deleting a file. Choose the diagnostic command that best matches the problem instead of running every command indiscriminately.

The 4-Command Troubleshooting Checklist

StepDiagnostic CommandWhat It Checks
1namei -l /path/to/filePermissions and ownership on every parent directory (+x to traverse, +w and +x to delete)
2getfacl -p /path/to/fileExtended ACL entries (+ in ls -l), named-user overrides, and #effective: mask limits
3findmnt -T /path/to/fileFilesystem type and mount options such as noexec or ro (read-only)
4lsattr /path/to/fileFilesystem inode attributes (file flags) such as the immutable (i) flag blocking even root

Common Least-Privilege Permission Examples

ModeSymbolicTypical Use Case
755rwxr-xr-xDirectories accessible to other users
644rw-r--r--Ordinary, non-sensitive files
700rwx------Private directories
600rw-------Private files (such as SSH private keys)
770rwxrwx---Group-shared directories
660rw-rw----Group-shared files

Treat these modes as practical starting points—always align final permissions with file ownership, required users, and organizational security policies.

Lab Cleanup Script

When you finish practicing, save and run cleanup-lab.sh as root to remove the temporary mount, immutable file, and /lab directory created by this tutorial.

#!/bin/bash
set -euo pipefail

if [[ $EUID -ne 0 ]]; then
  echo "Please run this script as root (e.g., sudo bash cleanup-lab.sh)."
  exit 1
fi

die() {
  echo "Cleanup stopped: $*" >&2
  exit 1
}

# Inspect mounts before touching anything under /lab.
mounts=$(findmnt -rn -o TARGET,FSTYPE)
usb_mounted=0

while read -r target fstype; do
  case "$target" in
    /lab/usb)
      [[ "$fstype" == tmpfs && "$usb_mounted" -eq 0 ]] ||
        die "Unexpected mount at /lab/usb."
      usb_mounted=1
      ;;
    /lab|/lab/*)
      die "Unexpected mount: $target"
      ;;
  esac
done <<< "$mounts"

# Verify the lab identification marker.
marker=/lab/.cnn-lab-marker

[[ -d /lab && ! -L /lab && $(stat -c %u /lab) == 0 ]] ||
  die "Expected a root-owned lab directory."

[[ -f "$marker" && ! -L "$marker" &&
   $(stat -c '%u:%h' "$marker") == 0:1 &&
   $(cat "$marker") == CNN-LINUX-PERMISSIONS-LAB-v1 ]] ||
  die "Lab identification marker missing or invalid."

# Reject symlinks and unexpected files before removing lab data.
shopt -s nullglob dotglob

check_entries() {
  local dir=$1
  shift
  local entry allowed known

  for entry in "$dir"/*; do
    known=0

    for allowed in "$@"; do
      [[ "$entry" != "$dir/$allowed" ]] || known=1
    done

    [[ $known -eq 1 && ! -L "$entry" ]] ||
      die "Unexpected entry: $entry"
  done
}

check_entries /lab .cnn-lab-marker vault readonly_dir usb shared.txt config.conf

for dir in /lab/vault /lab/readonly_dir /lab/usb; do
  if [[ -e "$dir" ]]; then
    [[ -d "$dir" && ! -L "$dir" ]] ||
      die "Unexpected directory type: $dir"
  fi
done

check_entries /lab/vault secret.txt
check_entries /lab/readonly_dir trash.txt
check_entries /lab/usb run.sh

for file in /lab/vault/secret.txt /lab/readonly_dir/trash.txt \
            /lab/usb/run.sh /lab/shared.txt /lab/config.conf; do
  if [[ -e "$file" ]]; then
    [[ -f "$file" && ! -L "$file" &&
       $(stat -c %h "$file") == 1 ]] ||
      die "Unexpected file type or hard link: $file"
  fi
done

# Do not continue if unmounting fails (e.g., the mount is busy).
if [[ $usb_mounted -eq 1 ]]; then
  umount /lab/usb
fi

# The underlying mount-point directory must be empty.
check_entries /lab/usb

# Clear immutable protection only on the dedicated lab file.
if [[ -f /lab/config.conf ]]; then
  chattr -i /lab/config.conf
fi

# Remove known files only; never recursively delete /lab.
rm -f -- /lab/vault/secret.txt /lab/readonly_dir/trash.txt \
         /lab/shared.txt /lab/config.conf

for dir in /lab/vault /lab/readonly_dir /lab/usb; do
  if [[ -d "$dir" ]]; then
    rmdir -- "$dir"
  fi
done

rm -- "$marker"
rmdir -- /lab

echo "Lab files removed. Demo accounts remain."

# Optional, only after verifying these are tutorial-created identities:
# userdel -r alex
# groupdel labshare

# If the private alex group still exists and is unused:
# groupdel alex

The cleanup script removes only known lab files and stops if it detects an unexpected mount or entry. Demo accounts are left in place by default; remove them only after verifying that this tutorial created them.

Run the cleanup script:

sudo bash cleanup-lab.sh

ComputerNetworkingNotes Ubuntu Tutorials Permission Denied Even After chmod 777? 4 Hidden Causes

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com