Skip to main content
In this guide, you’ll learn how to configure and maintain environment variables and login scripts that apply to all users on a Linux system. We’ll cover:
  • Understanding and viewing environment variables
  • Persisting system-wide variables via /etc/environment
  • Executing commands at login with /etc/profile.d/

Understanding Environment Variables

Environment variables are key–value pairs that shells and applications use to adjust behavior. You can list the current environment with either env or printenv.
To modify a variable for your current session, simply reassign it:
You can reference variables in scripts. For example, create a file in each user’s home directory:
This ensures the file lands in the right directory regardless of username.

Setting System-Wide Environment Variables

While individual users often tweak ~/.bashrc, you can enforce variables globally by editing /etc/environment:
Append lines in the format KEY="value":
Save and log out, then back in to apply changes:
The file /etc/environment only supports simple KEY="value" assignments. You cannot use shell expansions or commands here.

Running Commands at Login

To execute scripts for every user at login, place them in /etc/profile.d/. For example, to record the last login timestamp:
Add:
Scripts in /etc/profile.d/ are sourced by login shells automatically—you do not need a shebang (#!/bin/bash).
Avoid syntax errors in /etc/profile.d/ scripts. A malformed script can prevent users from logging in properly.

Further Reading & References


In the next article, we’ll explore advanced shell initialization techniques, including per-shell configuration and custom prompts.

Watch Video