English | 简体中文
An elegant, zero-dependency bash script to seamlessly authenticate the Codex CLI on remote or headless servers using an ephemeral SSH tunnel.
When using Codex CLI on a remote virtual machine or VPS, running codex login requires a local browser to complete the OAuth flow. The authentication callback is sent to http://localhost:1455 on the remote machine. Because your browser is on your local machine, the callback fails, leading to state mismatch errors or "Bad Request" screens.
While there are alternative methods (like exporting your local configuration files into a .tar.gz and SCPing them to the remote server), these methods are cumbersome, manual, and insecure if the tarball is mishandled.
This script automates the native network tunneling approach. It runs entirely on your local machine and does the following in one step:
- Connects to your remote server and cleans up any stalled
codexprocesses. - Silently establishes a background SSH tunnel mapping your local
1455port to the remote1455port. - Automatically triggers
codex loginon the remote server and displays the Authorization URL in your local terminal. - Magic: When you click the URL and authenticate in your browser, the traffic flows securely through the tunnel to the remote server, completing the login.
- Gracefully tears down the SSH tunnel the moment you are done. No lingering background processes, no manual port forwarding commands!
- SSH access to your target remote server.
- The
codexCLI tool installed on the remote server.
Simply download the script to your local machine and make it executable:
git clone https://github.com/codertesla/codex-remote-login.git
cd codex-remote-login
chmod +x codex-remote-login.shRun the script on your local machine, passing the SSH connection details for your remote server:
./codex-remote-login.sh <ssh_target>Using a standard SSH user and IP:
./codex-remote-login.sh ubuntu@192.168.1.100Using an SSH alias defined in your ~/.ssh/config:
./codex-remote-login.sh my-dev-serverPassing an explicit SSH key:
./codex-remote-login.sh -i ~/.ssh/my_private_key root@example.comssh <target> "pkill -f codex": Prevents state mismatch errors caused by previous failed login attempts.ssh -M -S <socket> -fnNT -L 1455:127.0.0.1:1455 <target>: Creates an active background SSH tunnel bound to a UNIX control socket.ssh -t <target> "codex login": Starts the interactive login prompt.trap cleanup EXIT: Catches script termination (successful login orCtrl+C) and commands the SSH control socket to terminate the tunnel.