alpine安装sing-box
方法一:通过alpine托管安装
在此之前先对系统进行更新并安装必要工具:
apk update && apk upgrade && apk add wget openssl
首先,查看sing-box的官方文档,得知alpine系统的安装包发布在edge版的testing仓库中,我们可以先查看一下包名。
1、指定临时仓库地址更新索引:
apk update --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing
2、搜索此仓库sing-box包:
apk search --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing sing-box
3、安装sing-box包(搜索结果中可以看到具体的版本号,安装的时候只需指定sing-box即可安装最新版本。因此亦可无需查询直接用下面命令安装):
apk add --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing sing-box
4、修改配置文件后启动服务:
1)修改配置文件(配置文件需另行修改,不是本文重点)
vi /etc/sing-box/config.json
2)添加自启并启动服务
rc-update add sing-box default && rc-service sing-box start
至此,sing-box就在alpine上正常运行了。
方法二:下载github linux架构包安装
由于方法一中的alpine软件包不一定更新得及时,我们可以通过官网github release包来直接安装。
1、配置临时环境变量(版本号可指定):
export SING_BOX_VERSION=1.11.3
export ARCH=$(case "$(uname -m)" in 'x86_64') echo 'amd64';; 'x86' | 'i686' | 'i386') echo '386';; 'aarch64' | 'arm64') echo 'arm64';; 'armv7l') echo 'armv7';; 's390x') echo 's390x';; *) echo '不支持的服务器架构';; esac)
echo -e "\n我的服务器架构是:"$ARCH
2、下载对应架构版本文件
wget https://github.com/SagerNet/sing-box/releases/download/v$SING_BOX_VERSION/sing-box-$SING_BOX_VERSION-linux-$ARCH.tar.gz
3、解压并将可执行文件移动到/usr/bin目录下:
tar -zxf sing-box-$SING_BOX_VERSION-linux-$ARCH.tar.gz && mv sing-box-$SING_BOX_VERSION-linux-$ARCH/sing-box /usr/bin
4、清理过程文件:
rm -rf ./sing-box-$SING_BOX_VERSION-linux-$ARCH && rm -f ./sing-box-$SING_BOX_VERSION-linux-$ARCH.tar.gz
5、创建配置目录以及基本配置文件(配置文件需另行修改,不是本文重点):
mkdir /etc/sing-box
echo "{}" > /etc/sing-box/config.json
6、配置系统文件:
cat <<'EOF' > /etc/init.d/sing-box
#!/sbin/openrc-run
name=$RC_SVCNAME
description="sing-box service"
supervisor="supervise-daemon"
command="/usr/bin/sing-box"
command_args="-D /var/lib/sing-box -C /etc/sing-box run"
depend() {
after net dns
}
reload() {
ebegin "Reloading $RC_SVCNAME"
/bin/kill -HUP $MAINPID
eend $?
}
EOF
7、加入OpenRC自启动:
chmod +x /etc/init.d/sing-box //加权系统文件
rc-update add sing-box default //自启
rc-service sing-box start //启动服务
8、其它命令:
rc-service sing-box restart //重启服务
rc-service sing-box status //查询服务状态
rc-status -a //查询所有服务状态
ps aux | grep sing-box //查询sing-box状态
killall sing-box //中止sing-box所有服务
rc-update del sing-box //删除自启
rc-service sing-box stop //停止服务
附后
附上方法一中通过alpine托管安装生成的配置文件(/etc/init.d/sing-box),此文件带有检查配置文件相关功能:
#!/sbin/openrc-run
name=$RC_SVCNAME
description="sing-box service"
supervisor="supervise-daemon"
command="/usr/bin/sing-box"
extra_started_commands="reload checkconfig"
: ${SINGBOX_CONFIG="/etc/sing-box"}
if [ -d "$SINGBOX_CONFIG" ]; then
_config_opt="-C $SINGBOX_CONFIG"
elif [ -z "$SINGBOX_CONFIG" ]; then
_config_opt=""
else
_config_opt="-c $SINGBOX_CONFIG"
fi
command_args="run --disable-color
-D ${SINGBOX_WORKDIR:-"/var/lib/sing-box"}
$_config_opt"
depend() {
after net dns
}
checkconfig() {
ebegin "Checking $RC_SVCNAME configuration"
sing-box check $_config_opt
eend $?
}
start_pre() {
checkconfig
}
reload() {
ebegin "Reloading $RC_SVCNAME"
checkconfig && $supervisor "$RC_SVCNAME" --signal HUP
eend $?
}
注:本文未涉及证书配置相关内容,仅着重讲解alpine系统安装sing-box的方法。
Thx, have fun!