Linux下各个环境变量以及bin目录解析

前言

最近在学习Go语言,配置Go环境的时候因为系统有一个低版本的Go,导致一直配置不到正确的版本。系统的Go在/usr/bin目录,查了一下是我配环境变量的命令不对。现在对这几个各式各样的环境变量和bin目录进行解析一下。

环境变量:/etc/profile/etc/bashrc~/.bashrc~/bash_profile

bin目录:/bin/usr/bin/sbin/usr/sbin/usr/local/bin

环境变量

  • /etc/profile 系统环境变量
  • /etc/bashrc 系统bash变量
  • ~/.bashrc 用户bash变量
  • ~/bash_profile 交互式进入bash shell的变量
  • /etc/environment 系统变量,非登录级别

登录系统执行顺序

  1. /etc/environment

  2. /etc/profile

  3. ~/.profile

  4. ~/.env(如果存在)

登录系统时shell读取顺序

  1. /etc/profile
  2. /etc/environment
  3. ~/.bash_profile
  4. ~/.bash_login
  5. ~/.profile
  6. ~/.bash_profile
  7. ~/.bashrc
  8. /etc/bashrc

退出时执行~/.bash_logout

bin目录

说明

首先需要明确一点的是,usr并不是user的所写,大概率是unix system resources的缩写,用户目录是home呀

引申

usr目录在很早以前的系统是家的目录,也是user的缩写,现在由/home/someone取代了/usr/someone,但是不排除一些unix系统还在使用usr作为家目录

文档说明

/usr usually contains by far the largest share of data on a system. Hence, this is one of the most important directories in the system as it contains all the user binaries, their documentation, libraries, header files, etc…. X and its supporting libraries can be found here. User programs like telnet, ftp, etc…. are also placed here. In the original Unix implementations, /usr was where the home directories of the users were placed (that is to say, /usr/someone was then the directory now known as /home/someone). In current Unices, /usr is where user-land programs and data (as opposed to ’system land’ programs and data) are. The name hasn’t changed, but it’s meaning has narrowed and lengthened from “everything user related” to “user usable programs and data”. As such, some people may now refer to this directory as meaning ‘User System Resources’ and not ‘user’ as was originally intended.(via 1,2)

明确了这一点就好解释了

/bin 和 /sbin

  • bin是系统必备的执行档,例如:cat、cp、chmod df、dmesg、gzip、kill、ls、mkdir、more、mount、rm、su、tar等
  • /sbin是系统管理员必备的执行档,例如:cfdisk、dhcpcd、dump、e2fsck、fdisk、halt、ifconfig、ifup、 ifdown、init、insmod、lilo、lsmod、mke2fs、modprobe、quotacheck、reboot、rmmod、 runlevel、shutdown等

/usr/bin 和/usr/sbin

  • /usr/bin 经常放一些软件应用的必备执行档,例如c++、g++、gcc、chdrv、diff、dig、du、eject、elm、free、gnome、 zip、htpasswd、kfm、ktop、last、less、locale、m4、make、man、mcopy、ncftp、 newaliases、nslookup passwd、quota、smb、wget等
  • /usr/sbin 经常放一些网络管理的必备软件,例如:dhcpd、httpd、imap、in.*d、inetd、lpd、named、netconfig、nmbd、samba、sendmail、squid、swap、tcpd、tcpdump等

/usr/local/bin

/usr/local/bin 经常放一些用户软件的link,apt自动安装的软件也会放在这个目录链接,/usr/bin很少会放link

/usr/local/bin/usr/bin相比,后者会随着系统更新而改变,而/usr/local/bin不受影响,建议在此处放用户应用链接

二者关系

经过不断查找,找到了一个中间桥梁。/etc/environment,这个是设置整个系统的环境,我查看里面只有

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
LANG="zh_CN.UTF-8"
LANGUAGE="zh_CN:zh:en_US:en"

查找资料发现,这是系统登录访问的第一个文件,之后是/etc/profile

/etc/bashrc

也就是系统会在开始定义好PATH变量的初始值把相关bin目录都加进去。

后续我们再添加环境变量的时候使用$PATH:YOUR PATH 或者YOUR PATH:$PATH即可

注意

当我们要使用我们后来安装的软件代替系统/usr/bin,我们需要使用YOUR PATH:$PATH

这样我们才能让我们定义的路径先生效。

如果更改的bashrc相关配置的,可以直接再启动一个终端让其生效,如果是改的profile相关的配置文件,需要执行source命令哦。

总结

最终得出,全局变量推荐放在/etc/profile,用户变量推荐放在~/.profile或者~/.bash_profile

这样用户登录只会执行一次,放到~/.bashrc是很多教程推荐的方式,但是这样会影响效率(每次打开Terminal都会执行)。

另外,在Terminal直接export 出来的是临时变量,生命周期是终端。

引用说明

Linux目录/usr缩写及目录结构说明

Linux环境变量加载顺序

Linux下的/bin、/sbin、/usr/bin、/usr/sbin目录的区别

感谢以上作者,如有侵权请私戳我

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注