resouce/install_v2ray_core.sh

97 lines
2.4 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
V2RAY_URL="https://git.functen.cn/wangqifan/resouce/raw/branch/main/v2ray-linux-64.zip"
WORKDIR="/tmp/v2ray-core-install"
echo "=== 检查是否为 root 用户 ==="
if [ "$(id -u)" -ne 0 ]; then
echo "请使用 root 用户运行此脚本sudo bash install_v2ray_core.sh"
exit 1
fi
echo "=== 安装 unzip如果尚未安装==="
apt update
apt install -y unzip
echo "=== 创建临时目录:$WORKDIR ==="
rm -rf "$WORKDIR"
mkdir -p "$WORKDIR"
cd "$WORKDIR"
echo "=== 下载 v2ray-core 压缩包 ==="
wget -O v2ray-linux-64.zip "$V2RAY_URL"
echo "=== 解压 v2ray-linux-64.zip ==="
unzip -o v2ray-linux-64.zip
# 解压后一般有 v2ray / geoip.dat / geosite.dat / config.json 等
if [ ! -f ./v2ray ]; then
echo "未在压缩包中找到 v2ray 可执行文件,安装中止。"
exit 1
fi
echo "=== 安装 v2ray 到 /usr/local/bin/v2ray ==="
install -m 755 ./v2ray /usr/local/bin/v2ray
echo "=== 安装 geo 数据文件到 /usr/local/share/v2ray ==="
mkdir -p /usr/local/share/v2ray
if [ -f ./geoip.dat ]; then
cp ./geoip.dat /usr/local/share/v2ray/geoip.dat
fi
if [ -f ./geosite.dat ]; then
cp ./geosite.dat /usr/local/share/v2ray/geosite.dat
fi
echo "=== 创建 /etc/v2ray 目录 ==="
mkdir -p /etc/v2ray
# 如果没有现成的 config.json则写一个最小配置不会监听任何端口主要是给 systemd 用)
if [ ! -f /etc/v2ray/config.json ]; then
echo "=== 生成默认配置 /etc/v2ray/config.json空 inbounds仅占位==="
cat >/etc/v2ray/config.json <<EOF
{
"log": {
"loglevel": "warning"
},
"inbounds": [],
"outbounds": [
{
"protocol": "freedom",
"settings": {}
}
]
}
EOF
fi
echo "=== 创建 systemd 服务 /etc/systemd/system/v2ray.service ==="
cat >/etc/systemd/system/v2ray.service <<'EOF'
[Unit]
Description=V2Ray Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/v2ray run -config /etc/v2ray/config.json
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
echo "=== 重新加载 systemd并启用 v2ray 服务 ==="
systemctl daemon-reload
systemctl enable --now v2ray
echo "=== 检查 v2ray 版本 ==="
v2ray -version || true
echo "=== 安装完成!==="
echo "如果配合 v2rayA 使用:"
echo " - v2ray 核心路径:/usr/local/bin/v2ray"
echo " - geo 文件目录:/usr/local/share/v2ray"
echo " - 配置目录:/etc/v2ray"
echo "查看服务状态systemctl status v2ray"
echo "查看日志journalctl -u v2ray -f"