在端口 80 上运行节点 js,而不以 root 身份运行

Posted

技术标签:

【中文标题】在端口 80 上运行节点 js,而不以 root 身份运行【英文标题】:Run node js on ports 80 without running as root 【发布时间】:2018-11-20 19:13:05 【问题描述】:

我想在端口 80 上运行我的 node.js 应用程序而不以 root 身份运行它

$> which node
/usr/bin/node

我曾尝试使用 setcap,但它不起作用:

$> sudo setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/node
Failed to set capabilities on file `/usr/bin/node' (Operation not 
permitted)
The value of the capability argument is not permitted for a file. Or 
the file is not a regular (non-symlink) file

我做错了什么?

【问题讨论】:

Best practices when running Node.js with port 80 (Ubuntu / Linode)的可能重复 【参考方案1】:

最后,我使用了端口重定向,不是用 iptables 而是用 ufw

https://serverfault.com/questions/238563/can-i-use-ufw-to-setup-a-port-forward

【讨论】:

【参考方案2】:

低于 1000 的端口需要 sudo 权限。您可以使用以下命令允许它在不使用 sudo 的情况下绑定到端口 80:

sudo setcap 'cap_net_bind_service=+ep' `which node`

【讨论】:

以上是关于在端口 80 上运行节点 js,而不以 root 身份运行的主要内容,如果未能解决你的问题,请参考以下文章