# Linux Environment Variables

{% tabs %}
{% tab title="Support VeryLazyTech 🎉" %}

* Become VeryLazyTech [**member**](https://shop.verylazytech.com/l/Membership)**! 🎁**
* **Follow** us on:
  * **✖ Twitter** [**@VeryLazyTech**](https://x.com/verylazytech)**.**
  * **👾 Github** [**@VeryLazyTech**](https://github.com/verylazytech)**.**
  * **📜 Medium** [**@VeryLazyTech**](https://medium.com/@verylazytech)**.**
  * **📺 YouTube** [**@VeryLazyTech**](https://www.youtube.com/@VeryLazyTechOfficial)**.**
  * **📩 Telegram** [**@VeryLazyTech**](https://t.me/+mSGyb008VL40MmVk)**.**
  * **🕵️‍♂️ My Site** [**@VeryLazyTech**](https://www.verylazytech.com/)**.**
* Visit our [**shop** ](https://shop.verylazytech.com/)for e-books and courses.  📚
  {% endtab %}
  {% endtabs %}

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

## **Types of Environment Variables in Linux**

### **1. System-Wide Environment 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`

### **2. User-Specific Environment Variables**

These are defined per user and stored in:

* `~/.bashrc`
* `~/.profile`
* `~/.bash_profile`

### **3. Shell Variables**

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

***

## **Commonly Used Linux Environment Variables**

1. **PATH**\
   The `PATH` variable defines directories where the system searches for executable files.

   ```bash
   echo $PATH
   export PATH=/usr/local/bin:$PATH
   ```
2. **HOME**\
   Represents the home directory of the current user.

   ```bash
   echo $HOME
   ```
3. **USER**\
   Stores the username of the logged-in user.

   ```bash
   echo $USER
   ```
4. **SHELL**\
   Specifies the default shell of the user.

   ```bash
   echo $SHELL
   ```
5. **EDITOR**\
   Defines the default text editor.

   ```bash
   export EDITOR=nano
   ```
6. **LANG**\
   Sets the system language.

   ```bash
   export LANG=en_US.UTF-8
   ```
7. **DISPLAY**\
   Specifies the display used by the X Window System.

   ```bash
   echo $DISPLAY
   export DISPLAY=:0.0
   ```
8. **HISTFILESIZE**\
   Sets the maximum number of lines contained in the history file.

   ```bash
   echo $HISTFILESIZE
   export HISTFILESIZE=5000
   ```
9. **HISTSIZE**\
   Defines the number of lines added to the history file per session.

   ```bash
   echo $HISTSIZE
   export HISTSIZE=1000
   ```
10. **HOSTNAME**\
    Stores the hostname of the computer.

    ```bash
    echo $HOSTNAME
    ```
11. **MAIL**\
    Specifies the location of the user’s mail spool.

    ```bash
    echo $MAIL
    ```
12. **MANPATH**\
    Defines the list of directories to search for manual pages.

    ```bash
    echo $MANPATH
    export MANPATH=/usr/local/share/man:$MANPATH
    ```
13. **OSTYPE**\
    Indicates the type of operating system.

    ```bash
    echo $OSTYPE
    ```
14. **PS1**\
    Defines the default Bash prompt.

    ```bash
    echo $PS1
    export PS1="[\u@\h \W]\$ "
    ```
15. **PWD**\
    Stores the current working directory.

    ```bash
    echo $PWD
    ```
16. **TERM**\
    Specifies the current terminal type (e.g., `xterm`, `linux`).

    ```bash
    echo $TERM
    export TERM=xterm-256color
    ```
17. **TZ**\
    Sets the time zone.

    ```bash
    echo $TZ
    export TZ=America/New_York
    ```

***

## **How to View Environment Variables in Linux**

#### **1. Using the `printenv` Command**

```bash
printenv
printenv PATH
```

#### **2. Using the `env` Command**

```bash
env
```

#### **3. Using the `set` Command**

```bash
set | less
```

***

## **How to Set and Export Environment Variables in Linux**

#### **1. Temporarily Setting an Environment Variable**

```bash
export MY_VAR="Hello World"
echo $MY_VAR
```

This variable will be available only for the current session.

#### **2. Permanently Setting an Environment Variable**

To make a variable persistent, add it to `~/.bashrc` or `~/.profile`.

```bash
echo 'export MY_VAR="Hello World"' >> ~/.bashrc
source ~/.bashrc
```

### **How to Unset Environment Variables**

To remove an environment variable:

```bash
unset MY_VAR
echo $MY_VAR  # No output
```

***

## **Working with Environment Variables in Scripts**

Environment variables are often used in **Bash scripting** to automate tasks.

Example script:

```bash
#!/bin/bash
echo "The current user is: $USER"
echo "The home directory is: $HOME"
```

Save this as `script.sh`, then execute:

```bash
bash script.sh
```

### **Security Considerations for Environment Variables**

1. **Avoid Storing Sensitive Data**: Never store passwords in environment variables.
2. **Use `readonly` for Critical Variables**:

   ```bash
   readonly SECURE_VAR="Sensitive Data"
   ```
3. **Restrict Access to Environment Files**:

   ```bash
   chmod 600 ~/.bashrc
   ```

## Interesting variables for hacking <a href="#interesting-variables-for-hacking" id="interesting-variables-for-hacking"></a>

#### **HISTFILESIZE** <a href="#histfilesize" id="histfilesize"></a>

Change the **value of this variable to 0**, so when you **end your session** the **history file** (\~/.bash\_history) **will be deleted**.

```bash
export HISTFILESIZE=0
```

#### **HISTSIZE** <a href="#histsize" id="histsize"></a>

Change the **value of this variable to 0**, so when you **end your session** any command will be added to the **history file** (\~/.bash\_history).

```bash
export HISTSIZE=0
```

#### http\_proxy & https\_proxy <a href="#http_proxy--https_proxy" id="http_proxy--https_proxy"></a>

The processes will use the **proxy** declared here to connect to internet through **http or https**.

```bash
export http_proxy="http://10.10.10.10:8080"
export https_proxy="http://10.10.10.10:8080"
```

#### SSL\_CERT\_FILE & SSL\_CERT\_DIR <a href="#ssl_cert_file--ssl_cert_dir" id="ssl_cert_file--ssl_cert_dir"></a>

The processes will trust the certificates indicated in **these env variables**.

```bash
export SSL_CERT_FILE=/path/to/ca-bundle.pem
export SSL_CERT_DIR=/path/to/ca-certificates
```

**LD\_PRELOAD**\
Allows injecting shared libraries into running processes, often used for privilege escalation or bypassing security measures.

```bash
export LD_PRELOAD=/tmp/malicious.so
```

**LD\_LIBRARY\_PATH**\
Defines directories where the dynamic linker searches for shared libraries, which can be used for hijacking.

```bash
export LD_LIBRARY_PATH=/tmp/mylib:$LD_LIBRARY_PATH
```

**PATH Manipulation**\
Adding a malicious directory to `PATH` can be used for command hijacking.

```bash
export PATH=/tmp/malicious:$PATH
```

**TMOUT**\
Automatically logs out an idle user, useful for clearing sessions quickly.

```bash
export TMOUT=1
```

**XDG\_CONFIG\_HOME**\
Can be used to control where applications store configuration files, potentially allowing manipulation.

```bash
export XDG_CONFIG_HOME=/tmp/custom-config
```

**IFS (Internal Field Separator)**\
Modifying `IFS` can be used to change command parsing behavior in scripts.

```bash
export IFS=$'\n'
```

**PS1 Manipulation**\
Modify the prompt to hide the current user or create deception.

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

**HOME**\
Change `HOME` to manipulate where programs store configurations or execute files.

```bash
export HOME=/tmp/fakehome
```

**MAIL**\
Modify the mail spool directory to read or redirect emails.

```bash
export MAIL=/tmp/mail
```

**SUDO\_ASKPASS**\
Trick `sudo` into using a fake prompt to steal passwords.

```bash
export SUDO_ASKPASS=/tmp/fake-pass-prompt
sudo -A whoami
```

**GDBINIT**\
Define a malicious GDB startup file to execute arbitrary commands.

```bash
export GDBINIT=/tmp/malicious-gdbinit
```

***

{% embed url="<https://shop.verylazytech.com/l/2023OSCPOffSecPenetrationTestingwithKaliLinux>" %}
