侦听大多数端口时出现 Node.js EACCES 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9164915/
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 15:08:05  来源:igfitidea点击:

Node.js EACCES error when listening on most ports

httpnode.jspermission-denied

提问by jwegner

I'm testing out an app (hopefully to run on heroku, but am having issues locally as well). It's giving me an EACCES error when it runs http.Server.listen() - but it only occurs on some ports.

我正在测试一个应用程序(希望在 heroku 上运行,但在本地也有问题)。它在运行 http.Server.listen() 时给我一个 EACCES 错误 - 但它只发生在某些端口上。

So, locally I'm running:

所以,我在本地运行:

joe@joebuntu:~$ node
> var h = require('http').createServer();
> h.listen(900);
Error: EACCES, Permission denied
    at Server._doListen (net.js:1062:5)
    at net.js:1033:14
    at Object.lookup (dns.js:132:45)
    at Server.listen (net.js:1027:20)
    at [object Context]:1:3
    at Interface.<anonymous> (repl.js:150:22)
    at Interface.emit (events.js:42:17)
    at Interface._onLine (readline.js:132:10)
    at Interface._line (readline.js:387:8)
    at Interface._ttyWrite (readline.js:564:14)

I don't have anything running on port 900 (or any of the other 20 ports I've tried), so this should work. The weird part is that it doeswork on some ports. For instance, port 3000 works perfectly.

我没有在端口 900(或我尝试过的其他 20 个端口中的任何一个)上运行任何东西,所以这应该可以工作。奇怪的是它确实在某些端口工作。例如,端口 3000 完美运行。

What would cause this?

什么会导致这种情况?

Update 1:

更新 1:

I figured out that on my local computer, the EACCES error is coming because I have to run node as root in order to bind to those certain ports. I don't know why this happens, but using sudo fixes it. However, this doesn't explain how I would fix it on Heroku. There is no way to run as root on Heroku, so how can I listen on port 80?

我发现在我的本地计算机上,EACCES 错误即将到来,因为我必须以 root 身份运行节点才能绑定到那些特定端口。我不知道为什么会发生这种情况,但是使用 sudo 可以修复它。但是,这并不能解释我将如何在 Heroku 上修复它。无法在 Heroku 上以 root 身份运行,那么我如何侦听端口 80?

回答by Ben Taber

Running on your workstation

在您的工作站上运行

As a general rule, processes running without root privileges cannot bind to ports below 1024.

作为一般规则,没有 root 权限运行的进程不能绑定到 1024 以下的端口。

So try a higher port, or run with elevated privileges via sudo. You can downgrade privileges after you have bound to the low port using process.setgidand process.setuid.

所以尝试一个更高的端口,或者通过sudo. 使用process.setgid和绑定到低端口后,您可以降级权限process.setuid

Running on heroku

在heroku上运行

When running your apps on heroku you have to use the port as specified in the PORT environment variable.

在 heroku 上运行您的应用程序时,您必须使用 PORT 环境变量中指定的端口。

See http://devcenter.heroku.com/articles/node-js

http://devcenter.heroku.com/articles/node-js

const server = require('http').createServer();
const port = process.env.PORT || 3000;

server.listen(port, () => console.log(`Listening on ${port}`));

回答by user1303768

Non-privileged user (not root) can't open a listening socket on ports below 1024.

非特权用户(不是 root)无法在 1024 以下的端口上打开侦听套接字。

回答by Meet Mehta

Check this reference link:

检查此参考链接

Give Safe User Permission To Use Port 80

Remember, we do NOT want to run your applications as the root user, but there is a hitch: your safe user does not have permission to use the default HTTP port (80). You goal is to be able to publish a website that visitors can use by navigating to an easy to use URL like http://ip:port/

Unfortunately, unless you sign on as root, you'll normally have to use a URL like http://ip:port- where port number > 1024.

A lot of people get stuck here, but the solution is easy. There a few options but this is the one I like. Type the following commands:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

Now, when you tell a Node application that you want it to run on port 80, it will not complain.

授予安全用户使用端口 80 的权限

请记住,我们不想以 root 用户身份运行您的应用程序,但有一个问题:您的安全用户没有使用默认 HTTP 端口 (80) 的权限。您的目标是能够发布一个网站,访问者可以通过导航到一个易于使用的 URL 来使用,例如 http://ip:port/

不幸的是,除非您以 root 身份登录,否则您通常必须使用类似http://ip:port- 端口号 > 1024的 URL 。

很多人被困在这里,但解决方案很简单。有几种选择,但这是我喜欢的一种。键入以下命令:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

现在,当您告诉 Node 应用程序您希望它在端口 80 上运行时,它不会抱怨。

回答by eleven

Another approach is to make port redirection:

另一种方法是进行端口重定向:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 900 -j REDIRECT --to-port 3000

And run your server on >1024 port:

并在 >1024 端口上运行您的服务器:

require('http').createServer().listen(3000);

ps the same could be done for https(443) port by the way.

ps 顺便说一下,https(443) 端口也可以这样做。

回答by Mark Karwowski

It means node is not able to listen on defined port. Change it to something like 1234 or 2000 or 3000 and restart your server.

这意味着节点无法侦听定义的端口。将其更改为 1234 或 2000 或 3000 之类的内容,然后重新启动服务器。

回答by Dilip Muthukurussimana

OMG!! In my case I was doing ....listen(ip, port)instead of ...listen(port, ip)and that was throwing up the error msg: Error: listen EACCES localhost

我的天啊!!在我的情况下,我正在做....listen(ip, port)而不是...listen(port, ip)抛出错误消息:Error: listen EACCES localhost

I was using port numbers >= 3000 and even tried with admin access. Nothing worked out. Then with a closer relook, I noticed the issue. Changed it to ...listen(port, ip)and everything started working fine!!

我使用的端口号 >= 3000,甚至尝试使用管理员访问。什么都没解决。然后仔细再看,我注意到了这个问题。将其更改为...listen(port, ip),一切开始正常运行!!

Just calling this out in case if its useful to someone else...

如果它对其他人有用,只需将其调用...

回答by mabounassif

I got this error on my mac because it ran the apache server by default using the same port as the one used by the node server which in my case was the port 80. All I had to do is stop it with sudo apachectl stop

我在我的 mac 上遇到这个错误,因为它默认使用与节点服务器使用的端口相同的端口运行 apache 服务器,在我的例子中是端口 80。我所要做的就是停止它 sudo apachectl stop

Hope this helps someone.

希望这可以帮助某人。

回答by ZILONG PAN

I got this error on my mac too. I use npm run devto run my Nodejs app in Windows and it works fine. But I got this error on my mac - error given was: Error: bind EACCES null:80.

我的 mac 也出现了这个错误。我使用npm run dev运行在Windows中我的应用程序的NodeJS和它工作正常。但是我在我的 mac - 上遇到了这个错误error given was: Error: bind EACCES null:80

One way to solve this is to run it with root access. You may use sudo npm run devand will need you to put in your password.

解决此问题的一种方法是使用 root 访问权限运行它。您可以使用sudo npm run dev并且需要您输入密码。

It is generally preferable to serve your application on a non privileged port, such as 3000, which will work without root permissions.

通常最好在非特权端口(例如 3000)上为您的应用程序提供服务,该端口无需 root 权限即可工作。

reference: Node.js EACCES error when listening on http 80 port (permission denied)

参考:Node.js EACCES 侦听 http 80 端口时出错(权限被拒绝)

回答by DraughtGlobe

Try authbind:

尝试身份验证:

http://manpages.ubuntu.com/manpages/hardy/man1/authbind.1.html

http://manpages.ubuntu.com/manpages/hardy/man1/authbind.1.html

After installing, you can add a file with the name of the port number you want to use in the following folder: /etc/authbind/byport/

安装后,您可以在以下文件夹中添加一个带有您要使用的端口号名称的文件:/etc/authbind/byport/

Give it 500 permissions using chmod and change the ownership to the user you want to run the program under.

使用 chmod 为其授予 500 权限,并将所有权更改为您要在其下运行程序的用户。

After that, do "authbind node ..." as that user in your project.

之后,在您的项目中以该用户身份执行“authbind node ...”。

回答by Sean

Remember if you use sudo to bind to port 80 and are using the env variables PORT & NODE_ENV you must reexport those vars as you are now under root profile and not your user profile. So, to get this to work on my Mac i did the following:

请记住,如果您使用 sudo 绑定到端口 80 并使用环境变量 PORT & NODE_ENV,您必须重新导出这些变量,因为您现在处于 root 配置文件而不是您的用户配置文件下。所以,为了让它在我的 Mac 上工作,我做了以下事情:

sudo su
export NODE_ENV=production
export PORT=80
docpad run