以下步骤介绍如何在 Linux 系统(如 Debian/Ubuntu 和 CentOS/Fedora)上安装和配置 V2Ray,这是一款强大的代理软件。

1. 下载和安装 V2Ray

对于 Debian/Ubuntu 系统:

# 使用脚本自动安装 V2Ray
bash <(curl -L -s https://install.direct/go.sh)

对于 CentOS/Fedora 系统:

# 使用脚本自动安装 V2Ray
sudo bash <(curl -L -s https://install.direct/go.sh)

安装脚本将会自动下载并安装 V2Ray。

2. 配置 V2Ray

V2Ray 的配置文件位于 /etc/v2ray/config.json。下面是一个示例配置文件,您可以根据需要进行修改:

{
  "inbounds": [{
    "port": 1080,
    "listen": "0.0.0.0",
    "protocol": "socks",
    "settings": {
      "auth": "noauth",
      "udp": false,
      "ip": "127.0.0.1"
    }
  }],
  "outbounds": [{
    "protocol": "vmess",
    "settings": {
      "vnext": [{
        "address": "your_v2ray_server_address",
        "port": 443,
        "users": [{
          "id": "your_uuid",
          "alterId": 64,
          "security": "aes-128-gcm"
        }]
      }]
    }
  }]
}
  • inbounds 部分定义了本地监听的端口和协议,这里设置为 SOCKS 代理,端口为 1080
  • outbounds 部分定义了连接到远程 V2Ray 服务器的配置,您需要根据您的服务器地址、UUID 等信息进行修改。

3. 启动和管理 V2Ray 服务

使用 systemd 来管理 V2Ray 服务。

启动 V2Ray 服务:

sudo systemctl start v2ray

设置开机自启动:

sudo systemctl enable v2ray

查看 V2Ray 服务状态:

sudo systemctl status v2ray

4. 验证 V2Ray 是否正常运行

您可以使用以下命令检查 V2Ray 是否正在监听指定的端口:

netstat -lntp | grep v2ray

5. 设置为系统服务(可选)

为了确保 V2Ray 在系统启动时自动运行,可以将其设置为 systemd 服务。

创建一个 V2Ray 服务文件:

sudo nano /etc/systemd/system/v2ray.service

在文件中添加以下内容:

[Unit]
Description=V2Ray Service
After=network.target
 
[Service]
ExecStart=/usr/bin/v2ray -config /etc/v2ray/config.json
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

保存并关闭文件,然后启用并启动 V2Ray 服务:

sudo systemctl daemon-reload
sudo systemctl enable v2ray
sudo systemctl start v2ray

6. 检查 V2Ray 服务状态

您可以使用以下命令检查 V2Ray 服务的状态:

systemctl status v2ray

如果 V2Ray 正常运行,您应该会看到类似以下的输出:

● v2ray.service - V2Ray Service
   Loaded: loaded (/etc/systemd/system/v2ray.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2024-06-25 10:00:00 UTC; 1min 30s ago
 Main PID: 12345 (v2ray)
    Tasks: 4 (limit: 4915)
   Memory: 20.0M
   CGroup: /system.slice/v2ray.service
           └─12345 /usr/bin/v2ray -config /etc/v2ray/config.json

现在,V2Ray 应该已经成功安装并运行在您的 Linux 系统上。您可以根据需要调整 config.json 文件中的配置,以满足您的代理需求。

7. 脚本化安装和配置 V2Ray

为了方便重复操作,您可以将上述安装和配置步骤写成一个 Shell 脚本。以下是一个完整的安装脚本示例:

#!/bin/bash
 
# 安装 V2Ray
echo "Installing V2Ray..."
bash <(curl -L -s https://install.direct/go.sh)
 
# 创建 V2Ray 配置目录
CONFIG_DIR="/etc/v2ray"
if [ ! -d "$CONFIG_DIR" ]; then
  mkdir -p "$CONFIG_DIR"
fi
 
# 创建示例配置文件
echo "Creating example config.json..."
cat <<EOL > ${CONFIG_DIR}/config.json
{
  "inbounds": [{
    "port": 1080,
    "listen": "0.0.0.0",
    "protocol": "socks",
    "settings": {
      "auth": "noauth",
      "udp": false,
      "ip": "127.0.0.1"
    }
  }],
  "outbounds": [{
    "protocol": "vmess",
    "settings": {
      "vnext": [{
        "address": "your_v2ray_server_address",
        "port": 443,
        "users": [{
          "id": "your_uuid",
          "alterId": 64,
          "security": "aes-128-gcm"
        }]
      }]
    }
  }]
}
EOL
 
# 创建 systemd 服务文件
echo "Creating systemd service file..."
sudo bash -c "cat <<EOL > /etc/systemd/system/v2ray.service
[Unit]
Description=V2Ray Service
After=network.target
 
[Service]
ExecStart=/usr/bin/v2ray -config /etc/v2ray/config.json
Restart=on-failure
 
[Install]
WantedBy=multi-user.target
EOL"
 
# 启用并启动 V2Ray 服务
echo "Enabling and starting V2Ray service..."
sudo systemctl daemon-reload
sudo systemctl enable v2ray
sudo systemctl start v2ray
 
# 检查 V2Ray 服务状态
echo "V2Ray service status:"
systemctl status v2ray

使用方法:

  1. 将上述脚本保存为 install_v2ray.sh

  2. 赋予脚本执行权限:

    chmod +x install_v2ray.sh
  3. 执行脚本:

    sudo ./install_v2ray.sh

通过这个脚本,您可以自动化安装和配置 V2Ray,并将其设置为系统服务。确保根据实际情况修改配置文件中的代理服务器地址、端口和 UUID。