内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档详细介绍用户级和系统级环境变量的
风哥提示:
配置方法,包括配置文件位置、生效方式、优先级等。
Part01-用户级环境变量配置
1.1 用户级配置文件
$ ls -la ~ | grep bash
-rw——-. 1 user user 1234 Apr 3 13:30:00 .bash_history
-rw-r–r–. 1 user user 234 Jan 1 00:00:00 .bash_logout
-rw-r–r–. 1 user user 567 Jan 1 00:00:00 .bash_profile
-rw-r–r–. 1 user user 3456 Jan 1 00:00:00 .bashrc
# 查看各配置文件内容
$ cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
$ cat ~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
alias ll=’ls -la’
alias la=’ls -A’
alias l=’ls -CF’
$ cat ~/.bash_logout
# ~/.bash_logout: executed by bash(1) when login shell exits.
# when leaving the console clear the screen to increase privacy
if [ “$SHLVL” = 1 ]; then
[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
1.2 配置用户级环境变量
$ vi ~/.bashrc
# 在.bashrc文件末尾添加以下内容
# User Environment Variables
export EDITOR=vim
export VISUAL=vim
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTTIMEFORMAT=”%F %T ”
# Custom PATH
export PATH=$PATH:$HOME/bin:$HOME/.local/bin
# 重新加载.bashrc文件
$ source ~/.bashrc
# 验证环境变量
$ echo $EDITOR
vim
$ echo $VISUAL
vim
$ echo $HISTSIZE
10000
$ echo $HISTTIMEFORMAT
%F %T
# 编辑.bash_profile文件添加环境变量
$ vi ~/.bash_profile
# 在.bash_profile文件末尾添加以下内容
# User Environment Variables (Login Shell)
export USER_VAR=”user_value”
export APP_HOME=/opt/myapp
export PATH=$PATH:$APP_HOME/bin
# 重新登录验证
$ exit
$ ssh user@localhost
$ echo $USER_VAR
user_value
$ echo $APP_HOME
/opt/myapp
Part02-系统级环境变量配置
2.1 系统级配置文件
$ ls -la /etc/profile*
-rw-r–r–. 1 root root 1234 Jan 1 00:00:00 /etc/profile
-rw-r–r–. 1 root root 567 Jan 1 00:00:00 /etc/profile.d/
# 查看/etc/profile文件内容
$ cat /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# 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 such
# changes will be preserved when upgrading.
# Prevent double sourcing in case profile is invoked directly
if [ -n “$BASH_VERSION” ]; then
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
fi
# Path manipulation
if [ “$EUID” = “0” ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=$(/usr/bin/hostnamectl –static 2>/dev/null)
HISTSIZE=1000
if [ “$HISTCONTROL” = “ignorespace” ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for 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 ] && [ “`/usr/bin/id -gn`” = “`/usr/bin/id -un`” ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r “$i” ]; then
if [ “$PS1” ]; then
. “$i”
else
. “$i” >/dev/null
fi
fi
done
unset i
unset -f pathmunge
# 查看profile.d目录下的配置文件
$ ls /etc/profile.d/
colorgrep.csh
colorgrep.sh
colorls.csh
colorls.sh
less.csh
less.sh
vim.csh
vim.sh
2.2 配置系统级环境变量
$ sudo vi /etc/profile.d/custom.sh
# 添加以下内容
# System-wide Environment Variables
export SYSTEM_EDITOR=vim
export SYSTEM_VISUAL=vim
export SYSTEM_HISTSIZE=5000
# System-wide PATH
export PATH=$PATH:/opt/system/bin
# 重新加载系统配置
$ source /etc/profile
# 验证系统级环境变量
$ echo $SYSTEM_EDITOR
vim
$ echo $SYSTEM_HISTSIZE
5000
# 切换到其他用户验证
$ su – user2
$ echo $SYSTEM_EDITOR
vim
$ echo $SYSTEM_HISTSIZE
5000
# 所有用户都能看到系统级环境变量
Part03-环境变量优先级
3.1 环境变量加载顺序
# 1. /etc/profile(系统级)
# 2. /etc/profile.d/*.sh(系统级)
# 3. ~/.bash_profile(用户级登录shell)
# 4. ~/.bashrc(用户级交互shell)
# 5. 临时设置的环境变量(当前shell会话)
# 测试环境变量优先级
# 在/etc/profile.d/中设置
$ sudo sh -c ‘echo “export TEST_VAR=system_level” > /etc/profile.d/test.sh’
# 在~/.bashrc中设置
$ echo ‘export TEST_VAR=user_level_bashrc’ >> ~/.bashrc
# 在~/.bash_profile中设置
$ echo ‘export TEST_VAR=user_level_profile’ >> ~/.bash_profile
# 临时设置
$ export TEST_VAR=temp_level
# 查看当前值
$ echo $TEST_VAR
temp_level
# 新开终端查看值
$ bash
$ echo $TEST_VAR
user_level_bashrc
# 重新登录查看值
$ exit
$ ssh user@localhost
$ echo $TEST_VAR
user_level_profile
# 结论:临时 > bashrc > bash_profile > profile.d > profile
Part04-环境变量生效方式
4.1 使环境变量生效
$ source ~/.bashrc
# 方式2:使用.命令重新加载配置文件
$ . ~/.bashrc
# 方式3:重新登录
$ exit
$ ssh user@localhost
# 方式4:新开终端
# 在图形界面中打开新终端
# 验证环境变量已生效
$ echo $EDITOR
vim
# 查看所有环境变量
$ env | grep EDITOR
EDITOR=vim
Part05-实战案例
5.1 配置Python开发环境
$ sudo dnf install -y python3 python3-pip
# 查找Python安装路径
$ which python3
/usr/bin/python3
$ which pip3
/usr/bin/pip3
# 配置Python环境变量
$ cat >> ~/.bashrc << 'EOF'
# Python Environment
export PYTHON_HOME=/usr
export PATH=$PATH:$PYTHON_HOME/bin
export PYTHONPATH=$PYTHONPATH:$HOME/python/lib
export PIP_CONFIG_FILE=$HOME/.pip/pip.conf
EOF
# 创建pip配置目录
$ mkdir -p ~/.pip
# 创建pip配置文件
$ cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF
# 重新加载配置
$ source ~/.bashrc
# 验证Python环境
$ echo $PYTHON_HOME
/usr
$ echo $PYTHONPATH
:/home/user/python/lib
$ python3 --version
Python 3.9.更多视频教程www.fgedu.net.cn7
$ pip3 --version
pip 21.2.3 from /usr/lib/python3.9/site-packages/pip (python 3.9)
1. 用户级环境变量配置在~/.bashrc或~/.bash_profile
2. 系统级环境变量配置在/etc/profile.d/目录
3. 修改配置文件后使用source命令重新加载
4. 环境变量名通常使用大写字母
5. 注意环境变量的优先级和加载顺序
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
