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

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

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:

  •   $ printenv
    
  • Printing the value of a specific environment variable:

      $ printenv PATH
    
  • Printing the values of multiple environment variables:

      $ printenv USER HOME
    
  • Using grep to filter specific environment variables:

      $ printenv | grep -i term
    
  • Redirecting output to a file:

      $ printenv > environment_variables.txt
    

Viewing Command History (history):

    • Displaying command history:

         $ history
      
      • Limiting the number of displayed commands:

         $ history 10
        
      • Clearing command history:

         $ history -c
        
      • Searching command history:

         $ history | grep ssh
        
      • Using ! to execute a specific command from history:

         $ !100
        
  1. Saving Command History to a File (history):

    • Saving command history to a file:

        $ history > command_history.txt
      
    • Appending command history to an existing file:

        $ history >> command_history.txt
      
    • Filtering command history before saving:

        $ history | grep ssh > ssh_commands.txt
      
    • Excluding line numbers from command history:

        $ history -w command_history.txt
      
    • Exporting command history for use in another session:

        $ history -a
      
  2. Customizing Environment Profiles:

    • Editing the .bashrc file to set environment variables:

        $ nano ~/.bashrc
      
    • Adding a new environment variable:

        export MY_VAR="value"
      
    • Sourcing the updated .bashrc file to apply changes:

        $ source ~/.bashrc
      
    • Creating system-wide environment variables in /etc/environment:

        $ sudo nano /etc/environment
      
    • Reloading environment variables without rebooting:

        $ source /etc/environment
      
  3. Managing User-Specific Environment Profiles:

    • Editing the .bash_profile file for user-specific environment settings:

        $ nano ~/.bash_profile
      
    • Exporting environment variables in the .bash_profile file:

        export PATH="$PATH:/usr/local/bin"
      
    • Reloading the .bash_profile without logging out:

        $ source ~/.bash_profile
      
    • Creating 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.

Did you find this article valuable?

Support Vijay Kumar Singh by becoming a sponsor. Any amount is appreciated!