systemctl-set-environment
Set system-wide environment variables
TLDR
Set a single environment variable
Set multiple environment variables at once
Set an environment variable for the user service manager
SYNOPSIS
systemctl set-environment VARIABLE=VALUE [VARIABLE=VALUE...]
PARAMETERS
VARIABLE=VALUE
One or more environment variable name-value pairs to set. The variable name is case-sensitive, and the value will be assigned directly. Multiple pairs can be specified, separated by spaces.
--user
Operate on the per-user service manager instance, rather than the system-wide instance. This sets environment variables for the current user's systemd services only.
--system
Operate on the system-wide service manager instance (this is the default behavior).
DESCRIPTION
The systemctl set-environment command is used to persistently set environment variables within the systemd manager's environment block. These variables become available to all services and programs subsequently started by that specific systemd manager instance (either the system-wide manager or a user-specific manager).
Unlike setting variables in a shell with export, which only affects the current shell and its child processes, set-environment provides a system-wide or user-wide persistent mechanism that survives reboots. It's particularly useful for global configurations, paths (e.g., JAVA_HOME), or other settings that many services might rely on. For service-specific environment variables, it's generally recommended to use the Environment= or EnvironmentFile= directives within the service unit file itself, as these take precedence over variables set via set-environment for that particular unit.
CAVEATS
Global Scope: Variables set via set-environment affect all services started by the respective systemd manager. Care must be taken to avoid unintended side effects or conflicts.
Precedence: Environment variables defined directly in a service unit file (using Environment= or EnvironmentFile=) will override variables set via set-environment for that specific service.
Security: Avoid storing sensitive information (like passwords or API keys) as global environment variables, as they can be widely accessible to all processes under the systemd manager.
Existing Services: Services already running will not automatically pick up new environment variables; they must be restarted to apply the changes.
Shell Impact: This command does not modify the environment of your current shell session. It only affects processes started by systemd.
PERSISTENCE ACROSS REBOOTS
Environment variables set using systemctl set-environment are stored persistently by the systemd manager and will be restored automatically after system reboots, ensuring that the desired environment is consistently available for all systemd-managed processes.
ENVIRONMENT VARIABLE PRECEDENCE
Systemd processes evaluate environment variables in a specific order of precedence, from lowest to highest:
1. Variables from /etc/environment and /etc/default/*.
2. Variables from environment.d snippets.
3. Variables set via systemctl set-environment.
4. Variables set directly in unit files via Environment= or EnvironmentFile= (these have the highest precedence for a specific unit).
HISTORY
The systemctl set-environment command is part of the systemd init system, which has become the de facto standard init system for many Linux distributions since its adoption in the early 2010s. It provides a more dynamic and persistent way to manage global environment variables compared to traditional methods like editing /etc/environment, offering greater flexibility for system administrators and developers.


