在 Ubuntu Server 設定 Codex CLI Remote Control
本文說明如何在 Ubuntu Server 安裝 Codex CLI、登入 ChatGPT、啟動 Remote Control daemon,並產生配對碼。codex remote-control 仍是實驗性功能,指令與使用資格可能隨版本、帳號或 workspace 政策變動。
前置條件
開始前,確認環境符合以下條件:
- Ubuntu Server 可連線至外部 HTTPS 網站
- 目前使用者具有
sudo權限,或系統已安裝curl與 CA certificates - ChatGPT 帳號支援 Codex,組織管理員也已開放 Remote Control
- Ubuntu Server 與控制端使用相同的 ChatGPT 帳號及 workspace
安裝必要套件:
sudo apt-get update sudo apt-get install -y curl ca-certificates
Remote Control 透過安全 relay 連線,不需將 app-server 或 Secure Shell (SSH) port 暴露到公網。
1. 安裝 Codex CLI
執行官方 standalone installer:
curl -fsSL https://chatgpt.com/codex/install.sh | sh
將預設安裝目錄加入 PATH:
export PATH="$HOME/.local/bin:$PATH" grep -qxF 'export PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" || \ printf '%s\n' 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc" source "$HOME/.bashrc" codex --version
2. 登入 ChatGPT
Ubuntu Server 通常沒有桌面瀏覽器,請使用 device-code authentication:
codex login --device-auth
- 在電腦或手機開啟終端顯示的網址
- 登入 ChatGPT 並選擇正確的 workspace
- 輸入一次性 code,完成授權
回到 Ubuntu 終端並確認登入狀態:
codex login status
Remote Control 應使用 ChatGPT 帳號登入。API key 可能無法提供完整功能。
3. 啟動 Remote Control
先確認目前版本支援的指令:
codex remote-control --help
啟動 daemon:
codex remote-control start
若要以前景模式查看輸出,改執行 codex remote-control。按 Ctrl+C 可結束程序。
4. 產生配對碼
daemon 啟動後,產生短效配對碼:
codex remote-control pair
立即在支援 Remote Control 的 client 或 ChatGPT 配對流程輸入配對碼。配對碼過期時,重新執行相同指令。請勿將配對碼貼到公開聊天室、issue 或 log。
若手機 App 沒有 Remote 選項,請更新 App,並檢查帳號資格、workspace 政策與功能 rollout。
日常操作
常用指令如下:
codex remote-control start # 啟動 daemon codex remote-control pair # 產生新配對碼 codex remote-control stop # 停止 daemon
疑難排解
以下項目涵蓋常見的安裝、版本與登入問題。
找不到 codex 指令
重新加入安裝路徑:
export PATH="$HOME/.local/bin:$PATH" command -v codex
找不到 remote-control 子命令
更新 CLI 後再次檢查:
codex update codex --version codex remote-control --help
若更新後仍找不到,代表目前版本、帳號 rollout 或 workspace 政策尚未提供此功能。
Headless 登入卡住
重新執行 codex login --device-auth。若公司網路使用 Transport Layer Security (TLS) inspection,請依組織提供的 CA bundle 設定:
export CODEX_CA_CERTIFICATE=/path/to/corporate-root-ca.pem codex login --device-auth
配對後看不到 Ubuntu host
依序確認以下項目:
- Ubuntu 與控制端使用相同的 ChatGPT 帳號及 workspace
codex login status顯示有效登入codex remote-control start已執行- 配對碼尚未過期
- 組織管理員已允許 Remote Control
- Ubuntu Server 可連線至外部 HTTPS 網站
安全注意事項
Remote session 會沿用 Ubuntu host 上的檔案、憑證、權限、工具與 sandbox 設定。請遵守以下原則:
- 使用低權限 Linux 帳號執行 Codex,避免使用
root - 不要公開
~/.codex/auth.json、access token 或 pairing code - 不要將 app-server WebSocket 或本機 port 暴露到公網
- 停用時執行
codex remote-control stop,並從 client 移除舊裝置
Remote Control 不會略過 command approvals 或 sandbox;原有安全控制仍然有效。
官方資料
使用 systemd user service 在背景執行
若要讓 Remote Control 在 SSH 中斷後繼續執行,可建立 systemd user service。以下做法參考 Reddit 社群分享,並使用前景指令,讓 systemd 直接監管程序。
先確認 Codex 的安裝位置:
command -v codex
建立 user service 與 log 目錄:
mkdir -p "$HOME/.config/systemd/user" "$HOME/.codex"
建立 ~/.config/systemd/user/codex-remote-control.service,內容如下:
[Unit] Description=Codex remote control bridge After=network-online.target [Service] Type=simple WorkingDirectory=/home/USERNAME Environment=PATH=/home/USERNAME/.local/bin:/usr/local/bin:/usr/bin:/bin ExecStart=/home/USERNAME/.local/bin/codex remote-control Restart=always RestartSec=5 StandardOutput=append:/home/USERNAME/.codex/remote-control.log StandardError=append:/home/USERNAME/.codex/remote-control.log [Install] WantedBy=default.target
請將範本中的 USERNAME 替換成實際的 Linux 使用者名稱。ExecStart 使用前景模式的 codex remote-control,讓 systemd 持續監管程序。
載入、啟用並立即啟動 service:
systemctl --user daemon-reload systemctl --user enable --now codex-remote-control.service systemctl --user status codex-remote-control.service