Skip to content

Instantly share code, notes, and snippets.

@eric-gitta-moore
Last active August 10, 2026 03:22
Show Gist options
  • Select an option

  • Save eric-gitta-moore/a1dc14eb1f510ce4839b1e8259a8ad49 to your computer and use it in GitHub Desktop.

Select an option

Save eric-gitta-moore/a1dc14eb1f510ce4839b1e8259a8ad49 to your computer and use it in GitHub Desktop.
Windows / Mac 远程操作主 Mac Codex

Windows / Mac 远程操作主 Mac Codex

目标:主 Mac 只运行一个 app-server,本机 ChatGPT、Codex CLI 和第二台电脑的 ChatGPT 共用它。

主 Mac ChatGPT ── WS :45001 ── socat ───────────────────┐
主 Mac Codex CLI ─────────────── unix:// ────────────────┤
第二端 ChatGPT ── Remote Connections / SSH ── proxy ─────┤
                                                         ▼
                     ~/.codex/app-server-control/app-server-control.sock
                                                         │
                                                         ▼
                              tmux 中唯一的 codex app-server

说明:无需运行 app-server daemon start,也无需在 Windows 安装 standalone Codex。

1. 主 Mac:一次性配置

安装依赖:

brew install tmux socat

给 ChatGPT.app 内置的 Codex 创建 PATH 入口:

mkdir -p ~/.local/bin
ln -sfn /Applications/ChatGPT.app/Contents/Resources/codex ~/.local/bin/codex
ln -sfn /Applications/ChatGPT.app/Contents/Resources/codex-code-mode-host ~/.local/bin/codex-code-mode-host

在 ~/.zshrc 中加入:

export PATH="$HOME/.local/bin:$PATH"
export CODEX_SSH_SKIP_APP_SERVER_BOOT=true

验证 Codex 的真实路径:

zsh -lic 'whence -p codex && exec codex --version'

预期:第一行是 ~/.local/bin/codex。不要使用 alias codex=...,因为 SSH 连接后执行程序时不会展开 alias。

2. 主 Mac:每次启动

创建 tmux 会话:

tmux new -s codex-app-server

在第一个窗口启动 app-server:

/Applications/ChatGPT.app/Contents/Resources/codex -c features.code_mode_host=true app-server --listen unix://

按 Ctrl+B,再按 C 新建第二个窗口,然后启动 WS 转发:

socat TCP-LISTEN:45001,bind=127.0.0.1,reuseaddr,fork UNIX-CONNECT:$HOME/.codex/app-server-control/app-server-control.sock

按 Ctrl+B,再按 D 退出 tmux 界面。完全退出当前已打开的 ChatGPT,然后设置 WS 地址:

launchctl setenv CODEX_APP_SERVER_WS_URL ws://127.0.0.1:45001/rpc

验证变量:

launchctl getenv CODEX_APP_SERVER_WS_URL

预期输出:ws://127.0.0.1:45001/rpc。

打开 ChatGPT:

open -a ChatGPT

说明:在同一 macOS 登录会话内,也可以从 Dock 再次打开 ChatGPT。注销或重启 Mac 后,需要先重新设置该变量。/rpc 与当前 ChatGPT.app 使用的内部地址格式一致。

本机 Codex CLI 需要共用该 app-server 时,运行:

codex --remote unix://

3. 第二台电脑:Remote Connections / SSH

在主 Mac 打开 System Settings → General → Sharing → Remote Login,并配置 SSH 密钥登录。

使用 GOST 增加 SSH 端口(可选)

macOS 的远程登录继续监听 22 端口,由 GOST 监听额外端口并把 TCP 连接转发到本机 OpenSSH:

brew install gost
gost -L "tcp://:2222/127.0.0.1:22"

需要同时开放多个端口时,可以配置多个监听器:

gost \
  -L "tcp://:2222/127.0.0.1:22" \
  -L "tcp://:10022/127.0.0.1:22"

这里的 :2222 会监听主 Mac 的所有网络接口。如果只允许局域网访问,可以改为主 Mac 的局域网地址,例如 tcp://192.168.1.10:2222/127.0.0.1:22。

在主 Mac 检查 GOST 的监听状态:

lsof -nP -iTCP:2222 -sTCP:LISTEN
ssh -p 2222 <macOS 用户名>@127.0.0.1

GOST 需要持续运行,可以放在独立的 tmux 窗口中,或配置为 launchd 服务。跨设备连接时,还需要在 macOS 防火墙、路由器或云网络中放行该端口。SSH 加密和身份认证仍由 OpenSSH 处理。

在第二台电脑编辑 SSH 配置:

  • Windows:%USERPROFILE%\.ssh\config
  • macOS:~/.ssh/config
Host mac-codex
    HostName <主 Mac IP>
    User <macOS 用户名>
    Port 2222
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
    ServerAliveInterval 30
    ServerAliveCountMax 4

在第二台电脑的终端验证连接:

ssh mac-codex "zsh -lic 'whence -p codex && codex --version'"

验证成功后,在第二台电脑的 ChatGPT 打开 Settings → Connections → SSH,选择 mac-codex。

内部行为:ChatGPT 会自动在主 Mac 运行 codex app-server proxy,把 SSH 连接转发到共享 Unix socket。用户无需手动执行该命令。每个远程连接可以有一个 proxy 进程,真正处理任务的 app-server 仍只有一个。

4. 验证

在主 Mac 检查共享 app-server 和 WS 端口:

codex app-server daemon version
lsof -nP -iTCP:45001 -sTCP:LISTEN

5. 排障

第二台电脑出现 socket hang up 时,在主 Mac 检查 Codex 路径和 Unix socket:

zsh -lic 'whence -p codex && exec codex --version'
ls -l ~/.codex/app-server-control/app-server-control.sock

如果 Codex 路径指向 ~/.asdf/shims/codex,或只有 alias codex=...,请重新创建第 1 节中的 ~/.local/bin 软链接,并确保该目录位于 PATH 前部。

6. 停止

先退出两端 ChatGPT 和所有 codex --remote,然后在主 Mac 停止 tmux 会话:

tmux kill-session -t codex-app-server
pgrep -af "codex.*app-server"

使用限制:同一个正在运行的任务只由一端继续输入;另一端等待 turn/completed 后再接手。

兼容性:CODEX_APP_SERVER_WS_URL 和 CODEX_SSH_SKIP_APP_SERVER_BOOT 属于当前 ChatGPT Desktop 的内部实现,ChatGPT 更新后需要重新验证。

参考:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment