Environment variable profile

Keywords: shell vim ssh CentOS

When the environment variable is written to the configuration file, it still takes effect after restarting the machine

1. Location of environmental variables

Following are the default configuration files for five types of storage environment variables

The environment variables stored in / etc/profile / etc/profile.d/*.sh and / etc/bashrc files are valid for all users.

Environment variables stored in ~/. bash_profile and ~/. bashrc are only valid for the current user (~denotes the home directory)

Note: After modifying the configuration file, you can use the "source filename" or ". filename" (note that there are spaces in the middle) command to ensure that the modification of the configuration file takes effect immediately.

2. The role of environment variable configuration files

The following is the call flow chart of the environment variables (if two identical environment variables are defined before and after, the variables defined after the process are of high priority, because the latter will cover the former; if they are represented in the way of variable superposition, they will not be covered, that is, they will still be of high priority).

Role of 2.1/etc/profile

Role of 2.2 ~/.bash_profile

Role of 2.3 ~/.bashrc

Role of 2.4/etc/bashrc

Look at the contents of the file first

[root@wenhaijin ~]# cat /etc/bashrc
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
        fi
        ;;
    screen)
        if [ -e /etc/sysconfig/bash-prompt-screen ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
        else
            PROMPT_COMMAND='printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
        fi
        ;;
    *)
        [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
        ;;
      esac
  fi
  # Turn on checkwinsize
  shopt -s checkwinsize
  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  # You might want to have e.g. tty in prompt (e.g. more virtual machines)
  # and console windows
  # If you want to do so, just add e.g.
  # if [ "$PS1" ]; then
  #   PS1="[\u@\h:\l \W]\\$ "
  # fi
  # to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell
    # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }

    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002
    else
       umask 022
    fi

    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null 2>&1
            fi
        fi
    done

    unset i
    unset pathmunge
fi
# vim:ts=4:sw=4
[root@wenhaijin ~]# 

Some of the variables defined in this file are already defined in / etc/profile, but the comments show that the variables defined here are valid for users who have not entered a password.

# We're not a login shell

# By default, we want umask to get set. This sets it for non-login shell.

3. Other configuration files and login information

3.1 Environment variable profile ~/bash_logout in effect at logout

[root@wenhaijin ~]# cat .bash_logoutĀ 
# ~/.bash_logout

3.2 Save file of historical command ~/bash_history

When there is a problem with the system, you can look at the file to see what is done.

3.3 shell login information

Local terminal welcome information can be customized through / etc/issue

3.4 Welcome Information for Remote Terminal

Remote terminal welcome information can be defined by / etc/issue.net

###View Remote Configuration Welcome Information File
[root@wenhaijin ~]# vim /etc/issue.net 
CentOS release 6.5 (Final)
Kernel \r on an \m

###Edit / etc/ssh/sshd_config file
[root@wenhaijin ~]# vim /etc/ssh/sshd_config 

# no default banner path
#Banner none
###Add remote profile welcome information and save exit
Banner /etc/issue.net

###Restart ssh service after modification
[root@wenhaijin ~]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[root@wenhaijin ~]# 

Re-log in remotely after the modification and find that the welcome message has been seen, but from the results found that the information represented by " r" and " m" has not been correctly identified, indicating that these two variables are only valid for local welcome information.


CentOS release 6.5 (Final)
Kernel \r on an \m

Last login: Thu Apr 13 21:50:30 2017 from 113.87.161.29

Welcome to aliyun Elastic Compute Service!

[root@wenhaijin ~]# 

3.5 Locally and remotely recognizable escape file information/etc/motd

However, / etc/issue and / etc/issue.net are welcome messages that can be seen before and after login, while / etc/motd is welcome messages that can be seen after login.

Posted by robbyc on Tue, 09 Jul 2019 12:50:08 -0700