On this page
- What are environment variables?
- How do I set a variable for the current shell?
- How do I make a variable permanent for one user?
- How do I set variables for all users?
- Why do sudo and cron not see my variables?
- How do I pass variables to a systemd service?
- How should I store secrets such as API keys?
- How do I debug a missing or wrong variable?
- Questions
What are environment variables?
Environment variables are named strings a process inherits from its parent, used to pass settings such as paths, ports, API keys or the current language. Every program you start from a shell gets a copy of the shell environment; changes in the child never flow back to the parent. That rule explains most confusion: a variable set in one SSH session does not exist in another session, in cron or in a systemd service.
List what the current shell exports and read single values:
printenv | sort | head -30
printenv PATH
echo "$HOME"
env | grep -i langHow do I set a variable for the current shell?
Use export so child processes see the value; without it the variable stays local to the shell:
APP_PORT=8080
export APP_PORT
export APP_ENV=production
bash -c 'echo "$APP_PORT $APP_ENV"'
unset APP_ENVFor a single command, put the assignment in front of it. The value applies to that command only and leaves your shell untouched:
APP_ENV=staging ./run.sh
LC_ALL=C sort big.txtVariables set this way vanish when you log out. The Bash variables guide explains quoting and expansion rules in more detail.
How do I make a variable permanent for one user?
Add the export line to a startup file in that home directory. ~/.profile (or ~/.bash_profile on Rocky and Alma, if it exists) runs for login shells, which includes SSH sessions; ~/.bashrc runs for every interactive Bash shell:
echo 'export EDITOR=nano' >> ~/.bashrc
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.profile
source ~/.bashrc
echo "$EDITOR"On Ubuntu and Debian, the default ~/.profile sources ~/.bashrc, so either file works for SSH logins. Keep secrets out of these files when other admins share the account or when you sync dotfiles to Git.
How do I set variables for all users?
Use /etc/environment for plain values and /etc/profile.d/ for anything that needs shell logic. /etc/environment is read by PAM at login, not by a shell, so it takes simple KEY=value lines with no export and no variable expansion:
sudo nano /etc/environment
# add a line such as:
HTTP_TIMEOUT=30For values that build on other variables, drop a script into /etc/profile.d/:
echo 'export PATH="$PATH:/opt/tools/bin"' | sudo tee /etc/profile.d/tools.sh
sudo chmod 644 /etc/profile.d/tools.shBoth take effect at the next login; reconnect over SSH to test. Be careful with PATH in /etc/environment: a typo there can make basic commands disappear for every user.
Why do sudo and cron not see my variables?
sudo resets the environment on purpose, and cron starts with an almost empty one. With sudo you have three options:
sudo APP_ENV=production ./migrate.sh
sudo --preserve-env=APP_ENV ./migrate.sh
sudo -E ./migrate.shThe first form is the clearest. -E keeps everything, which may be blocked by the sudoers policy and can leak more than you meant. The secure_path setting in /etc/sudoers also replaces PATH, which is why a tool in ~/.local/bin is not found under sudo. For cron, set variables at the top of the crontab or source a file inside the script, as shown in the cron guide.
How do I pass variables to a systemd service?
Use Environment= for a few non-secret values and EnvironmentFile= for a list kept outside the unit:
[Service]
Environment=APP_ENV=production APP_PORT=8080
EnvironmentFile=/etc/myapp/envThe environment file holds one KEY=value per line, without export. Quotes around a value are allowed but not required. After editing either, apply the change:
sudo systemctl daemon-reload
sudo systemctl restart myapp
sudo systemctl show myapp -p Environment
sudo cat /proc/$(systemctl show -p MainPID --value myapp)/environ | tr "\0" "\n"The last line prints what the running process actually received, which settles any doubt about whether a change was picked up. Full unit examples are in run an app as a systemd service.
How should I store secrets such as API keys?
Keep secrets in a file only root and the service can read, never in the unit file, the shell history or a Git repository. A root-owned environment file with mode 600 is the common pattern, because systemd reads it as root before dropping privileges:
sudo install -d -m 700 /etc/myapp
sudo install -m 600 /dev/null /etc/myapp/env
sudo nano /etc/myapp/env
ls -l /etc/myapp/envUnit files are world readable and systemctl show prints Environment= values to any user, so secrets do not belong there. A stronger option is systemd credentials: the value is placed in a private directory for the service and never enters the environment:
[Service]
LoadCredential=api_key:/etc/myapp/api_keyThe app then reads the file $CREDENTIALS_DIRECTORY/api_key. To avoid secrets in your shell history, type them with read -rs API_KEY or start the command with a space when HISTCONTROL=ignorespace is set.
How do I debug a missing or wrong variable?
Check which process you are looking at, where it was started from, and what it really received. Useful steps:
- Run
printenv NAMEin a fresh SSH session to test login files. - Run
grep -r NAME ~/.bashrc ~/.profile /etc/environment /etc/profile.d/to find duplicates that overwrite each other. - Read
/proc/PID/environof the running program as shown above. - Remember that an application started before your change keeps its old environment until restarted.
Docker containers are separate again: pass values with -e or --env-file, as described in the Docker Engine tutorial.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A1-1 | any size, settings cost nothing | 1 vCPU, 1 GB RAM, 25 GB NVMe | $3.60/mo | $43.20 for 12 months |
Questions about Environment variables on Linux
What is the difference between .bashrc and .profile?
.profile runs once for login shells such as SSH sessions; .bashrc runs for every interactive Bash shell. On Ubuntu and Debian the default .profile loads .bashrc too.
Do I need to reboot after changing /etc/environment?
No. Log out and back in. Services managed by systemd do not read it at all; give them their own settings.
Can other users read my environment variables?
Other normal users cannot read /proc/PID/environ of your processes, but root can, and values in unit files or command lines are visible more widely. Keep secrets in files with tight permissions.
How do I remove a variable permanently?
Delete the line from the file that sets it, then start a new session. In the current shell run unset NAME.
Are .env files read automatically?
Only by applications or libraries that look for them, such as many Node.js and Python frameworks. The shell and systemd ignore .env unless you point EnvironmentFile= at it.
More in Server basics
- Create a sudo user
- Add a swap file
- Change the hostname
- Time zone and time sync
- File and directory permissions
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Automatic security updates
- Run an app as a systemd service
- Schedule jobs with cron and systemd timers
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- apt and dnf package management
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release