Lesson 4 of 10Foundations

12 min · theory and practice · Basics

Who runs the command: user and environment

Explain the differences between your terminal, a service and a scheduled job.

Reproduce a failure as the actual user without widening access across the server.

An administrator’s command and the same line in automation are different experiments. User identity, groups, working directory and PATH determine what can happen. Compare those conditions as well as the command text.

Find the process’s actual identity

The file owner does not identify its current reader. For a service, inspect User, Group and PID, then compare with ps. An unset User for a system unit normally means root, but confirm the actual process.

An interactive shell’s groups can differ from a running process’s groups. Changing an account does not automatically replace an existing process’s execution context.

Commands
id
id backup
systemctl show shop-api -p User -p Group -p MainPID
ps -eo pid,user,group,args | head

Test access as the same user

test -r checks readability and test -w checks writability. Directories also require traversal through each parent. A readable final file cannot compensate for an inaccessible parent directory.

Separate an access probe from a full run. Start without creating files. When the task actually needs a write, run it in the training environment and verify the expected artifact.

Commands
namei -l /var/lib/shop/shop.sqlite3
sudo -u backup test -r /var/lib/shop/shop.sqlite3
printf "read_status=%s\n" "$?"

Compare working directory and PATH

A relative configuration path depends on the launch directory. PATH determines where the shell searches for short command names. Finding a utility in your home terminal does not establish that cron can find it.

Avoid dumping an application’s entire environment, which may contain secrets. Inspect relevant unit properties and use a safe probe that prints only the directory and PATH.

Commands
sudo -u backup env -i HOME=/home/backup PATH=/usr/bin:/bin /bin/sh -c 'pwd; command -v sqlite3'

Repair the boundary while preserving its purpose

For a utility lookup problem, fix the path or declared environment. For directory access, fix ownership, group or a narrow permission boundary. Running the entire job as root hides the cause and changes many more conditions.

Repeat the test as the intended user after changing anything. Verify source reading and destination writing separately; they are different operations with different access requirements.

From symptom to check

ObservationNext checkHow to interpret it
Works as rootRepeat as the unit’s UserTest the same access boundary as the application.
command not foundCheck command -v with the minimal PATHSeparate program discovery from execution failure.
Readable file, failing scriptCheck parents and working directoryA relative path may refer to a different file.

Common mistakes

  • Using chmod 777 instead of identifying the executing user.
  • Comparing only the command while ignoring its working directory.
  • Posting an entire environment with passwords into comments or chat.

How to prove recovery

  1. The user, group, directory and launch mechanism are known.
  2. Access checks were repeated as that account.
  3. Automation produces its result without wholesale privilege elevation.

MINI INCIDENT

One script, two outcomes

The backup works in your terminal; cron reports “sqlite3: not found”. Source and destination permissions have been checked as backup. What experiment separates the next hypothesis?

Evidence to investigate
interactive PATH=/usr/local/bin:/usr/bin:/bin
cron PATH=/usr/bin:/bin
Explore the diagnosis

Compare command -v sqlite3 in both environments and use its actual installed path. If lookup succeeds, move to the next check. Repeating chmod cannot change command discovery, and switching the job to root does not explain the difference.

WORKSHOP

Workshop: explain the evidence and choose an action

The username matches, but groups and process start times differ.

Sample evidence
interactive groups: backup,archive
running process groups: backup
archive directory group: archive
  1. Why does id in a new shell not describe the old process?

    Compare your reasoning

    The process inherited groups when it started. New account settings do not automatically replace an existing execution context.

  2. What should be compared before a new run?

    Compare your reasoning

    Compare the launcher, required supplementary group and directory access to identify the intended new process context.

  3. How can the repair be verified without extra privileges?

    Compare your reasoning

    Test the required operation with the intended user and groups after a controlled launch. Do not change the whole service to root to bypass one boundary.

CHECK YOUR UNDERSTANDING

Why check reading and writing separately?

Show explanation

They use different paths and permissions. Reading the source database does not prove that the destination directory permits creating a backup.

Documentation

Guest progress is stored separately in this browser.

TAKE IT TO THE TERMINAL

Practice on an incident

Choose a linked case and write three diagnostic checks before changing configuration. After the repair, repeat the original user operation.

DID IT MAKE SENSE?

Was this article helpful?

Rate the article or tell us what needed more explanation. Your feedback helps improve the lessons.

Helpful 1 Not helpful 0

Sign in to rate this article and leave a comment.

Discussion

0

Join the discussion →

No comments yet. Share your first impression or ask a question about this topic.