Mastering Environment Variables in Linux: A Comprehensive Guide

Written by sajerestan | Published 2023/11/22
Tech Story Tags: kali-linux | linux | linux-and-unix | linux-for-newbies | kali-linux-persistent-drive | ubuntu | linux-beginners | environment-variables

TLDRvia the TL;DR App

Table of Contents

  1. Introduction
  2. What are Environment Variables?
  3. Purpose of Environment Variables
  4. Types of Environment Variables
  5. Setting Environment Variables for Single Users
    • 5.1 Temporary Environment Variables
    • 5.2 Permanent Environment Variables for Single Users
  6. Setting Environment Variables for All Users
    • 6.1 Permanent Environment Variables (Using Profile File)
    • 6.2 Permanent Environment Variables (Using Environment File)
  7. Verifying the Environment Variable
  8. Troubleshooting
    • 8.1 Troubleshooting in Firefox
    • 8.2 Troubleshooting in Chrome
  9. Conclusion

Introduction

In the Linux operating system, environment variables are essential components that enable users to store and access information across various processes and user sessions. They provide a useful way to customize system behavior, configure applications, and simplify repetitive tasks. This guide will explore multiple approaches to setting environment variables in Linux, catering to both individual users and system-wide configurations.

What are Environment Variables?

Environment variables are values that can change and impact how programs behave on a computer system. They are name-value pairs that all processes can access within a particular user environment or shell session. These variables provide a flexible and convenient method for managing system-wide settings, configuring applications, and customizing system behavior.

Purpose of Environment Variables:

Environment variables serve several crucial purposes:

  1. Customization: Environment variables allow users to tailor their computing experience by defining specific settings or preferences. For instance, a user can set an environment variable to specify their preferred text editor or default directory.
  2. Application Configuration: Applications often rely on environment variables to access configuration settings or locate resources. For example, the PATH environment variable informs applications where to search for executable files.
  3. System-Wide Management: Administrators can use environment variables to manage system-wide settings and configurations. For instance, setting the TZ environment variable defines the default time zone for all users.

Types of Environment Variables:

Environment variables can be classified into two main categories:

  1. User Environment Variables:These variables are specific to a user and are stored in their profile files. Changes to user environment variables only affect the environment of the respective user.
  2. System Environment Variables: These variables are global to the system and are defined in system-wide configuration files. Changes to system environment variables affect the environment of all users.

SSLKEYLOGFILE=/home/stanley/Documents/Coding/browser-log/sslkeylog.log

Setting Environment Variables for Single Users

Method 1: Temporary Environment Variables

Temporary environment variables are defined within the current shell session and cease to exist once the shell terminates. To set a temporary environment variable, use the following syntax:

SSLKEYLOGFILE=/home/stanley/Documents/Coding/browser-log/sslkeylog.log

Method 2: Permanent Environment Variables for Single Users

Permanent environment variables persist across shell sessions and are available to the user who sets them. To set a permanent environment variable for a single user, use the export command followed by the variable definition:

export SSLKEYLOGFILE=/home/stanley/Documents/Coding/browser-log/sslkeylog.log

To make this permanent change effective, add the export command and variable definition to the user's initialization file. These files are typically located in the user's home directory:

  • ~/.bashrc: For bash users
  • ~/.zshrc: For zsh users

Open the appropriate file using a text editor like nano or vi. Add the export command followed by the variable definition to the end of the file. Save the file and exit the text editor. The next time the user logs in or opens a new terminal session, the permanent environment variable will be set.

Setting Environment Variables for All Users

Method 1: Permanent Environment Variables for All Users (Using Profile File)

Permanent environment variables for all users can be set by adding them to the /etc/profile file. This file is read by all users when they log in, so any changes made to the file will be reflected in the environment of all users.

  1. Open the /etc/profile file in a text editor with sudo:
sudo nano /etc/profile
  1. Add the following line to the end of the file:
export SSLKEYLOGFILE=/home/stanley/Documents/Coding/browser-log/sslkeylog.log
  1. Save the file and exit the text editor.

  2. All users who log in after you make this change will have the SSLKEYLOGFILE environment variable set to /home/stanley/Documents/Coding/browser-log/sslkeylog.log.

Method 2: Permanent Environment Variables for All Users (Using Environment File)

Permanent environment variables for all users can alternatively be set by adding them to the /etc/environment file. This file is also read by all users when they log in, so any changes made to the file will be reflected in the environment of all users.

  1. Open the /etc/environment file in a text editor with sudo:
sudo nano /etc/environment
  1. Add the following line to the end of the file:
export SSLKEYLOGFILE=/home/stanley/Documents/Coding/browser-log/sslkeylog.log
  1. Save the file and exit the text editor.
  2. All users who log in after you make this change will have the SSLKEYLOGFILE environment variable set to /home/stanley/Documents/Coding/browser-log/sslkeylog.log.

Verifying the Environment Variable

To verify that the environment variable has been set correctly, use the echo command followed by the variable name:

echo $SSLKEYLOGFILE

This command should display the specified path:

/home/stanley/Documents/Coding/browser-log/sslkeylog.log

This confirms that the environment variable has been set effectively.

Here are the logged keys below

Please note that Chrome should work with this setup out of the box but if it does not work, here are the steps to troubleshoot it.

Troubleshooting

Step 1: Open Firefox

Launch the Firefox browser on your computer. This will open the main Firefox window, ready for further configuration.

Step 2: Enter Configuration Settings

Type about:config into the Firefox address bar and press Enter. This will take you to the advanced configuration settings, where you can modify various aspects of Firefox's behavior.

More Resource

Step 3: Proceed with Caution

Firefox will display a warning message stating that you are about to enter the configuration settings. Click on "Accept the Risk and Continue." Be cautious while making changes in these settings, as it may affect browser behavior.

Step 4: Search for Security.fileuri.strict_origin_policy

In the search bar at the top of the page, type security.fileuri.strict_origin_policy. This setting determines whether Firefox enforces the same-origin policy for file:// URLs.

More Resource

Step 5: Toggle the Value

Double-click on the security.fileuri.strict_origin_policy entry to toggle its value. Set it to false to disable the strict origin policy for local files.

Step 6: Confirm Changes

After changing the value, close the about:config tab. Your changes are now saved.

Step 7: Restart Firefox

To apply the changes, restart Firefox. Close the browser and open it again. This will ensure that the new settings are in effect.

Chrome

Google Chrome also allows you to specify a log file when launching the browser. Here's how you can do it:

  1. Open the Command Prompt or Terminal:

    • On Linux, open the Terminal using your preferred method.
  2. Navigate to the Google Chrome Installation Directory: Use the cd command to navigate to the directory where Google Chrome is installed. This might vary based on your operating system.

    cd path\to\chrome
    
  3. Launch Chrome with Logging: Use the following command to launch Chrome with logging enabled. Replace /path/to/logfile.log with the desired path and filename for your log file.

    chrome --enable-logging --v=1 --log-file=/path/to/logfile.log
    

  4. Verify the Log File: After launching Chrome with the specified log file, check the log file for relevant information. You can use a text editor or command-line tools to view the contents of the log file.

    cat /path/to/logfile.log
    

Conclusion

To conclude, environment variables play a crucial role in Linux as they help in configuring system behavior and applications. This guide offers a detailed explanation of how to set and manage environment variables for individual users and system-wide configurations. Additionally, it includes troubleshooting tips for popular browsers. Having a good understanding of environment variables and utilizing them effectively enhances flexibility and customization options within the Linux operating system.


Written by sajerestan | I'm Stanley Sajere, a pro in InfoSec, Education, and Web Dev, passionate about innovation in these fields
Published by HackerNoon on 2023/11/22