Post

🐧 Linux Environment Variables

Introduction to Linux Environment Variables

In a Linux operating system, environment variables are dynamic values that define the behavior of system processes and applications. These variables store configuration data, such as the system path, user preferences, and settings, making them essential for efficient system operations and automation.

Environment Variables

πŸ”’ System-Wide Variables

These are available for all users and are set by the system administrator. They are defined in files such as:

  • /etc/environment
  • /etc/profile
  • /etc/bash.bashrc

πŸ‘€ User-Specific Variables

These are defined per user and stored in:

  • ~/.bashrc
  • ~/.profile
  • ~/.bash_profile

🐚 Shell Variables

Shell variables exist only within the running shell session. They can be created and modified within the terminal.

πŸ“Œ Commonly Used Linux Variables

πŸ“‚ File & Directory

PATH The PATH variable defines directories where the system searches for executable files.

1
2
3
4
5
6
7
8
9
# PATH - Executable lookup directories
echo $PATH
export PATH=/usr/local/bin:$PATH

# HOME - User's home directory
echo $HOME

# PWD - Present working directory
echo $PWD

πŸ‘€ User Information

1
2
3
4
5
# USER - Current username
echo $USER

# HOSTNAME - System hostname
echo $HOSTNAME

🐚 Shell Settings

1
2
3
4
5
6
# SHELL - Default shell
echo $SHELL

# PS1 - Bash prompt string
echo $PS1
export PS1="[\u@\h \W]\$ "

πŸ“ Editors and Language

1
2
3
4
5
# EDITOR - Default text editor
export EDITOR=nano

# LANG - System language
export LANG=en_US.UTF-8

πŸ–₯️ GUI and Display

1
2
3
# DISPLAY - X Window System display
echo $DISPLAY
export DISPLAY=:0.0

πŸ“œ History

1
2
3
4
5
6
7
# HISTFILESIZE - Max history file size
echo $HISTFILESIZE
export HISTFILESIZE=5000

# HISTSIZE - Commands per session
echo $HISTSIZE
export HISTSIZE=1000

πŸ“¬ Mail

1
2
# MAIL - Mail spool location
echo $MAIL

πŸ“š Documentation

1
2
3
# MANPATH - Manual search path
echo $MANPATH
export MANPATH=/usr/local/share/man:$MANPATH

πŸ–₯️ Terminal

1
2
3
# TERM - Terminal type
echo $TERM
export TERM=xterm-256color

πŸ•’ Time

1
2
# TZ - Timezone
export TZ=Asia/Kolkata

🧠 OS Info

1
2
# OSTYPE - OS type
echo $OSTYPE

πŸ” Viewing Environment Variables

πŸ“¦ Using printenv

1
2
printenv
printenv PATH

🌍 Using env

1
env

πŸ“ƒ Using set

1
set | less

✍️ Setting Environment Variables

πŸ”„ Temporarily (Current Session Only)

1
2
export MY_VAR="Hello World"
echo $MY_VAR

πŸ’Ύ Permanently (Across Sessions)

Add the variable to ~/.bashrc or ~/.profile:

1
2
echo 'export MY_VAR="Hello World"' >> ~/.bashrc
source ~/.bashrc

πŸ—‘οΈ Unsetting Variables

1
2
unset MY_VAR
echo $MY_VAR  # No output expected

πŸ› οΈ Using Variables in Scripts

πŸ§ͺ Example Bash Script

1
2
3
#!/bin/bash
echo "Current user: $USER"
echo "Home directory: $HOME"

Save as script.sh and run:

1
2
chmod +x script.sh
./script.sh

πŸ” Security Tips for Env Variables

  1. Avoid storing sensitive data (e.g., passwords) in environment variables.
  2. Use readonly for critical variables:
    1
    
     readonly SECURE_VAR="Secret"
    
  3. Restrict permissions:
    1
    
     chmod 600 ~/.bashrc
    

    πŸ•΅οΈ Advanced and Offensive Usage

πŸ“œ History Deletion

1
2
export HISTFILESIZE=0
export HISTSIZE=0

🌐 Proxy Redirection

1
2
export http_proxy="http://10.10.10.10:8080"
export https_proxy="http://10.10.10.10:8080"

πŸ”’ SSL Certificate Trust

1
2
export SSL_CERT_FILE=/path/to/ca-bundle.pem
export SSL_CERT_DIR=/path/to/ca-certificates

🧬 Library Injection

1
2
export LD_PRELOAD=/tmp/malicious.so
export LD_LIBRARY_PATH=/tmp/mylib:$LD_LIBRARY_PATH

πŸͺ€ PATH Hijacking

1
export PATH=/tmp/malicious:$PATH

⏱️ Auto-Logout Timeout

1
export TMOUT=1

πŸ› οΈ App Config Hijack

1
export XDG_CONFIG_HOME=/tmp/custom-config

πŸ” Field Separator Manipulation

1
export IFS=$'\n'

πŸ§™ PS1 Prompt Manipulation

1
export PS1='[\u@\h \W]# '

🏠 HOME Override

1
export HOME=/tmp/fakehome

πŸ“§ MAIL Redirection

1
export MAIL=/tmp/mail

πŸ§ͺ Sudo Askpass Exploit

1
2
export SUDO_ASKPASS=/tmp/fake-pass-prompt
sudo -A whoami

🐞 GDB Exploitation

1
export GDBINIT=/tmp/malicious-gdbinit

🧾 Summary

Environment variables are a fundamental aspect of Linux systems. They control how the system and user sessions behave, help in scripting, system management, and even (mis)use in penetration testing. Managing them wisely leads to a more secure and efficient Linux environment.

This post is licensed under CC BY 4.0 by the author.