Skip to content
mentoor

Topic 4 of 5 · 5 min read

Files, permissions and environment variables

Goal: Diagnose a permission or environment-variable failure from an agent's error output and choose the least-privilege fix.

When a command fails with "permission denied" or "not found", read who is running it, which file it touches and which environment it sees, then grant the smallest access that fixes it. The common agent fix is sudo or chmod 777. Both work, and both widen access more than the problem needs.

Check yourself · 1 of 3

A file shows the mode -rw-r----- . Who can read it?

Choose one answer

Why it matters

Permissions and environment variables are where agents break things quietly. A chmod -R 777 clears the error and opens the files to every user. A .env file with real keys gets committed, printed in a log or pasted into a chat.

The parts

  • Paths. Absolute paths start with /. Relative paths depend on the current directory (pwd). ~ is your home.
  • Ownership and mode. ls -l shows lines like -rw-r----- 1 deploy www-data 812 .env: the file is owned by user deploy, group www-data; the owner can read and write, the group can read, others get nothing. Octal 640 means the same. Common modes: 644 for files, 755 for scripts and directories, 600 for secrets. A directory needs the execute bit to enter.
  • Who am I? id shows your user and groups. namei -l /path/to/file shows the mode of every directory on the way: a locked parent folder causes "permission denied" on a file that looks fine.
  • Environment variables. env lists them. A variable is visible to child programs only if exported. PATH is the list of folders searched for commands; "command not found" often means the tool is installed but not in the PATH of this shell or of the process manager.
  • .env files. They hold local settings and secrets. They belong in .gitignore, with an .env.example that lists the keys and no values.

Before and after

Weak: the agent sees EACCES: permission denied, open 'logs/app.log' and runs

sudo chmod -R 777 .

Fixed: check the cause, then change one thing.

ls -ld logs
id
chgrp app logs && chmod 770 logs

What goes wrong

  • Secrets in git or logs. git add . picks up .env. Run git status before committing, and never echo or cat a secret into agent output.
  • Running as root by habit. Files created under sudo become root-owned and break the next normal run.
  • Different environment, different result. A command works in your terminal and fails in cron or a process manager, because PATH and variables differ.

Ask the agent to name the user, the file and the missing permission before it changes anything.

Examples

Reading the mode string

$ ls -l
-rwxr-xr-x 1 root  root   214 deploy.sh
-rw------- 1 alice alice  96 .env
drwxr-x--- 2 alice www-data 4096 logs

deploy.sh runs for anyone. .env is readable only by alice, which is right for a secret. logs lets www-data read and enter but not write, so a web process that writes logs will fail until the group gets write access (chmod g+w logs).

Command not found under cron

* * * * * node /srv/app/job.js
# /bin/sh: node: not found

Cron runs with a short PATH. Use the full path from command -v node, or set PATH at the top of the crontab. Installing Node again would not fix it.

Key points

  • Read ls -l and id to see who owns a file and who is running the command before changing access.
  • Use namei -l to find a parent directory that blocks access to a file that looks fine.
  • Grant the smallest mode that works, such as 640 or 600 for secrets, instead of 777 or sudo.
  • A command found in your terminal can be missing from another process's PATH, such as cron or a process manager.
  • Keep .env out of git with .gitignore, commit an .env.example without values and never print secret values in agent output.

Common mistakes

  • Fixing every permission error with sudo or chmod 777 instead of finding the owner and the missing bit.
  • Running git add . without checking git status, so a .env file or key lands in a commit.
  • Testing in your own shell and assuming cron, CI or a process manager has the same PATH and variables.
  • Printing environment variables or config files into agent logs where secrets are then stored.

Check yourself · 2 of 3

An agent hits 'permission denied' writing to logs/app.log, which is owned by another user's group. What is the least-privilege fix?

Choose one answer

Check yourself · 3 of 3

Which practice keeps secrets in a .env file out of version control?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic