Users, groups, and permissions

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.

Shell input
cd "$HOME/unix-practice"
pwd
id

The 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.

Shell input
mkdir access-test

If 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.

Shell input
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/record

The 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.

BitOrdinary fileDirectory
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.

Shell input
cat access-test/record

Expect north meadow. Remove only the owner’s search permission from the test directory. Predict whether reading the same path will succeed.

Shell input
chmod u-x access-test
ls -ld access-test

The directory mode now begins with drw-------. Try the read again.

Shell input
cat access-test/record

Expect 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.

Shell input
chmod u+x access-test
cat access-test/record

The 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.

UNIX is a registered trademark of The Open Group.