node.js 在 EC2 上:未找到 sudo 节点命令,但没有 sudo 的节点没问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4976658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:51:48  来源:igfitidea点击:

On EC2: sudo node command not found, but node without sudo is ok

node.jsbashamazon-ec2sudo

提问by foobar

I have just installed nodejs on a new EC2 micro instance.

我刚刚在新的 EC2 微型实例上安装了 nodejs。

I installed it normally, ./configure -> make -> sudo make install.

我正常安装,./configure -> make -> sudo make install。

Problem:When I run "node" under ec2-user, it runs perfectly. When I run "sudo node", it fails.

问题:当我在 ec2-user 下运行“node”时,它运行完美。当我运行“sudo node”时,它失败了。

I found out that node is in:

我发现节点在:

[ec2-user@XXXX ~]$ whereis node
node: /usr/local/bin/node /usr/local/lib/node

and the current path is

而当前的路径是

[ec2-user@XXXX ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/opt/aws/bin:/home/ec2-user/bin

but, the sudo path is

但是,sudo 路径是

[root@ip-10-112-222-32 ~]# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin

then I tried to edit the root PATH to include the paths to node, so "node" runs when I'm logged in as root - but it still won't work when I log in as ec2-user and run "sudo node".

然后我尝试编辑根路径以包含节点的路径,因此当我以 root 身份登录时“节点”运行 - 但是当我以 ec2-user 身份登录并运行“sudo node”时它仍然无法工作.

I need this to install npm properfly. Any idea on how to include the node path while running "sudo node"?

我需要这个来正确安装 npm。关于如何在运行“sudo node”时包含节点路径的任何想法?

回答by Michael Dillon

Yes, it is a bit annoying but you can fix it with some links:

是的,这有点烦人,但您可以通过一些链接修复它:

sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf

There might be more but that is all I have run across so far. Lack of node-waf will cause some npminstalls to fail with a rather cryptic error message.

可能还有更多,但这就是我迄今为止遇到的全部。缺少 node-waf 会导致某些npm安装失败,并显示相当神秘的错误消息。

回答by Hyman Frost

I added /usr/local/binto secure_pathin /etc/sudoers

我加入/usr/local/binsecure_path/etc/sudoers

$ sudo visudo

Then change this line:

然后改变这一行:

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

To:

到:

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

回答by Jeff Pal

it happens because the node executable is not found in /usr/bin. So follow the steps:

发生这种情况是因为在 /usr/bin 中找不到节点可执行文件。所以请按照以下步骤操作:

  1. find node:
  1. 查找节点:

whereis node

whereis node

in my case: node: /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node

就我而言: node: /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node

  1. make a symbolic link for node:

    sudo ln -s /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node /usr/bin/node

  1. 为节点创建一个符号链接:

    sudo ln -s /home/<my_user>/.nvm/versions/node/v8.9.4/bin/node /usr/bin/node

It's done!

完成!

回答by Shripad Krishna

Why not use the absolute path to node? If you planning to use an upstart script it is going to need an absolute path anyways.

为什么不使用节点的绝对路径?如果您打算使用新贵脚本,则无论如何都需要绝对路径。

sudo /usr/local/bin/node server.js

回答by Pavel Zubkou

You could pass full path to nodeexecutable from parent (non-sudo shell) using whichcommand.

您可以node使用which命令将完整路径从父级(非 sudo shell)传递到可执行文件。

sudo `which node`

回答by Amro

try the following:

尝试以下操作:

export PATH=$PATH:/usr/local/bin
sudo node --version

回答by user1839216

For me, it worked to just change ownership of node folder from root to ec2-user (logged in as ec2-user).

对我来说,它只是将节点文件夹的所有权从 root 更改为 ec2-user(以 ec2-user 身份登录)。

(Note: I created my node folder in /var/lib/)

(注意:我在 /var/lib/ 中创建了我的节点文件夹)

sudo chown -R ec2-user /var/lib/node/

Then

然后

npm install mongojs

should work fine (provided you have installed npm ok of course!)

应该可以正常工作(当然,前提是您已安装 npm ok!)

回答by Will Voelcker

How about using "sudo $(which node)" instead of "sudo node" ?

使用 "sudo $(which node)" 而不是 "sudo node" 怎么样?

回答by Hyman Murphy

Here's an approach that doesn't use symlinks, or require root:

这是一种不使用符号链接或需要 root 的方法:

$ git clone https://github.com/joyent/node.git
$ cd node
$ mkdir ~/opt
$ export PREFIX=~/opt; ./configure
$ make
$ make install
$ echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc

Then I did:

然后我做了:

$ git clone https://github.com/isaacs/npm.git
$ cd npm
$ make install

The benefits of not running node as root are discussed here:

这里讨论了不以 root 身份运行 node 的好处:

http://increaseyourgeek.wordpress.com/2010/08/18/install-node-js-without-using-sudo/

http://increaseyourgeek.wordpress.com/2010/08/18/install-node-js-without-using-sudo/

Its inline with:

其内联:

https://github.com/joyent/node/wiki/Installation

https://github.com/joyent/node/wiki/Installation

回答by shubham

In my case, Node was installed without sudoprefix. So node was unavailable for the superuser that why it is not working sudo node server

就我而言,Node 安装时没有sudo前缀。所以节点对超级用户不可用,为什么它不工作sudo node server