Skip to main content
To set an environment variable permanently on Linux for your user, add a line such as export APP_ENV=production to the end of ~/.bashrc, then run source ~/.bashrc (or log in again). For all users, add APP_ENV=production (without export) to /etc/environment. Check it with echo $APP_ENV or printenv APP_ENV.

Before you start

  • Connect with SSH.
  • An environment variable is a named value that programs can read, such as PATH (where the shell looks for commands) or HOME (your home folder). Apps often read settings such as API keys or NODE_ENV this way.
  • These steps are for the bash shell, the default on Ubuntu, Debian, AlmaLinux and Rocky Linux. Check yours with echo $SHELL.

How to set a variable for the current session

The variable lasts until you log out, and programs you start from this shell inherit it. Without export, only the shell itself sees it, not the programs it starts. To remove it: unset APP_ENV.

How to set an environment variable permanently

1

Open .bashrc

2

Add the variable at the end

Use quotes if the value has spaces: export GREETING="hello world".
3

Load it

New SSH sessions load it automatically.

Which file should I use?

If both ~/.bash_profile and ~/.profile exist, bash reads only ~/.bash_profile at login. On AlmaLinux and Rocky, ~/.bash_profile loads ~/.bashrc, so ~/.bashrc works for both.

How to set variables for services and cron jobs

~/.bashrc is not read by systemd services or cron jobs. Set variables there instead:
  • systemd service: add Environment=APP_ENV=production or EnvironmentFile=/opt/myapp/.env to the [Service] section, then sudo systemctl daemon-reload and restart. See create a systemd service.
  • cron: put APP_ENV=production on its own line at the top of the crontab (crontab -e), or set it inside the script. See schedule tasks with cron.
  • sudo: sudo resets most variables for safety. Pass one on purpose with sudo APP_ENV=production command if your sudo policy allows it.
Variables set in .bashrc or /etc/environment can be read by any process you run, and /etc/environment by every user. Don’t put passwords or API keys there. Keep secrets in a file only the app user can read (chmod 600) and load it with EnvironmentFile=.

How to check it worked

Open a new SSH session and run:
printenv prints the value, or nothing if the variable isn’t set. To see all variables, run printenv or env. If a broken .bashrc stops you logging in normally, connect with ssh -t alex@203.0.113.10 bash --norc and fix the file, or use the VNC console.

Frequently asked questions

How do I add a folder to PATH permanently?

Add export PATH="$HOME/bin:$PATH" to ~/.bashrc and run source ~/.bashrc. Put the new folder first to override system commands, or last ($PATH:$HOME/bin) to only add new ones.

Why is my variable empty in a new terminal?

You set it with export only in the old session, or you added it to a file that this kind of shell doesn’t read. Use ~/.bashrc for your user, and log in again after changing /etc/environment.

What is the difference between env and printenv?

Both list environment variables. printenv NAME prints one value. env NAME=value command runs a single command with an extra variable.

Can I use a .env file?

Yes. Many apps read .env themselves. To load one into your shell, run set -a; source .env; set +a. For services, use EnvironmentFile= in the systemd unit.

VPS and dedicated server guides

Every server guide in one place.

Edit files with nano and vim

Change .bashrc safely.

Create a systemd service

Environment for services.

Schedule tasks with cron

Environment for cron jobs.

Checked against the official GNU Bash Reference Manual: Bash Startup Files and the pam_env documentation on 4 November 2026; commands tested on Ubuntu 24.04 LTS. Commands can differ on other distributions, shells or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026