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?
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 -lshows lines like-rw-r----- 1 deploy www-data 812 .env: the file is owned by userdeploy, groupwww-data; the owner can read and write, the group can read, others get nothing. Octal640means the same. Common modes:644for files,755for scripts and directories,600for secrets. A directory needs the execute bit to enter. - Who am I?
idshows your user and groups.namei -l /path/to/fileshows the mode of every directory on the way: a locked parent folder causes "permission denied" on a file that looks fine. - Environment variables.
envlists them. A variable is visible to child programs only if exported.PATHis the list of folders searched for commands; "command not found" often means the tool is installed but not in thePATHof this shell or of the process manager. - .env files. They hold local settings and secrets. They belong in
.gitignore, with an.env.examplethat 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. Rungit statusbefore committing, and neverechoorcata 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
PATHand 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?
Check yourself · 3 of 3
Which practice keeps secrets in a .env file out of version control?
Practice items, not counted towards your mastery.
Start here
- The Missing Semester of Your CS Education (MIT) (opens in a new tab)CourseFree
Free lectures on the shell, shell scripting, data wrangling and command-line tools.
- ShellCheck (opens in a new tab)ToolFree
Paste a script or command and see unquoted variables and other classic traps flagged.
- The Linux Command Line (free book) (opens in a new tab)BookFree
A full book on navigation, permissions, processes, pipes and scripting.
More for this topic
- chmod(1) Linux manual page (opens in a new tab)DocsFree
Exact meaning of file modes, symbolic and octal, and recursive changes.
- credentials(7) Linux manual page (opens in a new tab)DocsFree
Explains user ids, group ids and process ids and how the kernel uses them.