sudo 环境:“g++:找不到命令”

sudo 环境:“g++:找不到命令”

我安装了相对较新的 RHEL 6.5,并从源代码 GCCC 4.9 安装到了该版本上。安装 GCC 4.9 后,我通过以下方式卸载了发行版提供的旧 GCC 版本:

sudo yum remove gcc

海湾合作委员会出现正确安装并对用户和用户可见root,但是当我尝试发出sudo需要编译器的命令时,找不到它。

在我看来,这PATH并不指向g++while sudo,但我不明白为什么。

g++安装在:

[john@haley boost_1_55_0]$ which g++
/usr/local/bin/g++

并以用户身份获取版本并root成功:

[john@haley boost_1_55_0]$ g++ --version
g++ (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[john@haley boost_1_55_0]$ sudo su -
root@haley /root # g++ --version
g++ (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

sudo g++ --version失败了:

john@haley boost_1_55_0]$ sudo g++ --version
[sudo] password for john: 
sudo: g++: command not found
[john@haley boost_1_55_0]$ 

检查PATHsudo

[john@haley boost_1_55_0]$ sudo echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/home/john/bin:/usr/local/bin
                                                                             ^^^^^^^^^

...似乎表明 的位置g++实际上在路径中。

为什么会失败,我能做些什么来解决这个问题?

回答评论中的问题:

是的,我可以使用以下显式路径执行它sudo

[john@haley boost_1_55_0]$ sudo /usr/local/bin/g++ --version
[sudo] password for john: 
g++ (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[john@haley boost_1_55_0]$ 

在检查时发现我做错了sudo PATH。以正确的方式去做表明/usr/local/bin事实上不是sudoPATH:

[john@haley boost_1_55_0]$ sudo env | grep PATH
13:PATH=/sbin:/bin:/usr/sbin:/usr/bin
[john@haley boost_1_55_0]$ 

答案1

我将其发布为答案,因为我通过对OP的评论发现了这个解决方案,但我不确定这就是我想要的应该做。

我可以通过运行sudo visudo和编辑来完成这项工作secure_path以包含/usr/local/bin.

在我的系统上,原始行是:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

将其更改为:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

“修复”问题:

[john@haley boost_1_55_0]$ sudo g++ --version
g++ (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[john@haley boost_1_55_0]$ 

相关内容