I’m using Ubuntu 14.04. I’m having trouble getting a command picked up in my $PATH using sudo. I have added this to my /root/.profile file
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
export PATH=$PATH:/usr/local/rvm/rubies/default/bin
mesg n
I have logged out and logged back in as my user with sudo privileges. Then I try and run the command within the $PATH but I get a “command not found” …
daveuser@mymachine:/home/rails/myproject$ sudo gem pristine --all
sudo: gem: command not found
daveuser@mymachine:/home/rails/myproject$ sudo echo $PATH
/usr/local/rvm/gems/ruby-2.3.0/bin:/usr/local/rvm/gems/ruby-2.3.0@global/bin:/usr/local/rvm/rubies/ruby-2.3.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/ bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/rvm/bin
daveuser@mymachine:/home/rails/myproject$ sudo which gem
daveuser@mymachine:/home/rails/myproject$ which gem
/usr/local/rvm/rubies/ruby-2.3.0/bin/gem
Notice that when I echo $PATH I get exactly the PATH where the “gem” command lives. What else do I need to do to get my “gem” command recognized when I run the command as the sudo user?
Edit: The preserve-env didn’t work. here’s what happened …
remoteuser@remotemachine:/home/rails/myproject$ sudo --preserve-env gem pristine --all
[sudo] password for remoteuser:
sudo: gem: command not found
Edit 2: output of sudo -l
remoteuser@rtemotemachine:/home/rails/myproject$ sudo -l
Matching Defaults entries for remoteuser on rtemotemachine:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin
User remoteuser may run the following commands on rtemotemachine:
(ALL : ALL) ALL
sudo <command>
use to search for<command>
? – steeldriver Oct 22 '16 at 17:02sudo echo $PATH
, the shell expands$PATH
beforesudo
ever comes into play. Also, please add the output ofsudo -l
. – muru Oct 22 '16 at 17:02