Codex 局域网远程使用指南:macOS 服务端 + SSH 端口转发方案

本文提供了一种安全、稳定的方式,在局域网内让多台客户端(macOS、Windows、Ubuntu)通过 SSH 端口转发连接到 macOS 服务端的 Codex App Server。

一、服务端配置(macOS)

  1. 启用 SSH 服务(远程登录)

    • 打开「系统设置」→「通用」→「共享」
    • 开启「远程登录」(Remote Login)
    • 建议设置为「仅限特定用户」,并添加当前用户
  2. 创建 Codex 专用 authorized_keys 文件

    1
    2
    3
    4
    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    touch ~/.ssh/authorized_keys_codex
    chmod 600 ~/.ssh/authorized_keys_codex
  3. 生成 ws-token.txt(如果尚未生成)

    1
    2
    3
    mkdir -p ~/.codex
    openssl rand -hex 32 > ~/.codex/ws-token.txt
    chmod 600 ~/.codex/ws-token.txt
  4. 启动 Codex App Server(推荐使用 tmux 或 screen 后台运行)

    1
    2
    3
    4
    codex app-server \
    --listen ws://127.0.0.1:8899 \
    --ws-auth capability-token \
    --ws-token-file "$HOME/.codex/ws-token.txt"

二、客户端配置

注意:每个客户端都需要独立生成自己的 SSH 密钥对。

1. macOS 客户端

步骤 1:生成 SSH 密钥对

1
ssh-keygen -t ed25519 -C "codex-macos-client"

步骤 2:将公钥添加到服务端

1
ssh-copy-id -f -i ~/.ssh/id_ed25519.pub yourusername@192.168.0.179

(之后在服务端执行:cat ~/.ssh/authorized_keys >> ~/.ssh/authorized_keys_codex

步骤 3:创建 SSH 配置

~/.ssh/config 中添加:

1
2
3
4
5
6
Host codex-server
HostName 192.168.0.179
User yourusername
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
LocalForward 8899 127.0.0.1:8899

步骤 4:日常使用

  • 终端1(建立转发,保持打开):

    1
    ssh -N codex-server
  • 终端2(启动 Codex):

    1
    2
    3
    4
    5
    6
    # 设置环境变量 CODEX_REMOTE_TOKEN
    export CODEX_REMOTE_TOKEN=$(cat ~/.codex/ws-token.txt)

    # 启动 Codex
    codex --remote ws://localhost:8899 \
    --remote-auth-token-env CODEX_REMOTE_TOKEN

2. Windows 客户端

步骤 1:生成 SSH 密钥对

在 PowerShell 中执行:

1
ssh-keygen -t ed25519 -C "codex-windows-client"

步骤 2:复制公钥到服务端

在客户端执行 type $env:USERPROFILE\.ssh\id_ed25519.pub,复制输出的内容。

在服务端 macOS 执行:

1
cat >> ~/.ssh/authorized_keys_codex

(粘贴公钥内容后,按 Ctrl + D 保存)

步骤 3:创建 SSH 配置

新建文件 C:\Users\你的用户名\.ssh\config,写入:

1
2
3
4
5
6
Host codex-server
HostName 192.168.0.179
User yourusername
IdentityFile C:/Users/你的用户名/.ssh/id_ed25519
IdentitiesOnly yes
LocalForward 8899 127.0.0.1:8899

步骤 4:日常使用

  • PowerShell 窗口1(建立转发):

  • 1
    ssh -N codex-server
  • PowerShell 窗口2(启动 Codex):

  • 1
    2
    3
    4
    5
    # 设置环境变量 CODEX_REMOTE_TOKEN
    $env:CODEX_REMOTE_TOKEN = (Get-Content -Raw "$env:USERPROFILE\.codex\ws-token.txt").Trim()

    # 启动 Codex
    codex --remote ws://localhost:8899 --remote-auth-token-env CODEX_REMOTE_TOKEN

3. Ubuntu 客户端

操作与 macOS 客户端几乎一致:

生成密钥

1
ssh-keygen -t ed25519 -C "codex-ubuntu-client"

添加公钥:使用 ssh-copy-id 或手动复制到服务端的 authorized_keys_codex

SSH 配置~/.ssh/config):

1
2
3
4
5
6
Host codex-server
HostName 192.168.0.179
User yourusername
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
LocalForward 8899 127.0.0.1:8899

日常使用

  • 建立转发:ssh -N codex-server
  • 启动 Codex:
  • 1
    2
    3
    4
    5
    6
    # 设置环境变量 CODEX_REMOTE_TOKEN
    export CODEX_REMOTE_TOKEN=$(cat ~/.codex/ws-token.txt)

    # 启动 Codex
    codex codex --remote ws://localhost:8899 \
    --remote-auth-token-env CODEX_REMOTE_TOKEN

使用建议

  • 服务端建议使用 tmux 让 app-server 在后台运行。
  • 客户端可将 ssh -N codex-server 命令加入快捷方式或脚本。
  • 如果出现 thread/start failed,建议在客户端重置 ~/.codex 目录后再尝试。


Codex 局域网远程使用指南:macOS 服务端 + SSH 端口转发方案
https://jycpp.github.io/2026/26-05-01-Codex局域网远程使用指南.html
作者
Jet Yan
发布于
2026年5月1日
许可协议