To log in to the Codex CLI on a headless server or over SSH, run codex login --device-auth, open the link it prints on any device with a browser, sign in, and enter the one-time code. Device code login is in beta and must first be enabled in your ChatGPT security settings, or by your workspace admin. If you can't use it, forward Codex's callback port with ssh -L 1455:localhost:1455 user@remote and run codex login in that session, or log in on your own computer and copy ~/.codex/auth.json to the server. For automation, an API key avoids the browser altogether.
Everything below follows OpenAI's authentication docs.
01Why the normal login fails on a server
codex login opens a browser, and after you sign in the browser hands your credentials back to Codex through a callback on localhost (port 1455 by default). On a server with no browser, or when you're connected over SSH, that browser and that localhost aren't on the same machine as Codex, so the callback never arrives. Local network settings that block the callback cause the same problem.
02Option 1: device code login (preferred)
- Turn it on first: in your ChatGPT security settings for a personal account, or in ChatGPT workspace permissions if you're a workspace admin.
- On the server, run:
codex login --device-auth
Or start codex login and choose Sign in with Device Code in the login screen.
- Open the link on your laptop or phone, sign in, and enter the one-time code shown in the terminal.
OpenAI labels it beta; if it isn't available to you, use one of the options below.
03Option 2: forward the callback port over SSH
If you can forward ports, the normal browser flow works through a tunnel. From your local machine:
ssh -L 1455:localhost:1455 user@remote
In that SSH session run codex login, then open the address it prints in the browser on your local machine. When you sign in, the browser calls localhost:1455 on your machine, and SSH carries it to Codex on the server.
04Option 3: copy auth.json from a machine with a browser
- On your own computer, run
codex loginand finish the browser flow. - Check that
~/.codex/auth.jsonexists. - Copy it to the same place on the server:
ssh user@remote 'mkdir -p ~/.codex'
scp ~/.codex/auth.json user@remote:~/.codex/auth.json
or in one line without scp:
ssh user@remote 'mkdir -p ~/.codex && cat > ~/.codex/auth.json' < ~/.codex/auth.json
If your computer stores Codex credentials in the OS credential store instead of a file, there may be no auth.json to copy. Switch to file storage (below) on that machine first.
Treat auth.json like a password. It contains access tokens. Don't commit it, paste it into tickets or share it in chat. For ChatGPT sign-ins, Codex refreshes tokens automatically while in use.
05Option 4: an API key
You can sign in with an OpenAI API key instead of ChatGPT, which needs no browser:
printenv OPENAI_API_KEY | codex login --with-api-key
The trade-offs: API usage is billed at standard API rates through your OpenAI Platform account rather than your ChatGPT plan, and some features that rely on ChatGPT workspace access or cloud services are limited or unavailable. OpenAI recommends API keys for programmatic workflows such as CI/CD, and warns against exposing Codex execution in untrusted or public environments. ChatGPT Enterprise workspaces can also issue Codex access tokens for trusted automation (codex login --with-access-token).
06Where Codex keeps your login
The cli_auth_credentials_store setting in ~/.codex/config.toml controls it:
fileauth.json under CODEX_HOME (by default ~/.codex)keyringThe operating system's credential store; fails if there isn't oneautoThe credential store when available, otherwise auth.jsonephemeralMemory only, for the current processcli_auth_credentials_store = "file"
If you copied auth.json to the server, file or auto will use it. In managed environments, an admin can enforce this setting, and you can't override it.
07Check or clear the login
codex login status # shows the active authentication method
codex logout # clears stored credentials
If login keeps failing, direct codex login runs write a codex-login.log file in your configured log directory. Behind a corporate TLS proxy, set CODEX_CA_CERTIFICATE to your CA bundle before logging in.
08After you're logged in
Run Codex inside tmux so it survives a dropped connection, and set up notifications so you know when it needs you: Codex notifications over SSH covers alerts that travel back through the SSH connection and pushes that reach your phone with the laptop shut. Notifier for Codex installs on a Linux server with one command and a 6-digit code from its iPhone or Android app, and lets you approve, answer and follow up from the phone. It is independent and not affiliated with OpenAI.
09FAQ
How do I log in to Codex without a browser?
Run codex login --device-auth and enter the one-time code on any device with a browser. Enable device code login in your ChatGPT security settings first.
Why doesn't codex login work on a remote server?
The browser can't reach Codex's callback on localhost:1455 on the server. Use device code login, or tunnel the port with ssh -L 1455:localhost:1455 user@remote.
Can I copy my Codex login to another machine?
Yes. Copy ~/.codex/auth.json to the same path on the other machine. Keep it private; it contains access tokens.
How do I check which account Codex is logged in with?
Run codex login status to see the active authentication method.