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

linux 无图形化配置代理

linux
本文字数:1.7k 字 | 阅读时长 ≈ 8 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

7. Mac 配置补充

mac 和 linux 的思路完全相同,下面简单展示一下

macOS 上不使用 apt,这里主要依赖 Homebrew。先确认是否已经安装 Homebrew:

brew --version
# 如果没安装,用下面命令安装
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

然后执行下面命令

brew update
# 查看当前 Homebrew 的安装路径:
brew --prefix
/opt/homebrew

这里的路径可以用 $(brew --prefix) 代替,这个路径后面要用,接下来用 Homebrew 安装 v2ray

(base) ➜ ~ brew install v2ray
xxx
(base) ➜ ~ which v2ray
/opt/homebrew/bin/v2ray
(base) ➜ ~ v2ray version        
V2Ray 5.48.0 (V2Fly, a community-driven edition of V2Ray.) Custom (go1.26.2 darwin/arm64)
A unified platform for anti-censorship.
(base) ➜ ~ 

编辑 v2ray 的配置文件,这里和 linux 完全相同了,执行下面命令即可,这里也先备份一下,然后正常编辑即可

V2RAY_ETC="$(brew --prefix)/etc/v2ray"
mkdir -p "$V2RAY_ETC"

# 如果原来有配置,先备份
cp "$V2RAY_ETC/config.json" "$V2RAY_ETC/config.json.bak.$(date +%F-%H%M%S)" 2>/dev/null || true
vim "$V2RAY_ETC/config.json"

配置完成后,检查配置文件是否正确,并启动

v2ray test -config "$(brew --prefix)/etc/v2ray/config.json"

# 可以先前台启动 v2ray,确认是否能正常运行:
v2ray run -config "$(brew --prefix)/etc/v2ray/config.json"

前台测试没有问题后,可以使用 Homebrew services 将 v2ray 作为后台服务启动:

brew services start v2ray

# 查看服务状态:
brew services list | grep v2ray

macOS 上没有 Linux 里的 ss 命令,所以使用 lsof 检查端口是否监听:

lsof -nP -iTCP:10808 -sTCP:LISTEN
lsof -nP -iTCP:10809 -sTCP:LISTEN

如果正常,会看到类似结果:

COMMAND   PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
v2ray   26394 bytedance    4u  IPv4 0x47ab7dfa0ce6833a      0t0  TCP 127.0.0.1:10808 (LISTEN)

COMMAND   PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
v2ray   26394 bytedance    6u  IPv4 0xaef33d2ad9595e15      0t0  TCP 127.0.0.1:10809 (LISTEN)

测试 SOCKS5 和 HTTP 代理,如果返回的是你的代理服务器出口 IP,说明代理配置成功

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

最后,macOS 默认 shell 一般是 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=socks5h://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=socks5h://127.0.0.1:10808

  echo "terminal proxy on"
}

proxy_off() {
  unset http_proxy https_proxy all_proxy
  unset HTTP_PROXY HTTPS_PROXY ALL_PROXY

  echo "terminal proxy off"
}

保存后重新加载 source ~/.zshrc

proxy_on
curl https://api.ipify.org
proxy_off

如果后续需要重启或者停止 v2ray,使用

brew services restart v2ray
brew services stop v2ray
May 06, 2025
Dec 31, 2024
Nov 17, 2024