Linux Environment Variables
Configure system-wide and user-level environment variables, PATH manipulation, and shell persistence (.bashrc, /etc/environment).
1. Setting and Exporting Variables
In Linux, a variable created without export is private to the current shell. Using export makes the variable available to all child processes and commands spawned by the shell:
# Temporary variable for current shell session only
export PORT=3000
export API_KEY="sk_live_12345"
# View variable value
echo $PORT
# Unset variable
unset API_KEY2. Inspecting Active Environment Variables
# Print all exported environment variables
printenv
# Filter for specific variable
printenv PATH
# or
env | grep NODE_3. Persistent User Configuration (~/.bashrc / ~/.zshrc)
Variables set via terminal export disappear when the shell session closes. To persist them across logins:
# Append to user's shell configuration
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
echo 'export EDITOR="nano"' >> ~/.bashrc
# Reload shell configuration in current session
source ~/.bashrc4. System-Wide Environment Variables (/etc/environment)
For variables that must be available globally to all users and system services:
# /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
NODE_ENV="production"
TZ="UTC"5. Understanding the $PATH Variable
PATH is a colon-separated list of directories where the shell searches for executable binary commands:
echo $PATH
# Output: /usr/local/bin:/usr/bin:/bin:/usr/sbinWhen you type node, Linux checks /usr/local/bin/node, then /usr/bin/node, running the first executable match it discovers.
Linux SSH Configuration & Hardening
Generate Ed25519 SSH keypairs, configure ~/.ssh/config, manage authorized_keys, and harden sshd_config against unauthorized access.
Standard Streams: stdin, stdout, and stderr
Master Unix standard I/O streams (File Descriptors 0, 1, 2), stream pipelines, and discarding output with /dev/null.