服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - Linux中环境变量配置的步骤详解

Linux中环境变量配置的步骤详解

2022-02-10 16:48Myths Linux

Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的环境变量。所以下面这篇文章主要给大家介绍了关于Linux中环境变量配置的相

简介

我们大家在平时使用linux的时候,经常需要配置一些环境变量,这时候一般都是网上随便搜搜就有人介绍经验的。不过问题在于他们的方法各不相同,有人说配置在/etc/profile里,有人说配置在/etc/environment,有人说配置在~/.bash_profile里,有人说配置在~/.bashrc里,有人说配置在~/.bash_login里,还有人说配置在~/.profile里。。。这真是公说公有理。。。那么问题来了,linux到底是怎么读取配置文件的呢,依据又是什么呢?下面这篇文章就来给大家详细的介绍下,一起来看看吧。

文档

我一向讨厌那种说结论不说出处的行为,这会给人一种“我凭什么相信你”的感觉。而且事实上没有出处就随便议论得出的结论也基本上是人云亦云的。事实上,与其去问别人,不如去问文档。 找了一会,发现关于环境变量配置的相关文档其实是在bash命令的man文档里,毕竟我们常用的就是这个shell。

在$man bash里,我发现了下面的一段文字:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
invocation
  a login shell is one whose first character of argument zero is a -, or
  one started with the --login option.
  an interactive shell is one started without non-option arguments and
  without the -c option whose standard input and error are both connected
  to terminals (as determined by isatty(3)), or one started with the -i
  option. ps1 is set and $- includes i if bash is interactive, allowing
  a shell script or a startup file to test this state.
  the following paragraphs describe how bash executes its startup files.
  if any of the files exist but cannot be read, bash reports an error.
  tildes are expanded in filenames as described below under tilde expan‐
  sion in the expansion section.
  when bash is invoked as an interactive login shell, or as a non-inter‐
  active shell with the --login option, it first reads and executes com‐
  mands from the file /etc/profile, if that file exists. after reading
  that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
  in that order, and reads and executes commands from the first one that
  exists and is readable. the --noprofile option may be used when the
  shell is started to inhibit this behavior.
  when a login shell exits, bash reads and executes commands from the
  file ~/.bash_logout, if it exists.
  when an interactive shell that is not a login shell is started, bash
  reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
  these files exist. this may be inhibited by using the --norc option.
  the --rcfile file option will force bash to read and execute commands
  from file instead of /etc/bash.bashrc and ~/.bashrc.
  when bash is started non-interactively, to run a shell script, for
  example, it looks for the variable bash_env in the environment, expands
  its value if it appears there, and uses the expanded value as the name
  of a file to read and execute. bash behaves as if the following com‐
  mand were executed:
    if [ -n "$bash_env" ]; then . "$bash_env"; fi
  but the value of the path variable is not used to search for the file‐
  name.

通过这段文字,我们发现其实所谓的环境变量配置文件,就是在shell登陆的时候自动加载的那些文件。不过他所定义的登陆却分为两种:

  • login shell登陆。
  • interactive shell登陆。

login shell 登陆

所谓的login shell登陆,实际上就是指需要输入密码的登陆。具体的说,包括开机登陆、ssh登陆,或者是输入bash --login这种“假装自己输入密码登陆”的方式。 在这种登陆方式下,系统会先读取/etc/profile文件,然后,系统会依次搜索~/.bash_profile、~/.bash_login、~/.profile 这三个文件,并运行只其中第一个存在的文件。 尤其要注意到后三个文件的“逻辑或”的关系。很多情况下我们会发现,明明已经修改了~/.profile文件为什么重新登陆后配置不生效呢?这是因为我们的系统可能存在了前面两个文件中的一个,导致不会继续读取剩下的文件。

下面的三张图很好的说明了这个问题:

Linux中环境变量配置的步骤详解

Linux中环境变量配置的步骤详解

Linux中环境变量配置的步骤详解

interactive shell 登陆

所谓的interactive shell登陆,其实就是相对于login shell登陆而言的。我们平时在登陆后右键打开终端、或者ctrl+alt+t打开终端都是interactive shell登陆。 在这种登陆方式下,系统会依次读取/etc/bash.bashrc和~/.bashrc,并加以执行。 通常情况下,~/.bashrc文件里会默认记录一些常量和一些别名,尤其是$ps1变量,这个变量决定着bash提示符的格式、样式以及颜色等。

注意:

需要注意的是,这两种登陆方式读取的是不同的配置文件,而且互相之间没有交集,因此当我们需要配置环境变量时,我们要根据自己的登陆方式将需要的变量配置到不同的文件里。 例如下面这个经典的问题。

典型问题

环境配置文件配置异常的例子是,当我用ssh登录服务器的时候,发现提示符是这样的:

?
1
bash-4.3$

没错,就像上面第三张图片里的那个bash一样,提示符非常奇怪,而且当输入ls时文件和文件夹的颜色也没有区分。 这个问题显然是由于$ps1这个环境变量没有配置,导致他用了默认值,虽然查看.bashrc文件时发现有$ps1这个变量的定义。,但是由于ssh属于login shell,因此他在登陆时读入的配置文件是/etc/profile一类的文件,并没有读入.bashrc。 导致这个问题的原因通常是我们误删除了/etc/profile里默认的配置文件,因此解决的办法也很简单。。。把.bashrc里的部分文件复制到/etc/profile里就行了。

这个问题给我们的启示是,当我们为服务器配置变量时,尽量配置到/etc/profile里或者~/.bash_profile里,因为用ssh登录服务器是基本上用不到.bashrc文件的;当我们给自己的电脑配置环境变量时,尽量配置到.bashrc里,因为这样我们只要打开终端就会读入这个文件,这样就可以不用注销就能应用配置了(只有注销重新登录才会应用/etc/profile一类的配置文件)。

总结

以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://blog.mythsman.com/2017/03/28/1/

延伸 · 阅读

精彩推荐
  • Linux详解Linux系统下PXE服务器的部署过程

    详解Linux系统下PXE服务器的部署过程

    这篇文章主要介绍了Linux系统下PXE服务器的部署过程,包括对PXE的API架构作了一个基本的简介,需要的朋友可以参考下...

    运维之道9812019-07-04
  • LinuxLinux lnmp下无法使用mail发邮件的两种解决方法

    Linux lnmp下无法使用mail发邮件的两种解决方法

    在配置了lnmp环境后,出现了mail函数不能发送邮件的问题,其实有两种方法,一是使用sendmail组件,而是使用postfix。下面为大家一一介绍下 ...

    Linux之家4042019-09-17
  • Linux手把手教您在 Linux 上使用 GPG 加解密文件

    手把手教您在 Linux 上使用 GPG 加解密文件

    在本教程中,我将告诉你如何用 GPG 加密和解密文件。这是一个简单的教程,你可以在你的 Linux 系统上尝试所有的练习。这将帮助你练习 GPG 命令,并在你...

    Linux中国6962021-12-15
  • Linuxlinux中rmdir命令使用详解(删除空目录)

    linux中rmdir命令使用详解(删除空目录)

    今天学习一下linux中命令: rmdir命令。rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的 ...

    linux命令大全5372019-11-19
  • LinuxLinux上设置用户通过SFTP访问目录的权限的方法

    Linux上设置用户通过SFTP访问目录的权限的方法

    这篇文章主要介绍了Linux上设置用户通过SFTP访问目录的权限的方法,SFTP可以理解为使用SSH协议进行FTP传输的协议,因而同时要对OpenSSH进行相关设置,需要的朋...

    OSChina10022019-06-19
  • LinuxLinux中环境变量配置的步骤详解

    Linux中环境变量配置的步骤详解

    Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的...

    Myths7882022-02-10
  • LinuxLinux常用的日志文件和常用命令

    Linux常用的日志文件和常用命令

    成功地管理任何系统的关键之一,是要知道系统中正在发生什么事。 Linux 中提供了异常日志,并且日志的细节是可配置的。Linux 日志都以明文形式存储,所...

    Linux教程网2632020-04-18
  • Linux确保Linux系统安全的前提条件 漏洞防护

    确保Linux系统安全的前提条件 漏洞防护

    Linux 作为开放式的操作系统受到很多程序员的喜爱,很多高级程序员都喜欢编写Linux操作系统的相关软件。这使得Linux操作系统有着丰富的软件支持,还有无...

    Linux之家2642020-04-11