在 EC2 上:未找到 sudo 节点命令,但没有 sudo 的节点可以

发布于 2024-10-17 00:10:01 字数 722 浏览 2 评论 0原文

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

我正常安装了,./configure ->制作->须藤进行安装。

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

我发现节点位于:

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

并且当前路径是

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

但是,sudo 路径是

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

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

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

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

夏末 2024-10-24 00:10:01

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

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

可能还有更多链接,但这就是我到目前为止遇到的所有内容。缺少 node-waf 将导致某些 npm 安装失败,并显示相当神秘的错误消息。

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 npm installs to fail with a rather cryptic error message.

苄①跕圉湢 2024-10-24 00:10:01

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

  1. 找到节点:

whereis node

在我的情况下 :node:/home//.nvm/versions/node/v8.9.4/bin/node code>

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

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

完成!

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

  1. find node:

whereis node

in my case: 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

It's done!

浪荡不羁 2024-10-24 00:10:01

我将 /usr/local/bin 添加到 /etc/sudoers 中的 secure_path

$ sudo visudo

然后将此行更改

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

为:

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

I added /usr/local/bin to secure_path in /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
有深☉意 2024-10-24 00:10:01

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

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

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
戴着白色围巾的女孩 2024-10-24 00:10:01

尝试以下操作:

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

try the following:

export PATH=$PATH:/usr/local/bin
sudo node --version
疯狂的代价 2024-10-24 00:10:01

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

sudo `which node`

You could pass full path to node executable from parent (non-sudo shell) using which command.

sudo `which node`
我早已燃尽 2024-10-24 00:10:01

使用“sudo $(哪个节点)”而不是“sudo 节点”怎么样?

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

野生奥特曼 2024-10-24 00:10:01

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

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

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

然后

npm install mongojs

应该可以正常工作(当然前提是你已经安装了 npm !)

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

(Note: I created my node folder in /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!)

云醉月微眠 2024-10-24 00:10:01

这是一种不使用符号链接或不需要 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

然后我这样做了:

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

这里讨论不以 root 身份运行节点的好处:

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

其内联:

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

Here's an approach that doesn't use symlinks, or require 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:

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

Its inline with:

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

苏辞 2024-10-24 00:10:01

就我而言,安装的 Node 没有 sudo 前缀。因此超级用户无法使用节点,这就是为什么它无法工作sudo节点服务器

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

蓦然回首 2024-10-24 00:10:01

如果您已经安装了节点环境,请将这些命令粘贴到您的实例中,它会将您的节点链接到您的用户目录。您将能够通过 sudo 使用节点。

    sudo ln -s "$(which node)" "/usr/local/bin/node"
    sudo ln -s "$(which npm)" "/usr/local/bin/npm"

If you have installed node environment already, paste these command in you r instance and it'll link your node to your user directory. And you will be able to use node with sudo.

    sudo ln -s "$(which node)" "/usr/local/bin/node"
    sudo ln -s "$(which npm)" "/usr/local/bin/npm"
深巷少女 2024-10-24 00:10:01

以 root 身份输入

sudo su

,然后执行 标准步骤

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
node -e "console.log('Running Node.js ' + process.version)"

Enter as root with

sudo su

and then do standard steps

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
node -e "console.log('Running Node.js ' + process.version)"
海螺姑娘 2024-10-24 00:10:01

这就是我为解决类似问题所做的。注意:我已经通过 snap 安装了节点。

第 1 步:通过 snap 安装节点

sudo snap install node --classic

第 2 步:查找节点的安装位置

whereis node

在我的例子中

/snap/bin/node.npm 
/snap/bin/node.npx 
/snap/bin/node.yarn 
/snap/bin/node 
/snap/bin/node.yarnpkg

第 3 步:创建指向节点、npm、npx 和 YARN 的符号链接

sudo ln -s /snap/bin/yarn /usr/bin/yarn
sudo ln -s /snap/bin/node /usr/bin/node
sudo ln -s /snap/bin/npm /usr/bin/npm

最后,所有用户都可以访问节点,甚至 sudo group

sudo node

通过 snap 为所有用户安装节点, ubuntu、debian

This is what I did to solve a similar issue. Note: I had installed node via snap.

Step 1: Install node via snap

sudo snap install node --classic

Step 2: Find where node has been installed

whereis node

In my case

/snap/bin/node.npm 
/snap/bin/node.npx 
/snap/bin/node.yarn 
/snap/bin/node 
/snap/bin/node.yarnpkg

Step 3: Create symbolic links to node, npm, npx and yarn

sudo ln -s /snap/bin/yarn /usr/bin/yarn
sudo ln -s /snap/bin/node /usr/bin/node
sudo ln -s /snap/bin/npm /usr/bin/npm

Finally node is accessible for all users, even sudo group

sudo node

Installing node via snap for all users, ubuntu, debian

春庭雪 2024-10-24 00:10:01

我尝试了上面答案中提到的所有内容,但没有任何效果。可能是因为我对符号链接相关的概念缺乏理解。我通过不使用 nvm 解决了这个问题。
我刚刚创建了一个新的ec2实例,没有安装nvm。相反,我像这样安装了 node 和 npm:

sudo apt update
sudo apt install nodejs npm

这为我完成了。

I tried everything mentioned in the above answers but nothing worked. Probably because of my lack of understanding of concepts related to sym links. I fixed the issue by not using nvm.
I just created a new ec2 instance and didn't install nvm. Instead I installed node and npm like so:

sudo apt update
sudo apt install nodejs npm

And this did it for me.

[浮城] 2024-10-24 00:10:01

我不知道这是否是正确的方法,但这就是我所做的...

sudo su
export PATH=$PATH:/home/ec2-user/local/node/bin
curl http://npmjs.org/install.sh | sh
chown -R ec2-user /home/ec2-user/local/node
exit

这安装了 npm,我现在可以安装我想要的任何软件包。

I don't know if this is the right way, but this is what i did...

sudo su
export PATH=$PATH:/home/ec2-user/local/node/bin
curl http://npmjs.org/install.sh | sh
chown -R ec2-user /home/ec2-user/local/node
exit

This installed npm, and I can now install any packages I want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文