Skip to content

Instantly share code, notes, and snippets.

@tranch
Last active July 19, 2026 13:32
Show Gist options
  • Select an option

  • Save tranch/4345d69c4cb6e93c57dd311645f9f8ab to your computer and use it in GitHub Desktop.

Select an option

Save tranch/4345d69c4cb6e93c57dd311645f9f8ab to your computer and use it in GitHub Desktop.
V2ray binary install script
#!/bin/sh
# Set the script to exit immediately if any command fails.
set -e
V2RAY_BINARY_PATH=/usr/local/bin
V2RAY_LOCATION_ASSET_PATH=/usr/local/share/v2ray
V2RAY_CONFIG_PATH=/usr/local/etc/v2ray
V2RAY_SERVICE_PATH="/etc/systemd/system/v2ray.service"
echo "Stopping and disabling V2Ray service..."
systemctl stop v2ray || true
systemctl disable v2ray || true
echo "Removing V2Ray files and directories..."
# Remove binary file
rm -f "$V2RAY_BINARY_PATH/v2ray"
# Remove config files
rm -f "$V2RAY_CONFIG_PATH/config.json"
rm -rf "$V2RAY_CONFIG_PATH"
# Remove data asset files
rm -f "$V2RAY_LOCATION_ASSET_PATH"/*.dat
rm -rf "$V2RAY_LOCATION_ASSET_PATH"
# Remove Systemd service file
if [ -f "$V2RAY_SERVICE_PATH" ]; then
rm -f "$V2RAY_SERVICE_PATH"
systemctl daemon-reload
echo "Removed Systemd service file and reloaded Systemd."
fi
#!/usr/bin/env bash
set -eu
V2RAY_DIR=/usr/local/bin
V2RAY=$V2RAY_DIR/v2ray
CONFIG_DIR=/usr/local/etc/v2ray
CONFIG=$CONFIG_DIR/config.yml
UNIT=/etc/systemd/system/v2ray.service
ASSETS=/usr/local/share/v2ray
PORT=${VMESS_PORT:-8090}
WS_PATH=${VMESS_WS_PATH:-/ws}
[ "$(id -u)" -eq 0 ] || { echo "Run this script as root" >&2; exit 1; }
apt-get update
apt-get install -y unzip wget
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
wget -q -O "$tmp_dir/v2ray.zip" \
https://github.com/v2fly/v2ray-core/releases/latest/download/v2ray-linux-64.zip
unzip -q -o "$tmp_dir/v2ray.zip" -d "$tmp_dir/v2ray"
install -d "$V2RAY_DIR" "$CONFIG_DIR" "$ASSETS"
install -m 0755 "$tmp_dir/v2ray/v2ray" "$V2RAY"
find "$tmp_dir/v2ray" -maxdepth 1 -type f -name '*.dat' -exec install -m 0644 {} "$ASSETS/" \;
UUID=$($V2RAY uuid)
tmp_config="$tmp_dir/config.yml"
cat >"$tmp_config" <<EOF
log:
loglevel: warning
inbounds:
- listen: 127.0.0.1
port: $PORT
protocol: vmess
settings:
clients:
- id: $UUID
alterId: 0
streamSettings:
network: ws
wsSettings:
path: $WS_PATH
outbounds:
- protocol: freedom
tag: direct
settings: {}
EOF
"$V2RAY" test -config "$tmp_config"
install -d -m 0755 "$CONFIG_DIR"
install -m 0644 "$tmp_config" "$CONFIG"
cat >"$UNIT" <<EOF
[Unit]
Description=V2Ray Service
After=network-online.target
Wants=network-online.target
[Service]
DynamicUser=yes
Environment=V2RAY_LOCATION_ASSET=$ASSETS
ExecStart=$V2RAY run -config $CONFIG
Restart=on-failure
NoNewPrivileges=true
PrivateTmp=true
ProtectHome=true
ProtectSystem=strict
ReadOnlyPaths=$ASSETS $CONFIG_DIR
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
echo "Installed V2Ray configuration: $CONFIG"
echo "Installed systemd unit: $UNIT"
echo "Generated VMess UUID: $UUID"
echo "The service was not enabled or started."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment