0%

Linux 环境变量

Linux 系统包含两类环境变量:系统环境变量和用户环境变量。

系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效。

1、修改用户环境变量
用户环境变量通常被存储在下面的文件中:

  • ~/.profile

  • ~/.bash_profile 或者 ~./bash_login

  • ~/.bashrc

上述文件在 Ubuntu 10.0 以前版本不推荐使用。

2、系统环境变量
系统环境变量一般保存在下面的文件中:

  • /etc/environment

  • /etc/profile

  • /etc/bash.bashrc

其中 environment 文件直接写变量 $VAR='ABC';profile 文件是在登录时被执行,要写成命令的形式,如export $VAR='ABC';bashrc 是当 bash 被打开的时候被读取执行

/etc/profile 不生效问题

发现启动后/etc/profile 不生效,需要手动source /etc/profile.

首先查到可能是login shellnon-login shell的区别:
简单来说需要输入密码登陆的是login shell如通过 ssh 访问,在图形桌面中直接打开的终端为non-login.

login shell(bash) 在登入时,会读取的配置文件:

  1. /etc/profile
  2. ~/.bash_profile 或~/.bash_login 或 ~/.profile (就按照这个顺序)
  3. ~/.bashrc (通常是写在前面两个配置文件中)

第二步之所以有三个文件,是因为不同的 shell 有可能命名不同,只会按顺序读取其中的一个。

non-login shell(bash) 在登入时,只会读取 ~/.bashrc

参考:

  1. https://blog.csdn.net/linuxnews/article/details/52579186
  2. (http://feihu.me/blog/2014/env-problem-when-ssh-executing-command-on-remote/)

使用 ssh 连接还是不生效

最后发现是因为使用的 zsh 并不会读取/etc/profile, 而是使用/etc/zsh/下面的 zshenv、zprofile、zshrc、zlogin文件,并以这个顺序进行加载。