Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to read and set environment variables on a Linux VPS

This guide shows you how to see, set and save environment variables on a Linux server.

How-To Guides2 min read14 steps

What is an environment variable?

An environment variable is a named setting that programs can read. It is like a sticky note with a name and a value. For example, HOME holds the path to your home folder. Programs read these notes to know how to behave.

You need SSH access to follow along. SSH is a safe way to type commands on a remote server.

Read variables

  1. Log in to your server over SSH.
  2. List all variables.
printenv

This prints every variable and its value.

  1. Show just one.
echo $HOME

The dollar sign tells the shell to give you the value.

  1. Look at the list of folders the system searches for programs.
echo $PATH

Set a variable for the current session

This lasts until you log out.

  1. Create the variable. No spaces around the equal sign.
MY_NAME="Hostvento"
  1. Make it visible to programs you start. This is called exporting.
export MY_NAME
  1. Check it.
echo $MY_NAME

To remove it, type unset MY_NAME.

Save a variable for your user

  1. Open the file ~/.bashrc in a text editor such as nano.
nano ~/.bashrc
  1. Go to the end and add a line.
export MY_NAME="Hostvento"
  1. Save the file. In nano, press Ctrl+O, Enter, then Ctrl+X.
  2. Load the change.
source ~/.bashrc

Save a variable for all users (root needed)

This step needs root access and affects everyone. Take a backup of the file first.

  1. Copy the file for safety.
sudo cp /etc/environment /etc/environment.bak
  1. Open /etc/environment and add a line like MY_NAME="Hostvento". Do not use the word export here.
  2. Log out and back in.

Another way is to put an export line in a file inside /etc/profile.d/, ending in .sh.

Add to PATH

export PATH=$PATH:/opt/mytool/bin

This adds a folder to the search list without removing the old ones. Never write PATH=/opt/mytool/bin alone, or the system will stop finding normal commands.

Tip: Do not put secrets like passwords in shared files. Other users or logs might expose them.

Quick recap

  • Environment variables are named settings that programs read.
  • Read them with printenv and echo $NAME.
  • Set them with export NAME=value.
  • Save them in ~/.bashrc or /etc/environment.