How to Use Codex CLI with SSH with ChatGPT subscription

When you log in with ChatGPT with Codex CLI, it expects your system to respond to port 1455. If you are trying to run Codex via SSH on a remote server without a GUI, you cannot log in with the ChatGPT subscription, so it won’t be able to return an OAuth token to port 1455. To solve this, you first create an SSH tunnel. Here is the command to create the tunnel

ssh -N -L 127.0.0.1:1455:127.0.0.1:1455 root@cloud-server-ip

After entering the password, this would sit as it is. Now, on another SSH terminal, run codex login and select ChatGPT subscription. It will then return a URL. Copy the URL to your browser and log in to your ChatGPT Plus account. If everything is going right, this should automatically send the OAuth token through the tunnel to your remote Codex CLI, it would respond by saying:

Successfully logged in

Codex is an excellent improvement on TUI since its inception. Using SSH is great for debugging a production system, doing DevOps work, etc.