CentOS 7 上的 bash PATH 从哪里获得 /usr/local/bin ?

Iai*_*lam 8 bash bashrc centos7

我刚刚使用 centos/7 框使用 Vagrant 启动了一个全新的 CentOS 7 VM(bash 版本 4.2.46),当我以 vagrant 用户身份登录时,这是我的路径:

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin
Run Code Online (Sandbox Code Playgroud)

以 root 身份登录时,这是 PATH:

/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Run Code Online (Sandbox Code Playgroud)

我知道这/etc/profile是添加/usr/local/sbin:/usr/sbin(大多数用户在最后,root 用户在开始时),/etc/skel/.bash_profile最终在 vagrant 主目录中的正在添加/home/vagrant/.local/bin:/home/vagrant/bin,但是初始设置来自哪里?

目前我没有解释为什么用户最终会/usr/local/bin:/usr/bin在他们的 PATH 中,而 root 得到其他人。

(坦率地说,根路径的顺序似乎很奇怪,因为/usr/local/sbin/usr/sbin应在结束时,根据/etc/profile。)

use*_*571 14

其他人在 CentOS 错误追踪器上也问过同样的问题。我发现的最早的帖子是这个

/usr/local/bin 硬编码在 /bin/bash 中。

  1. https://bugs.centos.org/view.php?id=10200
  2. https://bugs.centos.org/view.php?id=10199
  3. https://bugs.centos.org/view.php?id=10198
  4. https://bugs.centos.org/view.php?id=7492

正如您将看到的,这个问题最早是在 2012 年 5 月报告的,并且已经存在于 CentOS 6 和 CentOS 7 中。

因此,尽管可能令人困惑,但看起来您的分析路径并非来自 /etc/profile 是正确的。路径来自 /bin/bash。


Sch*_*ute 7

在 bash 源“config-top.h”中有这个:

/* The default value of the PATH variable. */
#ifndef DEFAULT_PATH_VALUE
#define DEFAULT_PATH_VALUE \
  "/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
#endif

/* The value for PATH when invoking `command -p'.  This is only used when
   the Posix.2 confstr () function, or CS_PATH define are not present. */
#ifndef STANDARD_UTILS_PATH
#define STANDARD_UTILS_PATH \
  "/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc"
#endif
Run Code Online (Sandbox Code Playgroud)


use*_*517 6

它看起来肯定是硬编码的 /bin/bash

strings /bin/bash | grep "\/usr\/local\/bin"
/usr/local/bin:/bin:/usr/bin
Run Code Online (Sandbox Code Playgroud)

这似乎是默认的 PATH。


小智 5

除了以前的答案;如果您通过 ssh 访问您的 VM,这可能是有意义的。

/etc/ssh/sshd_config

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
Run Code Online (Sandbox Code Playgroud)