> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Set an Environment Variable Permanently on Linux

> Set an environment variable permanently on Linux by adding export NAME=value to ~/.bashrc, then run source ~/.bashrc. Use /etc/environment for all users.

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`.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 5 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 (bash) | sudo for system-wide files |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md).
* 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

```bash theme={null}
export APP_ENV=production
echo $APP_ENV
```

```text theme={null}
production
```

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

> [tab] For your user (\~/.bashrc)
>
> **1. Open .bashrc**
>
> ```bash theme={null}
> nano ~/.bashrc
> ```
>
> **2. Add the variable at the end**
>
> ```bash theme={null}
> export APP_ENV=production
> export PATH="$HOME/bin:$PATH"
> ```
>
> Use quotes if the value has spaces: `export GREETING="hello world"`.
>
> **3. Load it**
>
> ```bash theme={null}
> source ~/.bashrc
> ```
>
> New SSH sessions load it automatically.

> [tab] For all users (/etc/environment)
>
> **1. Open the file**
>
> ```bash theme={null}
> sudo nano /etc/environment
> ```
>
> **2. Add one variable per line**
>
> ```text theme={null}
> APP_ENV=production
> ```
>
> This file isn't a script: no `export`, no `$VARIABLE` expansion. Be careful with the existing `PATH=` line; a mistake there breaks commands for everyone.
>
> **3. Log in again**
>
> `/etc/environment` is read at login, so start a new SSH session.

> [tab] For all users (script)
>
> For values that need `$PATH` or other variables, create a script in `/etc/profile.d/`:
>
> ```bash theme={null}
> sudo nano /etc/profile.d/myapp.sh
> ```
>
> ```bash theme={null}
> export APP_ENV=production
> export PATH="$PATH:/opt/myapp/bin"
> ```
>
> Login shells read every `*.sh` file in `/etc/profile.d/`. Log in again to apply it.

## Which file should I use?

| File | Applies to | Read when |
| - | - | - |
| `~/.bashrc` | You | Every interactive bash shell (on Ubuntu/Debian also at SSH login, through `~/.profile`) |
| `~/.profile` or `~/.bash_profile` | You | Login shells, such as a new SSH session |
| `/etc/environment` | All users | Login (read by PAM), not a script |
| `/etc/profile.d/*.sh` | All users | Login shells |

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](https://help.freakhosting.com/portals/other/how-to-create-a-systemd-service.md).
* **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](https://help.freakhosting.com/portals/other/how-to-schedule-tasks-with-cron.md).
* **sudo:** sudo resets most variables for safety. Pass one on purpose with `sudo APP_ENV=production command` if your sudo policy allows it.

> [!WARNING]
>
> 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:

```bash theme={null}
printenv APP_ENV
echo $PATH
```

`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](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md).

## 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.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Edit files with nano and vim](https://help.freakhosting.com/portals/other/how-to-edit-files-with-nano-and-vim.md)
>
> Change .bashrc safely.

> [card] [Create a systemd service](https://help.freakhosting.com/portals/other/how-to-create-a-systemd-service.md)
>
> Environment for services.

> [card] [Schedule tasks with cron](https://help.freakhosting.com/portals/other/how-to-schedule-tasks-with-cron.md)
>
> Environment for cron jobs.

***

> [!NOTE]
>
> **Checked against the official [GNU Bash Reference Manual: Bash Startup Files](https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html) 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.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.