linux 无图形化配置代理
linux
本文字数:1.1k 字 | 阅读时长 ≈ 5 min

linux 无图形化配置代理

linux
本文字数:1.1k 字 | 阅读时长 ≈ 5 min

最近需要在一台只有命令行的 Ubuntu 服务器上配置代理,让终端能够访问外网。本文记录一下完整的配置过程,目前我的场景如下:Ubuntu 无图形界面,使用 v2ray-core,节点类型是 vmess + ws + tls。话不多说,直接说怎么做的

1. 更新系统

先刷新软件源,并安装后面要用到的基础工具:

sudo apt update
sudo apt install -y curl ca-certificates
systemctl --version | head -n 1

其中 ca-certificates 主要是看 https 证书是否正常,如果能看到 systemd 版本信息(例如 systemd 255 (255.4-1ubuntu8.5)),说明当前系统可以按后面的方式继续配置

2. 安装 v2ray-core

通过官方安装脚本安装 v2ray-core,并注册成系统服务

bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)
which v2ray
systemctl status v2ray --no-pager

一个是查看 v2ray 安装位置,例如可能是 /usr/local/bin/v2ray,此外就是看是否成功启动,如下如果看到 active (running),说明 v2ray 已经成功安装并启动。

● v2ray.service - V2Ray Service
     Loaded: loaded (/etc/systemd/system/v2ray.service; enabled; preset: enabled)
     Active: active (running)

3. 配置客户端 config.json

编辑 v2ray 的配置文件,让它在本机启动两个代理入口:

127.0.0.1:10808:SOCKS5
127.0.0.1:10809:HTTP
# 备份一下原配置文件
sudo cp /usr/local/etc/v2ray/config.json /usr/local/etc/v2ray/config.json.bak

# 编辑配置文件并查看文件是否有问题
sudo nano /usr/local/etc/v2ray/config.json
sudo /usr/local/bin/v2ray test -config /usr/local/etc/v2ray/config.json

配置文件大概如下所示,换成你对应的内容即可

{
  "log": {
    "loglevel": "warning"
  },
  "inbounds": [
    {
      "port": 10808,
      "listen": "127.0.0.1",
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": true
      }
    },
    {
      "port": 10809,
      "listen": "127.0.0.1",
      "protocol": "http",
      "settings": {}
    }
  ],
  "outbounds": [
    {
      "tag": "proxy",
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "www.mxxxt.cc",
            "port": 443,
            "users": [
              {
                "id": "0491321d-92e8-41a7-b002-a3939a43fd43",
                "alterId": 0,
                "security": "auto"
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "security": "tls",
        "tlsSettings": {
          "serverName": "www.mxxxt.cc",
          "allowInsecure": true
        },
        "wsSettings": {
          "path": "/fjklaiowefadfafadfadfafkajladfkjalf",
          "headers": {
            "Host": "www.mxxxt.cc"
          }
        }
      }
    }
  ]
}

4. 重启服务并检查本地端口是否监听

重启并看一下端口是否监听,大概如下所示

sudo systemctl restart v2ray
➜ v2ray sudo systemctl status v2ray --no-pager

● v2ray.service - V2Ray Service
     Loaded: loaded (/etc/systemd/system/v2ray.service; enabled; preset: enabled)
    Drop-In: /etc/systemd/system/v2ray.service.d
             └─10-donot_touch_single_conf.conf
     Active: active (running) since Thu 2026-03-12 23:25:51 CST; 2s ago
       Docs: https://www.v2fly.org/
   Main PID: 18742 (v2ray)
      Tasks: 8 (limit: 18703)
     Memory: 12.8M (peak: 13.3M)
        CPU: 35ms
     CGroup: /system.slice/v2ray.service
             └─18742 /usr/local/bin/v2ray run -config /usr/local/etc/v2ray/config.json

Mar 12 23:25:51 harry systemd[1]: Started v2ray.service - V2Ray Service.
Mar 12 23:25:51 harry v2ray[18742]: V2Ray 5.30.0 (V2Fly, a community-driven edition of V2Ray.) Custom (go1.24.2 linux/amd64)
Mar 12 23:25:51 harry v2ray[18742]: A unified platform for anti-censorship.
Mar 12 23:25:51 harry v2ray[18742]: 2026/03/12 23:25:51 [Warning] V2Ray 5.30.0 started
➜ v2ray ss -lntp | grep -E '10808|10809'
LISTEN 0      4096       127.0.0.1:10808      0.0.0.0:*          
LISTEN 0      4096       127.0.0.1:10809      0.0.0.0:*          

5. 测试代理是否真的能出网

如下所示,如果显示的是你 remote 就是你代理服务器的 ip,就对了,如果出现 curl: (7) Failed to connect to api.ipify.org port 443 after 100 ms: Couldn't connect to server 就不对

➜ curl --proxy socks5h://127.0.0.1:10808 https://api.ipify.org
your_remote_ip
➜ curl -x http://127.0.0.1:10809 https://api.ipify.org
your_remote_ip       

6. 给 zsh 添加 proxy_on / proxy_off

因为我平时用的是 zsh,所以把代理开关写到 ~/.zshrc 里。编辑 ~/.zshrc

在最后追加:

proxy_on() {
  export http_proxy=http://127.0.0.1:10809
  export https_proxy=http://127.0.0.1:10809
  export all_proxy=socks5://127.0.0.1:10808
  export HTTP_PROXY=http://127.0.0.1:10809
  export HTTPS_PROXY=http://127.0.0.1:10809
  export ALL_PROXY=socks5://127.0.0.1:10808
  echo "proxy on"
}

proxy_off() {
  unset http_proxy https_proxy all_proxy
  unset HTTP_PROXY HTTPS_PROXY ALL_PROXY
  echo "proxy off"
}

保存后重新加载

source ~/.zshrc
(python3.10) ➜ ~ type proxy_on
proxy_on is a shell function from /home/yonghuiwang/.zshrc
(python3.10) ➜ ~ type proxy_off
proxy_off is a shell function from /home/yonghuiwang/.zshrc
May 06, 2025
Dec 31, 2024
Nov 17, 2024