出口

出口

我有一个 makefile,用于sudo make install处理应用程序中通常需要 root 安装的部分。不幸的是,这会导致一种奇怪的情况,无法sudo访问npm,而root我的用户和用户都可以访问:

自己:

$ which npm
/home/self/.nvm/versions/node/v5.9.1/bin/npm

根:

$ sudo su -

root@theserver~# which npm
/root/.nvm/versions/node/v5.9.1/bin/npm

出口

$ sudo which npm $ (这是空白行,sudo which npm 没有结果)

我该如何解决这个差异?我缺少 npm 生态系统的哪些功能,让我可以以不同的方式做到这一点?

我正在运行 Ubuntu 14.04 LTS:

cat /proc/version
cat /etc/issue
uname -a
Linux version 3.13.0-44-generic (buildd@lamiak) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
Ubuntu 14.04.4 LTS \n \l
Linux theserver 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

标准用户和 root 的 PATH 和 HOME。

user@server:~$ cat ~/.profile 
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
user@server:~$ sudo echo $PATH $HOME
/home/user/.nvm/versions/node/v5.9.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games /home/user
user@server:~$ echo $PATH $HOME
/home/user/.nvm/versions/node/v5.9.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games /home/user
user@server:~$ sudo su
root@server:/home/user# echo $PATH $HOME
/root/.nvm/versions/node/v5.9.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games /root
root@server:/home/user# cat ~/.profile
# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n

答案1

这可能是因为你env_resetsudoers(5)文件。

默认情况下,env_reset 选项处于启用状态。这会导致使用新的、最小的环境执行命令...

该最小环境不包含 PATH。sudoers Command Environment(5) 手册页的部分包含大量相关信息供您细读。

相关内容