Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Environment Variables and Config Files | Configuration, Networking, and DevOps Essentials
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Linux for DevOps Engineer

bookEnvironment Variables and Config Files

How Environment Variables Work

Environment variables are dynamic values stored by your operating system that can influence how processes and applications behave. You use them to configure applications without changing the application code itself. This allows you to manage configuration details like database connections, API keys, and environment-specific settings in a flexible, secure way.

Real-World Example: Database Configuration

Suppose you have a web application that connects to a database. Instead of hardcoding the database username, password, and host in your source code, you define them as environment variables:

  • DB_HOST for the database server address;
  • DB_USER for the username;
  • DB_PASS for the password.

This approach allows you to deploy the same application code to different environments (development, testing, production) by simply changing the environment variable values.

Setting Environment Variables in Linux Shell

You can set environment variables temporarily for a session or permanently for all future sessions.

Temporarily (current shell only):

export DB_HOST="localhost"
export DB_USER="admin"
export DB_PASS="securepassword"

These variables will be available to any command you run in the current terminal session.

Permanently (all sessions):

Add the export statements to your shell profile file (such as ~/.bashrc or ~/.bash_profile):

echo 'export DB_HOST="localhost"' >> ~/.bashrc
echo 'export DB_USER="admin"' >> ~/.bashrc
echo 'export DB_PASS="securepassword"' >> ~/.bashrc

Reload the profile with:

source ~/.bashrc

Accessing Environment Variables in Applications

Applications can read environment variables using language-specific methods. For example, in a Python application:

import os

db_host = os.environ.get("DB_HOST")
db_user = os.environ.get("DB_USER")
db_pass = os.environ.get("DB_PASS")

This code retrieves the values of the environment variables and uses them to connect to the database. You do not need to modify the application code to change database credentials; you only update the environment variables.

question mark

What is the primary purpose of environment variables in Linux-based systems?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain how to unset or remove an environment variable?

What are some best practices for managing sensitive information with environment variables?

How do I access environment variables in other programming languages?

bookEnvironment Variables and Config Files

Scorri per mostrare il menu

How Environment Variables Work

Environment variables are dynamic values stored by your operating system that can influence how processes and applications behave. You use them to configure applications without changing the application code itself. This allows you to manage configuration details like database connections, API keys, and environment-specific settings in a flexible, secure way.

Real-World Example: Database Configuration

Suppose you have a web application that connects to a database. Instead of hardcoding the database username, password, and host in your source code, you define them as environment variables:

  • DB_HOST for the database server address;
  • DB_USER for the username;
  • DB_PASS for the password.

This approach allows you to deploy the same application code to different environments (development, testing, production) by simply changing the environment variable values.

Setting Environment Variables in Linux Shell

You can set environment variables temporarily for a session or permanently for all future sessions.

Temporarily (current shell only):

export DB_HOST="localhost"
export DB_USER="admin"
export DB_PASS="securepassword"

These variables will be available to any command you run in the current terminal session.

Permanently (all sessions):

Add the export statements to your shell profile file (such as ~/.bashrc or ~/.bash_profile):

echo 'export DB_HOST="localhost"' >> ~/.bashrc
echo 'export DB_USER="admin"' >> ~/.bashrc
echo 'export DB_PASS="securepassword"' >> ~/.bashrc

Reload the profile with:

source ~/.bashrc

Accessing Environment Variables in Applications

Applications can read environment variables using language-specific methods. For example, in a Python application:

import os

db_host = os.environ.get("DB_HOST")
db_user = os.environ.get("DB_USER")
db_pass = os.environ.get("DB_PASS")

This code retrieves the values of the environment variables and uses them to connect to the database. You do not need to modify the application code to change database credentials; you only update the environment variables.

question mark

What is the primary purpose of environment variables in Linux-based systems?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1
some-alt