Environment Variables in Linux
-
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.
- Open the /etc/profile file in a text editor:
nano /etc/profile
- Add the following line to the end of the file:
export SSLKEYLOGFILE=/home/stanley/Documents/Coding/browser-log/sslkeylog.log
- Save the file and exit the text editor.
- 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.- Open the /etc/environment file in a text editor:
nano /etc/environment
- Add the following line to the end of the file:
export SSLKEYLOGFILE=/home/stanley/Documents/Coding/browser-log/sslkeylog.log
- Save the file and exit the text editor.
- 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
-
To see current environment variables type the following command:
root@SONARR0724:~# printenv
Output example:
SHELL=/bin/bash LANGUAGE=en_US:en PWD=/root LOGNAME=root XDG_SESSION_TYPE=tty MOTD_SHOWN=pam HOME=/root LANG=en_US.UTF-8 SSH_CONNECTION=10.196.0.19 52023 10.73.0.178 22 XDG_SESSION_CLASS=user TERM=xterm-256color USER=root SHLVL=1 XDG_SESSION_ID=191 XDG_RUNTIME_DIR=/run/user/0 SSH_CLIENT=10.196.0.19 52023 22 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus SSH_TTY=/dev/pts/0 _=/usr/bin/printenv
To add a variable for this session only you can use "export"
root@SONARR0724:~# export NETBOX_HOST="https://netbox.rweigel.net" root@SONARR0724:~# export ZABBIX_HOST="https://zabbix.rweigel.net"
To perma add them systemwide you need to add them to "/etc/environment"
nano /etc/environment GNU nano 7.2 /etc/environment ZABBIX_HOST="https://zabbix.rweigel.net" NETBOX_HOST="https://netbox.rweigel.net"