Synopsis
A readable file can be inaccessible through its directory. Create a small example under the account’s home directory, predict the result of an access change, and restore the original settings. Run the exercise as an ordinary user, since the root account can bypass many permission checks.
Identify the account and the file
Return to the practice directory and identify the account.
cd "$HOME/unix-practice"
pwd
idThe output from id identifies the current user, the primary group, and group memberships. Check that the working directory ends in /unix-practice. Create a directory reserved for this exercise.
mkdir access-testIf the name already exists, use it only if it belongs to this exercise. On a repeat run, the following commands restore owner access to the directory and replace the practice file access-test/record.
chmod 700 access-test
printf '%s\n' 'north meadow' > access-test/record
chmod 600 access-test/record
ls -ld access-test
ls -l access-test/recordThe first mode begins with drwx------, and the second begins with -rw-------. The first character identifies a directory (d) or an ordinary file (-). In the long listing, the owner and group names follow the link count. Their actual values depend on the account.
Read the three sets of bits
Read the nine positions after the file-type character in groups of three. The first group applies to the owner, the second to the file’s group, and the third to other accounts. A dash marks an absent permission.
| Bit | Ordinary file | Directory |
|---|---|---|
r (read) | Read the bytes in the file. | List entry names. |
w (write) | Change the file contents. | Change entries, such as creating or removing a name, with search permission. |
x (execute or search) | Execute a suitable program file. | Look up an entry by name or traverse the directory on a path. |
In a numeric mode, add 4 for read, 2 for write, and 1 for execute or search in each group. Thus 700 gives the owner all three directory permissions. The two zeros grant none of these permissions to the group or to other accounts. With these settings, only the owner among ordinary accounts can traverse access-test. OpenBSD chmod(1) · Debian chmod(1)
Reproduce and repair an access failure
First confirm that the note is readable.
cat access-test/recordExpect north meadow. Remove only the owner’s search permission from the test directory. Predict whether reading the same path will succeed.
chmod u-x access-test
ls -ld access-testThe directory mode now begins with drw-------. Try the read again.
cat access-test/recordExpect a permission error. Looking up record requires search permission on its parent directory even though the file itself still grants its owner read permission.
Restore the missing bit and retry.
chmod u+x access-test
cat access-test/recordThe return of north meadow confirms the repair. For a similar error elsewhere, inspect the owner and mode of each directory in the path before changing a file’s permissions.
Continue with Streams, redirection, and filters.