Permission denied — Diagnose file read access
A read failure may follow file mode, parent traversal restrictions, ACLs, mandatory access policy or a different effective identity. Inspect the affected identity and each path component. Do not broadly chmod 777 or disable security. The fixture demonstrates a file owned by the learner with no mode bits, then restores only owner read permission. On real systems use the minimal approved correction, preserve the old permissions, and repeat the failing action under its actual identity. Run the entire example as an ordinary user in a fresh Bash script without errexit (set -e), so the expected failure can be inspected. It uses mktemp, chmod, cat and rm; a privileged process with discretionary-access bypass may still read the file. The EXIT trap removes the private directory.
Formal Syntax & Structure
id; namei -l FILE; stat FILEParameters & Attributes
| Name | Type | Description |
|---|---|---|
| FILE | operand | Authorized file being investigated |
Return Value
GNU cat reports nonzero when a file cannot be read. Message varies by locale, implementation and cause.
Working Example
d=$(mktemp -d) || exit 1trap 'rm -rf -- "$d"' EXITprintf "demo\n" > "$d/sample"chmod 000 "$d/sample"(cd "$d" && cat sample) 2> "$d/error"rc=$?cat "$d/error"printf "status=%s\n" "$rc"test "$rc" -ne 0 || exit 1chmod u+r "$d/sample"cat "$d/sample"