Linux下环境变量的配置

  最近一直在Linux下调试MQ,研究了一下Linux环境变量的配置,部分资料搜集自网上:

环境变量的配置文件

         一般来说,许多Linux发行版的默认的Shell是Bash (GNU Bourne-Again Shell) ,Shell中变量根据作用域的不同(与大多数编程语言类似,比如在函数中定义的变量在函数外不能使用的),分为局部变量和环境变量(全局变量)。局部变量是普通的变量,仅在创建它的Shell中有效。环境变量。我们更多的使用“环境变量”而不是“全局变量”,因为这个名称更能体现它的特点。环境变量对创建它的Shell及其派生出来的子进程都有效。或者说环境变量可以继承,但它对其它与创建它的Shell没有关系的Shell并不可见。其实重启一个终端就是重新启动了一个shell。

         Linux的变量可分为两类:环境变量和本地变量。环境变量,或者称为全局变量,存在与所有的shell中,在你登陆系统的时候就已经有了相应的系统定义的环境变量了。Linux的环境变量具有继承性,即子shell会继承父shell的环境变量。#env 显示环境变量

         本地变量,当前shell中的变量,很显然本地变量中肯定包含环境变量。Linux的本地变量的非环境变量不具备继承性。#set显示本地变量

以上参考


       对不同版本的Linux,其配置环境变量的文件可能有/etc/environment/etc/profile/etc/bashrc~/.profile~.bash_profile~/.bashrc~/.bash_logout ……

配置文件的说明

/etc/environment:是整个系统的环境,启动系统后就会去读取该文件,一般不需要改

/etc/profile:是系统的每个用户的环境信息,当用户第一次登录时,该文件被执行;并从/etc/profile.d目录的配置文件中搜集shell的设置。

英文描述为:
# /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.
所以如果你有对/etc/profile有修改的话必须得重启你的修改才会生效,此修改对每个用户都生效。

/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bashshell被打开时,该文件被读取.
英文描述为:
# /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.
如果你想对所有的使用bash的用户修改某个配置并在以后打开的bash都生效的话可以修改这个文件,修改这个文件不用重启,重新打开一个bash即可生效。

~/.bash_profile
:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.
此文件类似于/etc/profile,也是需要需要重启才会生效,/etc/profile对所有用户生效,~/.bash_profile只对当前用户生效。

~/.bashrc
:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取.(每个用户都有一个.bashrc文件,在用户目录下)
此文件类似于/etc/bashrc,不需要重启生效,重新打开一个bash即可生效, /etc/bashrc对所有用户新打开的bash都生效,但~/.bashrc只对当前用户新打开的bash生效。

~/.bash_logout
:当每次退出系统(退出bashshell)时,执行该文件.

另外:

/etc/profile、/etc/bashrc 是系统全局环境变量设定 ;

~/.profile,~/.bashrc是用户目录下的私有环境变量设定;

/etc/profile中设定的变量(全局)的可以作用于任何用户;而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系。

~/.bash_profile 是交互式、login 方式进入bash运行的;
~/.bashrc 是交互式 non-login 方式进入bash运行的;通常二者设置大致相同,所以通常前者会调用后者。


配置文件的加载顺序

       首先需要了解交互式shell和非交互式shell、登录shell和非登录shell的区别,这是按两个不同的维度来划分的,一个是是否交互式,另一个是是否登录。(这里参考

交互式shell和非交互式shell(interactiveshell and non-interactive shell)
交互式模式就是在终端上执行,shell等待你的输入,并且立即执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、退出。当你退出后,shell也终止了。
shell
也可以运行在另外一种模式:非交互式模式,以shell script(非交互)方式执行。在这种模式下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾EOFshell也就终止了。
可以通过打印“$-”变量的值(代表着当前shell的选项标志),查看其中的“i”选项(表示interactive shell)来区分交互式与非交互式shell

登录shell和非登录shell
登录shell:是需要用户名、密码登录后才能进入的shell(或者通过”–login”选项生成的shell),也就是说最终会调用login命令的操作都可称之为login shell非登录shell:当然就不需要输入用户名和密码即可打开的Shell,例如:直接命令“bash”就是打开一个新的非登录shell,在GnomeKDE中打开一个终端terminal)窗口程序也是一个非登录shell,用户在图形界面启动一个terminal,或者执行/bin/bash/usr/bin/bash都属于non-login shell
执行exit命令,退出一个shell(登录或非登录shell);
执行logout命令,退出登录shell(不能退出非登录shell)。可以通过打印“$0”变量的值查看来区分。

 login shell在某用户由/bin/login登陆进系统后启动的shell,跟这个用户绑定。这个shell是用户登陆后启动的第一个进程。login进程在启动shell时传递第0个參数指明shell的名字,该參数第一个字符为"-",指明这是一个login shell比方对bash而言,启动參数为"-bash"。当bashlogin shell启动时,它会运行/etc/profile中的命令,然后/etc/profile调用/etc/profile.d文件夹下的全部脚本。然后运行~/.bash_profile~/.bash_profile调用~/.bashrc,最后~/.bashrc又调用/etc/bashrc要识别一个shell是否为login shell。仅仅需在该shell下运行echo $0:# echo $0。假设输出为该shell名字,加上一个'-'前缀。则说明该shelllogin shell比如-bash-su等等。


加载顺序:

系统启动时先加载/etc/environment,后加载/etc/profile

/etc/environment是设置整个系统的环境,而/etc/profile是设置所有用户的环境,前者与登录用户无关,后者与登录用户有关。系统应用程序的执行与用户环境可以是无关的,但与系统环境是相关的,所以当你登录时,你看到的提示信息,象日期、时间信息的显示格式与系统环境的LANG是相关的,缺省LANG=en_US,如果系统环境LANG=zh_CN,则提示信息是中文的,否则是英文的。如果同一个变量在/etc/profile和系/etc/environment有不同的值以/etc/profile为准。

对于Bash来说,登录shell(包括交互式登录shell和使用“–login”选项的非交互shell),它会首先读取和执行/etc/profile全局配置文件中的命令,然后依次查找~/.bash_profile、~/.bash_login 和 ~/.profile这三个配置文件,读取和执行这三个中的第一个存在且可读的文件中命令。除非被“–noprofile”选项禁止了。
在非登录shell里,只读取 ~/.bashrc (和 /etc/bash.bashrc、/etc/bashrc )文件,不同的发行版里面可能有所不同
,如RHEL6.3中非登录shell仅执行了“~/.bashrc”文件(没有执行/etc/bashrc),而KUbuntu10.04中却依次执行了/etc/bash.bashrc和 ~/.bashrc 文件。对于这些规则,可以直接在相应的配置文件中加一些echo命令来验证其真实性。

  

对于login shell初始化脚本加载顺序如下:

1./etc/profile/etc/profile.d/*

2. ~/.bash_profile~/.bashrc/etc/bashrc或者/etc/bash.bashrc
3. 
~/.bash_login
4. 
~/.profile
同一行内多个脚本是表示后面的脚本被包含在前面的脚本中得到加载。 
/etc/profile
是必须要执行的(1),然后后面3个(234),按照顺序谁存在就执行谁,然后后面的就不会再执行。


对于non-login shell初始化脚本加载顺序如下:

1. ~/.bashrc/etc/bashrc或者/etc/bash.bashrc/etc/profile.d/*
 
退出shell时去执行~/.bash_logout

对于~/.bashrc,是在non login shell启动时执行,也就意味着在图形界面每开启一次terminal,就会读取一次该文件。


       

配置环境变量的方法

设置环境变量:$export NAME="HELLOWORLD"  (临时变量,重启系统将失效)

显示环境变量:$echo $NAME

env 命令查看当前用户的环境变量


以添加mongodb server为列

修改方法一:

通过修改export PATH=$PATH:新路径

如:export PATH=$PATH:/usr/local/mongodb/bin
//配置完后可以通过echo $PATH查看配置结果。
生效方法:立即生效
有效期限:临时改变,只能在当前的终端窗口中有效,当前窗口关闭后就会恢复原有的path配置
用户局限:仅对当前用户

 

修改方法二:
通过修改~/.bashrc~/.bash_profile~/.profile文件:
如:vim ~/.bashrc
//在最后一行添上:
export PATH=/usr/local/mongodb/bin:$PATH
生效方法:(有以下两种)
1、关闭当前终端窗口,重新打开一个新终端窗口就能生效
2、输入“source ~/.bashrc”命令,立即生效
有效期限:永久有效
用户局限:仅对当前用户

 

修改方法三:
通过修改/etc/profile/etc/bashrc文件:
如:vim /etc/profile
/export PATH //找到设置PATH的行,添加
export PATH=/usr/local/mongodb/bin:$PATH
生效方法:系统重启
有效期限:永久有效
用户局限:对所有用户

 

修改方法四:不推荐
通过修改/etc/environment文件:
vim /etc/environment
在PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"中加入“:/usr/local/mongodb/bin”
生效方法:系统重启
有效期限:永久有效
用户局限:对所有用户


             



ubuntu 12.04中环境变量设置


Persistent environment variables

So far we've only discussed ways set an environment variable value temporarily until the shell session in which it was set is closed. One may wonder if there is a way to somehow permanently set an environment variable to a certain value.

Note: The shell config files mentioned below are only meant to be read by particular shells and desktop environments and should not be used.

 

Session-wide environment variables

Environment variable settings that should affect just a particular user (rather than the system as a whole) should be set into:

  • ~/.pam_environment - This file is specifically meant for setting a user's environment. It is not a script file, but rather consists of assignment expressions, one per line.

 

PATH DEFAULT=${PATH}:${HOME}/MyPrograms

Note: Using .pam_environment requires a re-login in order to initialize the variables. Restarting just the terminal is not sufficient to be able to use the variables.

If you are using KDE, see the KDE User-base page on this topic.

Not recommended anymore:

  • ~/.profile - In this file you can also place environment variable assignments, since it gets executed automatically by the DisplayManager during the start-up process desktop session as well as by the login shell when one logs-in from the textual console.

  • ~/.bash_profile or ~/.bash_login - If one of these file exist, bash executes it rather than "~/.profile" when it is started as a login shell. (Bash will prefer "~/.bash_profile" to "~/.bash_login"). However, these files won't influence a graphical session by default.

  • ~/.bashrc - Because of the way Ubuntu currently sets up the various script files by default, this may be the easiest place to set variables in. The default configuration nearly guarantees that this file will be executed in each and every invocation of bash as well as while logging in to the graphical environment. The performance cost of this will be negligible; the overhead of forking and execing bash will massively dominate the small cost of setting a handful of variables.

 

System-wide environment variables

Environment variable settings that affect the system as a whole (rather than just a particular user) should not be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded, but into

  • /etc/environment - This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

Not recommended:

  • /etc/profile - This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more than invoke the /etc/bash.bashrc file.

  • /etc/bash.bashrc - This is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.

Note: Any variables added to these locations will not be reflected when invoking them with a sudo command, as sudo has a default policy of resetting the Environment and setting a secure path (this behavior is defined in /etc/sudoers). As a workaround, you can use "sudo su" that will provide a shell with root privileges but retaining any modified PATH variables.

Note: When dealing with end-user/home desktop systems may be appropriate to place settings in the user's ~/.pam_environment files discussed above rather than the system-wide ones, since those files do not require one to utilize root privileges in order to edit and are easily moved between systems.

Note: Some systems now use an envvar.sh placed in the /etc/profile.d/ directory to set system wide environment strings.


Linux 下Shell变量,环境变量的联系与区别

Linux下Shell变量,环境变量的联系与区别


1、  简介

linux下的变量可分成两种:Shell变量和环境变量。

Shell变量,又称本地变量,包括私有变量以及用户变量,不同类的shell有不同的私有变量,bash,ksh,csh等,每种shell的私有变量都不一样

 

环境变量,又称用户变量,与shell无关

 

常规定义变量的方法:变量名=变量值

注意:

1.变量名和=号之间不能有空格,否则shell会把变量名当成命令

2.变量名称一般使用大写字母

 

2、  常用的相关命令

env命令:显示当前用户的环境变量

set命令:不加参数,显示当前shell所有本地设置的Shell变量,否则设置shell变量

unset命令:unset为shell内建指令,删除变量或函数。

exprot命令:显示(设置)当前导出成用户变量的shell变量(注意:export为bash或类bash私有的命令)

echo $变量名:查看变量值(其中,$变量:引用变量的意思)

 

3、  示例

a)  使用env命令显示当前用户的所有环境变量

[laiyu@localhost ~]$ env

HOSTNAME=localhost.localdomain

SHELL=/bin/bash

TERM=xterm

HISTSIZE=1000

USER=laiyu

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;...

MAIL=/var/spool/mail/laiyu

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/laiyu/bin

PWD=/home/laiyu

LANG=en_US.UTF-8

...

HOME=/home/laiyu

LOGNAME=laiyu

...

_=/bin/env

 

b)  使用set命令显示当前shell所有本地Shell变量

[root@localhost ~]# set | more

BASH=/bin/bash

...

BASH_CMDS=()

...

 

c)  使用unset命令来清除变量(不管是shell变量还是环境变量)

[laiyu@localhost ~]$ export TEST=test #增加一个环境变量TEST

[laiyu@localhost ~]$ env | grep TEST #查看增加的变量

TEST=test

[laiyu@localhost ~]$ unset $TEST #删除环境变量TEST(这里TEST前有无美元符都一样)

[laiyu@localhost ~]$ env | grep TEST #输出为空

注意:unset只能删除非只读变量

 

d)  使用echo命令显示环境变量(HOME变量为例)

[root@localhost ~]# echo $HOME

/root

 

e)  使用export命令将shell变量导出为用户变量,并显示

列出所有的Shell赋予程序的环境变量

[root@localhost ~]# export -p #等同export

...

declare -x HOME="/root"

declare -x HOSTNAME="localhost.localdomain"

declare -x LANG="en_US.UTF-8"

declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"

declare -x LOGNAME="root"

declare -xPATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/bin/:/usr/local/tsung/bin"

...

 

把shell变量导出为用户变量

[root@localhost ~]# SHOUKE=ishouke  #定义shell变量

[root@localhost ~]# echo $SHOUKE 

ishouke

#查看当前用户的用户变量是否存在SHOUKE变量,结果是不存在

[root@localhost ~]# env | grep SHOUKE

#查看当前shell的本地变量是否存在SHOUKE变量,结果是不存在

[root@localhost ~]# set | grep SHOUKE

SHOUKE=ishouke

#查看Shell赋予程序的环境变量中是否存在SHOUKE变量,结果不存在

[root@localhost ~]# export | grep SHOUKE

 

#导出shell变量为用户变量

[root@localhost ~]# export SHOUKE

[root@localhost ~]# env | grep SHOUKE

SHOUKE=ishouke

[root@localhost ~]#

--------------------------------------

也可以如下方式使用export

[root@localhost ~]# export SHOUKE=ishouke

[root@localhost ~]# env | grep SHOUKE

SHOUKE=ishouke

[root@localhost ~]#

 

注意:以上的设置仅在当前打开的shell窗口或或其子shell窗口中起作用,临时的,关闭当前shell窗口或重启机器,则刚才的设置失效,,或打开一个新窗口,设置在新窗口中不起作用。


参考:

https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell

http://liuzhijun.iteye.com/blog/1744465

https://www.freebsd.org/cgi/man.cgi?query=bash&apropos=0&sektion=0

https://www.cnblogs.com/zhaofeng555/p/4895517.html


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值