Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have a problem.

Lately I've installed nodejs (if you don't know what it is, it does not really matter, could be anything else) from sources to /opt/node:

$ ls -la /opt
lrwxrwxrwx  1 root root   11 2012-07-25 13:48 node -> node-0.6.3/
drwxr-xr-x  6 root root 4096 2012-07-25 13:48 node-0.6.3

so, the binaries are in /opt/node/bin:

$ ls -la /opt/node/bin
lrwxrwxrwx 1 root root      38 2012-06-20 11:44 npm -> ../lib/node_modules/npm/bin/npm-cli.js

As you can see, npm is there. To make it easier to use it from a command line I fixed $PATH inside .bashrc script:


and so did to the root's .bashrc:

# which npm

But if I run npm with sudo, npm is not found:

$ sudo which npm
$ sudo npm -g install uglify-js
sudo: npm: command not found

So, why is it happening? How do I gain what I want?


UPDATE: Following Paul's advice, I've added

Defaults  env_keep = PATH

to the /etc/sudoers

but the problem persists anyway:

$ echo $PATH
... :/opt/node/bin
$ sudo su -
# sudo -V
Environment variables to preserve:

So, Defaults directive did it's job, but I'm getting the same sudo: npm: command not found

share|improve this question

marked as duplicate by mdpc, nc4pk, Scott, JakeGould, Steven Jan 7 at 19:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

So you marked an answer as accepted, but it did not fix your problem? – T.W.R. Cole Jun 16 '14 at 19:23
It did. Why you think it didn't ? – Nemoden Jun 22 '14 at 6:00
Related: Why are PATH variables different when running via sudo and su? at Unix SE – kenorb Dec 24 '15 at 18:03
up vote 5 down vote accepted

sudo invokes a new shell, and the environmental variables that are passed to the new shell are governed by the '/etc/sudoers' file. If you want your path to be passed through, then you need to add

Defaults env_keep = "PATH"

to '/etc/sudoers' and it will keep the PATH environment variable in the sudo shell

If this does not work, it is possible that either the path is being overwritten by a secure_path directive (sometime a secure_path is compiled in to the binary). If so, try adding this command, replacing the groupname with a group you are a member of:

exempt_group = "groupname"

Or override the secure_path with your own one, eg:

secure_path = "/bin:/usr/bin:/sbin:/usr/sbin:/opt/node/bin"
share|improve this answer
Thank you for a reply. It seems to be the key, but it does not make a difference. See above the update section of my question. – Nemoden Jul 25 '12 at 4:46
Can you add a sudo env | grep PATH to the question? – Paul Jul 25 '12 at 5:18
@Nemoden just a ping as I left you off the previous comment – Paul Jul 25 '12 at 5:49
Path to /opt/node/bin is not in sudo env | grep PATH: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bi‌​n – Nemoden Jul 26 '12 at 1:39
@Nemoden Answer updated – Paul Jul 26 '12 at 9:32

I had this problem -> mine was caused by npm being install in /use/local/bin.

And I had this line in my /etc/sudoers file:

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

Typing which npm was the enlightening part :)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged .