在 CentOS 中向 $PATH 添加目录?

von*_*rad 100 configuration shell centos root path

我们刚刚启动了我们的新服务器,并且我们正在所有这些服务器上运行 CentOS。成功安装 Ruby Enterprise Edition 后,我现在想添加 REE /bin(位于/usr/lib/ruby-enterprise/bin)目录,使其成为服务器上的默认 Ruby 解释器。

我尝试了以下操作,它只会将其添加到当前的 shell 会话中:

export PATH=/usr/lib/ruby-enterprise/bin:$PATH
Run Code Online (Sandbox Code Playgroud)

所有用户永久添加此目录的正确方法是什么?我目前以.$PATHroot

Mik*_*aro 135

编辑这样/etc/profile的东西不是一个好主意,因为每当 CentOS 发布此文件的更新时,您将丢失所有更改。这正是/etc/profile.d用于:

echo 'pathmunge /usr/lib/ruby-enterprise/bin' > /etc/profile.d/ree.sh
chmod +x /etc/profile.d/ree.sh
Run Code Online (Sandbox Code Playgroud)

重新登录并享受您的(安全)更新$PATH

echo $PATH
/usr/lib/ruby-enterprise/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

which ruby
/usr/lib/ruby-enterprise/bin/ruby
Run Code Online (Sandbox Code Playgroud)

您可以重新加载配置文件,而不是重新登录:

. /etc/profile
Run Code Online (Sandbox Code Playgroud)

这将更新$PATH变量。

  • 是的,对于单个用户。但问题是关于为 **all** 用户更改 PATH。 (6认同)
  • @Mike 这个 pathmunge 命令是什么? (4认同)
  • `~/.profile` 也是另一个有效的选项 (2认同)

von*_*rad 11

在遵循 fmonk 的建议之后,我查看了/etc/bashrc,我注意到它说“环境内容在 /etc/profile 中”。我继续往里看/etc/profile,我看到了这个:

pathmunge () {
    if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
       if [ "$2" = "after" ] ; then
          PATH=$PATH:$1
       else
          PATH=$1:$PATH
       fi
    fi
}

[...]

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
fi
Run Code Online (Sandbox Code Playgroud)

为了解决我的问题,我只是pathmunge /usr/lib/ruby-enterprise/bin在 if 语句下面添加了。这解决了我的问题。

  • 您应该使用过 /etc/profile.d。看我下面的回答。 (7认同)

Gio*_*osK 5

抱歉,以下问题误解 了用户的个人资料 ,以防万一它对某人有帮助

修改 .bash_profile

nano ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)

然后在文件中的某处添加/修改由以下分隔的路径:

 PATH=$PATH:$HOME/bin:/your/path
 export PATH
Run Code Online (Sandbox Code Playgroud)

然后重新加载您的个人资料

source ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)

或注销并重新登录

如果您检查 PATH 它应该包括您新添加的路径

echo $PATH
Run Code Online (Sandbox Code Playgroud)