为当前最主流的抗封锁协议生成 sing-box 服务端及客户端配置。
443)或端口范围(e.g., 30000-40000)。客户端会自动进行端口跳跃。
sing-box generate reality-keypair 命令在服务器上执行,并将生成的公钥/私钥替换配置中的占位符。
#!/bin/bash
# 1. Install necessary tools
echo "Installing curl, tar, and openssl..."
apt update && apt install -y curl tar openssl
# 2. Detect architecture
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH="amd64"
;;
aarch64)
ARCH="arm64"
;;
*)
echo "Unsupported architecture: $ARCH"
exit 1
;;
esac
echo "Detected architecture: $ARCH"
# 3. Get the latest version of sing-box
LATEST_VERSION=$(curl -s "https://api.github.com/repos/SagerNet/sing-box/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sed 's/v//')
echo "Latest sing-box version: $LATEST_VERSION"
DOWNLOAD_URL="https://github.com/SagerNet/sing-box/releases/download/v${LATEST_VERSION}/sing-box-${LATEST_VERSION}-linux-${ARCH}.tar.gz"
# 4. Download and install
echo "Downloading from $DOWNLOAD_URL"
curl -sLo /tmp/sing-box.tar.gz "$DOWNLOAD_URL"
tar -xzf /tmp/sing-box.tar.gz -C /usr/local/bin --strip-components=1 "sing-box-${LATEST_VERSION}-linux-${ARCH}/sing-box"
chmod +x /usr/local/bin/sing-box
rm /tmp/sing-box.tar.gz
# 5. Create config directory
mkdir -p /etc/sing-box/
# 6. Create systemd service file
echo "Creating systemd service..."
cat > /etc/systemd/system/sing-box.service <<'EOF'
[Unit]
Description=sing-box service
After=network.target
[Service]
ExecStart=/usr/local/bin/sing-box run -D /etc/sing-box
Restart=on-failure
RestartSec=10
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
# 7. Enable and start service
systemctl daemon-reload
systemctl enable sing-box
echo "----------------------------------------"
echo "Sing-box installation complete!"
echo "Version $(/usr/local/bin/sing-box version)"
echo ""
echo "Next steps:"
echo "1. Place your .json config files into /etc/sing-box/"
echo "2. Start the service with: systemctl start sing-box"
echo "3. Check status with: systemctl status sing-box"
echo "4. View logs with: journalctl -u sing-box -f"
echo "----------------------------------------"