Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a relatively fresh RHEL 6.5 install, on to which I have installed from source GCCC 4.9. After I installed GCC 4.9, I uninstalled the distro-provided older GCC version via:

sudo yum remove gcc

GCC appears to be correctly installed and visible to both users and root, but when I try to issue a sudo command which needs the compiler, it cannot be found.

It seems to me that PATH doesn't point to g++ during sudo, but I don't understand why.

g++ is installed in:

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

And getting the version as a user and as root succeeds:

[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.

But sudo g++ --version fails:

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

Checking the PATH as sudo:

[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
                                                                             ^^^^^^^^^

... seems to indicate that the location of g++ is actually in the path.

Why is this failing, and what can I do to fix it?

Answering questions in comments:

yes, I can execute it using explicit paths under 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]$ 

It was observed that I was doing-it-wrong when checking the sudo PATH. Doing it the right way reveals that in fact /usr/local/bin is not in sudo's PATH:

[john@haley boost_1_55_0]$ sudo env | grep PATH
13:PATH=/sbin:/bin:/usr/sbin:/usr/bin
[john@haley boost_1_55_0]$ 
share|improve this question
    
Can you execute it if you use absolute pathnames with sudo? –  psimon Jun 21 at 14:57
    
@psimon: Yes; question edited with demo. –  John Dibling Jun 21 at 15:02
2  
The way you're checking $PATH is wrong because $PATH expanded by the shell. Try sudo env | grep PATH. –  Mikel Jun 21 at 15:02
    
@Mikel: Quite right. Doing it the right way confirms that the directory is not in sudo's path. Question edited with demo. –  John Dibling Jun 21 at 15:04
    
Should I visudo and add /usr/local/bin to the secure_path? –  John Dibling Jun 21 at 15:07

1 Answer 1

up vote 4 down vote accepted

I'm posting this as an answer because I discovered this solution through comments on the OP, but I'm not sure that this is what I should do.

I can make this work by running sudo visudo and editing the secure_path to include /usr/local/bin.

On my system, the original line is:

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

Changing it to:

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

"fixes" the problem:

[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]$ 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.