3.2 Mastering System-Wide Environment Profiles on Linux: A Comprehensive Guide

I'm Vijay Kumar Singh, a Linux, DevOps, Cloud enthusiast learner and contributor in shell scripting, Python, networking, Kubernetes, Terraform, Ansible, Jenkins, and cloud (Azure, GCP, AWS) and basics of IT world. 💻✨ Constantly exploring innovative IT technologies, sharing insights, and learning from the incredible Hashnode community. 🌟 On a mission to build robust solutions and make a positive impact in the tech world. 🚀 Let's connect and grow together!
#PowerToCloud
Introduction:
In the dynamic landscape of Linux system administration, managing system-wide environment profiles is a fundamental task. From setting global environment variables to monitoring command history, understanding how to effectively manage these profiles is essential for maintaining system integrity and optimizing workflow. In this detailed blog post, we'll explore the commands printenv and history, providing insights and examples on how to master system-wide environment profiles on Linux.
Printing Environment Variables (printenv):
Printing all environment variables:
$ printenvPrinting the value of a specific environment variable:
$ printenv PATHPrinting the values of multiple environment variables:
$ printenv USER HOMEUsing
grepto filter specific environment variables:$ printenv | grep -i termRedirecting output to a file:
$ printenv > environment_variables.txt
Viewing Command History (history):
Displaying command history:
$ historyLimiting the number of displayed commands:
$ history 10Clearing command history:
$ history -cSearching command history:
$ history | grep sshUsing
!to execute a specific command from history:$ !100
Saving Command History to a File (
history):Saving command history to a file:
$ history > command_history.txtAppending command history to an existing file:
$ history >> command_history.txtFiltering command history before saving:
$ history | grep ssh > ssh_commands.txtExcluding line numbers from command history:
$ history -w command_history.txtExporting command history for use in another session:
$ history -a
Customizing Environment Profiles:
Editing the
.bashrcfile to set environment variables:$ nano ~/.bashrcAdding a new environment variable:
export MY_VAR="value"Sourcing the updated
.bashrcfile to apply changes:$ source ~/.bashrcCreating system-wide environment variables in
/etc/environment:$ sudo nano /etc/environmentReloading environment variables without rebooting:
$ source /etc/environment
Managing User-Specific Environment Profiles:
Editing the
.bash_profilefile for user-specific environment settings:$ nano ~/.bash_profileExporting environment variables in the
.bash_profilefile:export PATH="$PATH:/usr/local/bin"Reloading the
.bash_profilewithout logging out:$ source ~/.bash_profileCreating aliases for commonly used commands in
.bashrc:alias ll='ls -alF'
Go through these cmd's to get grep on managing environment variables and history.
#Users current evnvironment
printenv
#or
env
PATH=/home/aaron/.local/bin:/home/aaron/bin:/usr/local/bin:/usr/local/sbin
:/usr/bin:/usr/sbin
HISTSIZE=1000
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
SESSION_MANAGER=local/unix:@/tmp/.ICE-unix/2260,unix/unix:/tmp/.ICE-
unix/2260
#Changing Env Variable HISTSIZE value
#to persist after reboot edit in .bashrc file
HISTSIZE=2000
#Add custom environment variables
cat .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
#Adding env var for all users
sudo vi /etc/environment
#Create a template for the users' environment
#Example:Inform all new users about some default policy
#Create ReadMe in /etc/skel dir
sudo vim /etc/skel/README
Please don't run CPU-intensive
processes between 8AM and 10PM.
#Adding a Env Variable just for a specific User
sudo vim /home/trinity/.bashrc
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
#add /opt/bin to path
PATH="$HOME/.local/bin:$HOME/bin:/opt/bin:$PATH"
echo $PATH
/home/trinity/.local/bin:/home/trinity/bin:/usr/local/bin:/usr/bin:/usr
/local/sbin:/usr/sbin
Conclusion:
Effective management of system-wide environment profiles is crucial for optimizing workflows and ensuring system stability on Linux. By mastering commands like printenv and history, along with various customization techniques, administrators can gain greater control over environment variables and command histories. Experiment with these commands and examples in your Linux environment to streamline your system administration tasks and enhance productivity.






