How-to

How to Set Environment Variables on a Linux Server

Where you set a variable decides who sees it: one command, your shell, every login, or a background service. Services ignore your shell files, which trips up almost everyone once.

How do I see the current variables?

printenv | sort | less
printenv HOME LANG
env | grep -i proxy

printenv lists exported variables, the ones child processes inherit. Shell variables that were set but not exported do not appear; set shows those too, along with shell functions.

How do I set a variable for one command only?

LC_ALL=C sort big.txt > sorted.txt
NODE_ENV=production PORT=3100 node server.js
env -i PATH=/usr/bin:/bin bash --noprofile --norc

A prefix assignment applies to that command and disappears afterwards. env -i starts a program with an empty environment plus whatever you list, useful to test how a script behaves under cron or systemd, where the environment is minimal.

How do I set it for the current session?

export APP_ENV=staging
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
unset APP_ENV

export makes the variable visible to programs started from this shell. It is gone when you log out.

Which file makes it permanent for my user?

FileRead byGood for
~/.profile or ~/.bash_profileLogin shells, such as SSH sessionsExported variables and PATH
~/.bashrcInteractive non login shells, and login shells that source itAliases, prompt, shell options
/etc/environmentPAM at login for every userSimple system wide KEY=value pairs
/etc/profile.d/*.shLogin shells for every userSystem wide exports with shell logic

On Debian and Ubuntu, ~/.profile sources ~/.bashrc, so variables in either usually show up over SSH. On the RHEL family, ~/.bash_profile does the same. Put exports in the profile file and shell tweaks in bashrc and you will rarely be surprised.

echo 'export EDITOR=vim' >> ~/.profile
. ~/.profile

How do I set a variable for every user?

echo 'TZ=UTC' | sudo tee -a /etc/environment
sudo tee /etc/profile.d/company.sh >/dev/null <<'EOF'
export HISTTIMEFORMAT='%F %T '
export PIP_INDEX_URL=https://pypi.example.internal/simple
EOF

/etc/environment is not a shell script: no export, no variable expansion, just KEY=value lines. Use /etc/profile.d/ when you need logic or expansion. Both apply at the next login.

Why does my service not see the variable?

systemd services do not read .bashrc, .profile or /etc/profile.d. They start with a small clean environment. Set variables in the unit instead, with a drop-in so package updates do not overwrite them:

sudo systemctl edit shop-api.service
# in the editor:
[Service]
Environment="APP_ENV=production" "WORKERS=12"
EnvironmentFile=/etc/shop-api/env

sudo systemctl daemon-reload
sudo systemctl restart shop-api
sudo systemctl show shop-api -p Environment

EnvironmentFile reads KEY=value lines from a file, which keeps secrets out of the unit file itself. Set the file to mode 600 and owned by root. The same rule applies to cron: cron jobs get a minimal environment, so set variables at the top of the crontab or inside the script. Set up a cron job shows the pattern.

Where should secrets like API keys go?

Not in /etc/environment or a profile file, because every process and user would inherit them. Good options:

  • An EnvironmentFile with mode 600 read only by the service.
  • systemd credentials with LoadCredential=, which pass a file to the service without putting the secret in its environment at all.
  • For containers, Docker or Compose secrets mounted as files.

Environment variables are visible to the process owner through /proc/PID/environ and often get dumped into error reports, so files are the safer channel for anything sensitive.

What about sudo and the environment?

sudo resets most variables by default for safety. To keep one for a single command, use sudo --preserve-env=HTTPS_PROXY apt update, or add Defaults env_keep += "HTTPS_PROXY" to a sudoers drop-in. Do not keep PATH or anything starting with LD_.

How do I check what a running process actually got?

sudo tr '\0' '\n' < /proc/$(pgrep -o -f shop-api)/environ | sort

That prints the environment the process started with. It does not change if you edit files later, so restart the process after any change.

How do I pass variables to a Docker container?

Use -e NAME=value on docker run, or an environment: block or env_file: entry in a Compose file. Variables on the host do not leak into containers unless you pass them on purpose, which is a feature. Keep the env file next to your compose file with mode 600 and out of version control.

Machines for App Servers and APIs

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S3Intel Xeon E-2356G6 / 1264 GB DDR42x 1 TB NVMe2x 10 Gbps$80.38$80.38 (1 month)

Keep Reading

Add a directory to PATH

Add a folder to PATH for your session, your user, every user or a systemd service, in the right order and without duplicates.

Open

Set up a cron job

Create a cron job that actually runs: crontab -e, full paths, environment, logging, locking against overlap and mail-free alerts.

Open

The sudo command

How sudo decides who may run what, the flags you use daily, safe sudoers rules, logging and the habits that keep root powers contained.

Open

SaaS hosting

Host a multi-tenant web app on bare metal: app, workers and database split by box as customers grow.

Open

FAQ

Quick Answers

Add an export line to ~/.profile for one user, or use /etc/environment or /etc/profile.d for everyone.

Services do not start a shell. Use Environment= or EnvironmentFile= in the unit.

/etc/environment is read by PAM as plain KEY=value. /etc/profile and profile.d are shell scripts for login shells.

Run unset NAME for the session and delete its line from the file that sets it.

Not ideal. Use files with mode 600 or systemd credentials instead.