我发现现在官方的一键安装脚本生成的systemd的启动文件,指定的是root用户运行的,毕竟v2ray是对外服务的,我担心会有人通过开放的端口可能存在的漏洞获得root权限,所以我做了个小小的设置,让v2ray以普通用户身份运行,减少攻击获取root权限的几率
1、创建用于运行v2ray的用户,且不允许执行解释器
useradd v2ray -s /usr/sbin/nologin
2、修改v2ray.service文件,加入User和Group两项
vim /etc/systemd/system/v2ray.service
[Unit]
Description=V2Ray Service
After=network.target
Wants=network.target
[Service]
User=v2ray
Group=v2ray
Type=simple
PIDFile=/var/run/v2ray.pid
ExecStart=/usr/bin/v2ray/v2ray -config /etc/v2ray/config.json
Restart=on-failure
[Install]
WantedBy=multi-user.target
执行systemctl daemon-reload
3、为日志目录赋权
chown -R v2ray:v2ray /var/log/v2ray
4、重启服务
systemctl restart v2ray
5、查看运行状态
ps -ef |grep v2ray
v2ray 3382 1 0 21:49 ? 00:00:00 /usr/bin/v2ray/v2ray -config /etc/v2ray/config.json
root 3484 2073 0 22:04 pts/0 00:00:00 grep v2ray
这样就可以看到v2ray是以普通用户运行的了
谢谢
推个push修改?
另外还需要做cap修改,因为低端口和透明代理功能需要CAP_NET_ADMIN能力。
一键脚本能否加入自动更新功能呢?作为服务器端,可以每周更新V2Ray-core内核
补充一点,对于使用低位端口(<=1024) 的用户,执行完上述操作后会遇到服务无法启动的问题,因为默认情况下低位端口只有 root 可以绑定,此时可以用 root 用户特别指定该进程即使没有 root 权限也可以绑定低位端口,需要执行如下命令:
setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/v2ray/v2ray
整理成TAR.GZ包放在世界的每一台提供下载的服务器上吧。呵呵
还有一点补充,如果用TLS的话,v2ray用户需要私钥的可读权限,推荐用acl权限控制
1、安装acl
apt-get install acl
2、赋予acl只读权限
setfacl -m user:v2ray:r-- /etc/v2ray/v2ray.key(私钥路径)
3、为了安全,私钥赋予root和v2ray用户只读权限
chmod 400 /etc/v2ray/v2ray.key
我还是 root吧
最初没有使用单独用户的原因是不能监听1024以下的端口,以及创建用户需要额外的工作。systemd的配置和安装脚本并不是绑定的,因为安装脚本并不支持所有的平台。在用户单独使用systemd的时候,就需要自己运行一些命令才可以创建合理的用户帐号。
对于这个问题,现在只能在v2ray.service中添加一些说明,让用户自己选择。
其实并不一定要创建新用户吧,systemd支持直接指定nobody用户,至于监听端口的问题
[Service]
CapabilityBoundingSet=CAP_NET_RAW CAP_NET_ADMIN
在.service文件中加上上面的内容就行了
然后配置文件和日志文件的权限就成了问题。我觉得还是不要踩坑为好,比如:
https://github.com/systemd/systemd/issues/7717
https://github.com/systemd/systemd/issues/7906
另外最新版的 systemd 中支持了 DynamicUser,应该还要等普及了才行
建议:
useradd -d /etc/v2ray/ -M -s /sbin/nologin v2ray
[Unit]
Description=V2Ray Service %i
After=network.target
Wants=network.target
[Service]
# This service runs as root. You may consider to run it as another use for security concerns.
# By uncommenting the following two lines, this service will run as user v2ray/v2ray.
# More discussion at https://github.com/v2ray/v2ray-core/issues/1011
User=v2ray
Group=v2ray
Type=simple
PIDFile=/var/run/v2ray.pid
ExecStart=/usr/bin/v2ray/v2ray -config /etc/v2ray/%i.json
Restart=on-failure
# Don't restart in the case of configuration error
RestartPreventExitStatus=23
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
RestartSec=5s
Restart=on-failure
WorkingDirectory=/etc/v2ray
DeviceAllow=/dev/null rw
DeviceAllow=/dev/net/tun rw
[Install]
WantedBy=multi-user.target
一键脚本能否加入自动更新功能呢?作为服务器端,可以每周更新V2Ray-core内核
可以参考该脚本,我已经写了个,在我的服务器上可以更新,由于隐私及水平所限,具体使用需要自己修改下。基于docker
https://github.com/AaG7xNnrgbzeyqc5woPS/V2_shell/blob/master/upv2.sh
我发现现在官方的一键安装脚本生成的systemd的启动文件,指定的是root用户运行的,毕竟v2ray是对外服务的,我担心会有人通过开放的端口可能存在的漏洞获得root权限,所以我做了个小小的设置,让v2ray以普通用户身份运行,减少攻击获取root权限的几率
1、创建用于运行v2ray的用户,且不允许执行解释器
useradd v2ray -s /usr/sbin/nologin2、修改v2ray.service文件,加入User和Group两项
vim /etc/systemd/system/v2ray.service[Unit] Description=V2Ray Service After=network.target Wants=network.target [Service] User=v2ray Group=v2ray Type=simple PIDFile=/var/run/v2ray.pid ExecStart=/usr/bin/v2ray/v2ray -config /etc/v2ray/config.json Restart=on-failure [Install] WantedBy=multi-user.target执行systemctl daemon-reload
3、为日志目录赋权
chown -R v2ray:v2ray /var/log/v2ray4、重启服务
systemctl restart v2ray5、查看运行状态
ps -ef |grep v2rayv2ray 3382 1 0 21:49 ? 00:00:00 /usr/bin/v2ray/v2ray -config /etc/v2ray/config.json root 3484 2073 0 22:04 pts/0 00:00:00 grep v2ray这样就可以看到v2ray是以普通用户运行的了
现在来看,那个 Group=v2ray 其实并没有太大意义,因为在文档描述来说,假设没指定 Group 时会使用默认的用户组
Most helpful comment
补充一点,对于使用低位端口(<=1024) 的用户,执行完上述操作后会遇到服务无法启动的问题,因为默认情况下低位端口只有 root 可以绑定,此时可以用 root 用户特别指定该进程即使没有 root 权限也可以绑定低位端口,需要执行如下命令:
setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/v2ray/v2ray