Linux Environment Variables Explained: PATH, $HOME and the Security Risks Hiding in Them
Type ls and your terminal finds the program instantly. Type echo $HOME and it knows where you live on the system. Neither of these is magic — both are powered by environment variables, invisible settings that shape every command you run. Understanding them is a Linux fundamental, and misconfiguring them is a real security risk.
What Are Environment Variables?
Environment variables are named values available to every program running in your shell session. Think of them as global settings for your terminal. See them all with:
env
Or check one at a time:
echo $HOME
echo $USER
echo $PATH
The Big Ones You Should Know
- PATH — a colon-separated list of directories where the shell looks for programs. When you type ls, the shell searches these directories in order until it finds an executable named ls.
- HOME — your home directory, e.g. /home/anony.
- USER — your username.
- SHELL — your current shell, e.g. /bin/bash.
- PWD — your current directory.
Why PATH Order Matters (and Why It’s Dangerous)
Run this:
echo $PATH
You will see something like:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The shell checks directories left to right. Now imagine a directory you can write to — like . (the current directory) — appears before the system directories. An attacker who can drop a malicious file named ls in that directory wins: when you type ls, you run their file, not the real one. This is called PATH hijacking, and it is a classic privilege-escalation trick.
Two rules follow:
Check for trouble with:
echo $PATH | tr ':' '\n'
Setting Variables (Temporarily vs Permanently)
Set a variable for one session:
export MY_VAR="hello"
echo $MY_VAR
Close the terminal and it is gone. To make it permanent, add the export line to ~/.bashrc (for interactive shells) or ~/.profile (for login shells), then reload:
source ~/.bashrc
The Security Mistakes to Avoid
- Secrets in environment variables are not truly secret. They show up in /proc/<pid>/environ for running processes and in shell history if you export them carelessly. For real secrets, use a proper secrets manager or a file with 600 permissions — never paste API keys into .bashrc on a shared machine.
- LD_PRELOAD and LD_LIBRARY_PATH let you override shared libraries. Attackers love these for hijacking programs. If you see them set and you did not set them, investigate.
- Sudo strips most variables for a reason. When you run sudo, it deliberately resets the environment so a poisoned PATH or library path cannot escalate privileges. Do not defeat this with sudo -E unless you understand exactly what you are preserving.
A Quick Defensive Habit
Add this to your routine when auditing a Linux box:
env | sort
echo $PATH | tr ':' '\n'
Five seconds, and you can spot a hijacked PATH, a leaked secret, or a suspicious library override before it becomes an incident.
Environment variables are quiet, boring, and everywhere — which is exactly why attackers target them. Know yours.



Comments
Post a Comment