frp的frps.service配置的User=nobody绑定不了某些端口

Created on 2 Nov 2019  ·  13Comments  ·  Source: fatedier/frp

Issue is only used for submiting bug report and documents typo. If there are same issues or answers can be found in documents, we will close it directly.
(为了节约时间,提高处理问题的效率,不按照格式填写的 issue 将会直接关闭。)
(请不要在 issue 评论中出现无意义的 加1我也是 等内容,将会被直接删除。)
(由于个人精力有限,和系统环境,网络环境等相关的求助问题请转至其他论坛或社交平台。)

Use the commands below to provide key information from your environment:
You do NOT have to include this information if this is a FEATURE REQUEST

What version of frp are you using (./frpc -v or ./frps -v)?
0.29.0

What operating system and processor architecture are you using (go env)?

Configures you used:

Steps to reproduce the issue:
1.复制frps.service到/etc/systemd/system
2.在/etc/frp/frps.ini 填写比如 vhost_https_port = 443
3.使用systemctl start frps启动 发现启动失败

Describe the results you received:
使用 systemctl status frps -l 显示

● frps.service - Frp Server Service
   Loaded: loaded (/etc/systemd/system/frps.service; disabled)
   Active: activating (auto-restart) (Result: exit-code) since Fri 2019-11-01 23:58:02 EDT; 2s ago
  Process: 2085 ExecStart=/usr/bin/frps -c /etc/frp/frps.ini (code=exited, status=1/FAILURE)
 Main PID: 2085 (code=exited, status=1/FAILURE)

Describe the results you expected:

Additional information you deem important (e.g. issue happens only occasionally):

Can you point out what caused this issue (optional)
使用User=nobody的时候,只能绑定一些高位端口,443之类的端口都不能绑定,netstat -ntulp |grep 443发现并没有被别的软件占用。
但是把User=nobody注释掉使用root用户就没问题了,很奇怪,不知道为啥。

Most helpful comment

额外一提:假设不使用 root 来进行绑定 1024 一下端口的话,可以使用 setcap 或者是增加 systemd 单元文件的 cap 相关选项来达成目的

All 13 comments

正常现象

这是系统限制

许多 Unix 系统会限制只有 root 用户才能绑定一些端口。而且这种限制很可能是写死的,也就是说没办法设置“允许某用户组绑定端口”。这种情况下只能先以 root 用户启动,绑定端口后再用 setuid 来 “drop root”。

你可以认为这只是一个示例配置,可以根据自己的需要进行修改。

……
部分系统限制了非 root 账户不能绑定 1024 以下的端口。

额外一提:假设不使用 root 来进行绑定 1024 一下端口的话,可以使用 setcap 或者是增加 systemd 单元文件的 cap 相关选项来达成目的

详细看了下,使用 systemd 的发行版可以使用 CapabilityBoundingSet = 来设置这个服务的一些能力

详细看了下,使用 systemd 的发行版可以使用 CapabilityBoundingSet = 来设置这个服务的一些能力

这里有点错误,CapabilityBoundingSet 是限制该服务拥有的 cap,也就是不在这个列表里的 cap 都会被去掉。要想添加一个 cap,正确的做法是

AmbientCapabilities=CAP_NET_BIND_SERVICE

这样你的 frps 就可以绑定 1~1023 这些端口 (Privileged Ports) 了

详细看了下,使用 systemd 的发行版可以使用 CapabilityBoundingSet = 来设置这个服务的一些能力

这里有点错误,CapabilityBoundingSet 是限制该服务拥有的 cap,也就是不在这个列表里的 cap 都会被去掉。要想添加一个 cap,正确的做法是

AmbientCapabilities=CAP_NET_BIND_SERVICE

这样你的 frps 就可以绑定 1~1023 这些端口 (Privileged Ports) 了

是这个样子的啊

详细看了下,使用 systemd 的发行版可以使用 CapabilityBoundingSet = 来设置这个服务的一些能力

这里有点错误,CapabilityBoundingSet 是限制该服务拥有的 cap,也就是不在这个列表里的 cap 都会被去掉。要想添加一个 cap,正确的做法是

AmbientCapabilities=CAP_NET_BIND_SERVICE

这样你的 frps 就可以绑定 1~1023 这些端口 (Privileged Ports) 了

是这个样子的啊

放在 [Service] 段下。
我粘个完整的方便后面的人看:

[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

详细看了下,使用 systemd 的发行版可以使用 CapabilityBoundingSet = 来设置这个服务的一些能力

这里有点错误,CapabilityBoundingSet 是限制该服务拥有的 cap,也就是不在这个列表里的 cap 都会被去掉。要想添加一个 cap,正确的做法是

AmbientCapabilities=CAP_NET_BIND_SERVICE

这样你的 frps 就可以绑定 1~1023 这些端口 (Privileged Ports) 了

是这个样子的啊

放在 [Service] 段下。
我粘个完整的方便后面的人看:

[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

不过那个 5s 究竟是为了什么?

详细看了下,使用 systemd 的发行版可以使用 CapabilityBoundingSet = 来设置这个服务的一些能力

这里有点错误,CapabilityBoundingSet 是限制该服务拥有的 cap,也就是不在这个列表里的 cap 都会被去掉。要想添加一个 cap,正确的做法是

AmbientCapabilities=CAP_NET_BIND_SERVICE

这样你的 frps 就可以绑定 1~1023 这些端口 (Privileged Ports) 了

看了下 文档 ,事实上使用 CapabilityBoundingSet 也可以增加 cap ,不过 CapabilityBoundingSet 也可以做到删减 cap

详细看了下,使用 systemd 的发行版可以使用 CapabilityBoundingSet = 来设置这个服务的一些能力

这里有点错误,CapabilityBoundingSet 是限制该服务拥有的 cap,也就是不在这个列表里的 cap 都会被去掉。要想添加一个 cap,正确的做法是

AmbientCapabilities=CAP_NET_BIND_SERVICE

这样你的 frps 就可以绑定 1~1023 这些端口 (Privileged Ports) 了

是这个样子的啊

放在 [Service] 段下。
我粘个完整的方便后面的人看:

[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

特意登陆表示感谢。
首先您提供了root cause的关键解法。
我还在困惑这句应该放在哪里生效,
AmbientCapabilities=CAP_NET_BIND_SERVICE
您又贴了完整code。

希望我这个无用的感谢信息没有给大家造成困扰。

Was this page helpful?
0 / 5 - 0 ratings