[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
# 添加路径[root@localhost ~]# export PATH="$PATH:/opt/pgsql-15.3/bin"# 路径生效[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]# postgres --version
postgres (PostgreSQL)15.3# 这种修改方式只对当前用户有效[root@localhost ~]# su - user[user@localhost ~]$ postgres --version
bash: postgres:command not found
# 这种修改方式只对当前会话有效,也就是说每当登出或注销系统以后,PATH设置就会失效[usert@localhost ~]$ su - root
password:
[root@localhost ~]# postgres --version
bash: postgres:command not found
总结:这种修改方式是仅限于当前会话的,当前会话登出或注销,对$PATH的修改也就失效了
2. 在路径文件/root/.bash_profile中添加路径
[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
# 添加路径[root@localhost ~]# cat /root/.bash_profile# .bash_profile# Get the aliases and functionsif[ -f ~/.bashrc ];then. ~/.bashrc
fi# User specific environment and startup programsPATH=$PATH:$HOME/bin # 在这一行添加新的路径exportPATH[root@localhost ~]# [root@localhost ~]# [root@localhost ~]# vim /root/.bash_profile# 修改完后执行cat [root@localhost ~]# cat /root/.bash_profile# .bash_profile# Get the aliases and functionsif[ -f ~/.bashrc ];then. ~/.bashrc
fi# User specific environment and startup programsPATH=$PATH:$HOME/bin:/opt/pgsql-15.3/bin # 在这一行添加了新的路径exportPATH[root@localhost ~]# [root@localhost ~]# [root@localhost ~]# # 当前会话路径生效[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]# postgres --version
postgres (PostgreSQL)15.3# 这种修改方式只对root用户生效,普通用户无效[root@localhost ~]# su - user[user@localhost ~]$ echo$PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
# 这种修改方式的改变是永久的[usert@localhost ~]$ su - root
password:
[root@localhost ~]# postgres --version
postgres (PostgreSQL)15.3
总结:这种修改方式仅仅对当前用户root的所有会话永久有效,切换系统用户后,无效
3. 在路径文件/etc/profile中添加路径
[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[root@localhost ~]# vim /etc/profile[root@localhost ~]# 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 this# will prevent the need for merging in future updates.pathmunge(){case":${PATH}:"in
*:"$1":*);;
*)if["$2"="after"];thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac}if[ -x /usr/bin/id ];thenif[ -z "$EUID"];then# ksh workaroundEUID=`/usr/bin/id -u`UID=`/usr/bin/id -ru`fiUSER="`/usr/bin/id -un`"LOGNAME=$USERMAIL="/var/spool/mail/$USER"fi# Path manipulationif["$EUID"="0"];then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fiHOSTNAME=`/usr/bin/hostname 2>/dev/null`HISTSIZE=1000if["$HISTCONTROL"="ignorespace"];thenexportHISTCONTROL=ignoreboth
elseexportHISTCONTROL=ignoredups
fiexportPATHUSERLOGNAME MAIL HOSTNAMEHISTSIZEHISTCONTROLexportPATH=$PATH:/opt/pgsql-15.3/bin # 在这一行添加了新的路径# 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 fileif[$UID -gt 199]&&["`/usr/bin/id -gn`"="`/usr/bin/id -un`"];thenumask 002
elseumask 022
fiforiin /etc/profile.d/*.sh /etc/profile.d/sh.local ;doif[ -r "$i"];thenif["${-#*i}"!="$-"];then."$i"else."$i">/dev/null
fifidoneunset i
unset -f pathmunge
[root@localhost ~]# source /etc/profile[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]# postgres --version
postgres (PostgreSQL)15.3# 切换用户后仍然生效[root@localhost ~]# su - userc
上一次登录:五 6月 914:09:51 CST 2023pts/0 上
[userc@localhost ~]$ postgres --version
postgres (PostgreSQL)15.3