Hostxpeed
Login Get Started →
Getting Started

How to Add Environment Variables

5 min read
23 views
Jun 10, 2026

Prerequisites

Before setting environment variables, make sure you have:

  • SSH access to your VPS
  • Understanding of what environment variables you need to set

What are Environment Variables?

Environment variables store system-wide or user-specific configuration values like paths, API keys, database credentials, and application settings.

View Current Environment Variables

Connect to your VPS:

ssh hxroot@YOUR_SERVER_IP -p 22

View all variables:

env

View specific variable:

echo $PATH
echo $HOME
echo $USER

Method 1: Temporary Variable (Session Only)

Set variable for current session (lost after logout):

export MY_VAR="Hello World"

Set for single command:

MY_VAR="value" command

Example:

DEBUG=true python app.py

Method 2: User-Specific Permanent Variables (~/.bashrc)

Edit ~/.bashrc:

nano ~/.bashrc

Add at the end:

export MY_API_KEY="abc123xyz"
export APP_ENV="production"
export PATH="$HOME/bin:$PATH"

Reload configuration:

source ~/.bashrc

Method 3: Login Shell Variables (~/.profile)

For login shells (SSH, console login):

nano ~/.profile

Add environment variables:

export EDITOR=nano
export PAGER=less

Method 4: System-Wide Variables (/etc/environment)

Set variables for all users:

sudo nano /etc/environment

Add (no export keyword needed):

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
MAVEN_HOME="/opt/maven"

Apply changes:

source /etc/environment

Method 5: System-Wide Profile Scripts (/etc/profile.d/)

Create custom script:

sudo nano /etc/profile.d/custom.sh

Add:

export NODE_ENV="production"
export DATABASE_URL="postgresql://localhost/mydb"

Make executable:

sudo chmod +x /etc/profile.d/custom.sh

Method 6: Application-Specific (.env file)

Create .env file:

nano /var/www/myapp/.env

Content:

DB_HOST=localhost
DB_USER=myuser
DB_PASS=mypassword
APP_KEY=base64:abc123

Load in Node.js:

require('dotenv').config()

Load in Python:

from dotenv import load_dotenv
load_dotenv()

Common Environment Variables

  • PATH - Directories to search for executables
  • HOME - Current user''s home directory
  • USER - Current username
  • EDITOR - Default text editor
  • LANG - Language/locale settings
  • TZ - Timezone

Remove Environment Variable

Temporary (session):

unset MY_VAR

Permanent: Remove line from ~/.bashrc, ~/.profile, or /etc/environment

Environment Variables for Service/Supervisor

For systemd services:

sudo nano /etc/systemd/system/myapp.service

Add:

[Service]
Environment="MY_VAR=value"
Environment="ANOTHER_VAR=another"

Secure Management of Sensitive Variables

Never hardcode passwords in scripts!

Alternative: Use vault or secrets manager, or load from encrypted file.

Simple approach for CRON jobs:

0 2 * * * . /home/user/.env && /usr/bin/backup-script

Script to Manage Environment Variables

#!/bin/bash
# Set environment from .env file
if [ -f .env ]; then
    export $(cat .env | grep -v '^#' | xargs)
    echo "Loaded environment from .env"
else
    echo "No .env file found"
fi

✅ You can now set and manage environment variables on your Hostxpeed VPS.

Was this article helpful?