Authentication fails on remote servers/headless environments without browser access
Description
When using Gemini CLI on a remote server via SSH (or any headless environment), the Google OAuth authentication flow fails because it requires opening a browser window that doesn't exist in these environments.
Steps to Reproduce
- SSH into a remote server that doesn't have a GUI/browser
- Install and run Gemini CLI
- Run
/authcommand - Select "Login with Google"
- The tool shows "Waiting for auth... (Press ESC to cancel)" but cannot complete authentication
Expected Behavior
The authentication should work in headless environments, similar to other CLI tools that provide:
- A URL that can be copied and opened in a browser on a different machine
- Device code flow where user gets a code to enter at a specific URL
- Alternative authentication methods like API keys for programmatic use
Actual Behavior
- Authentication gets stuck at "Waiting for auth..."
- No browser URL is provided to manually copy/paste
- No alternative authentication method available for headless environments
Environment
- OS: Linux (remote server via SSH)
- Terminal: SSH session without X11 forwarding
- Gemini CLI Version: [latest]
Suggested Solutions
- Device Code Flow: Implement OAuth device code flow that displays a URL and code for manual browser authentication
- URL Display: Show the authentication URL in the terminal so users can manually copy it to a browser
- API Key Support: Allow authentication using Google AI Studio API keys as an alternative
- Config File Auth: Support reading credentials from a config file for automated/scripted usage
Similar Issues in Other Tools
Many CLI tools have solved this problem:
gcloud auth login --no-browser(Google Cloud CLI)gh auth login(GitHub CLI) - provides device code flowaz login --use-device-code(Azure CLI)
Additional Context
This is blocking usage of Gemini CLI in common development scenarios like:
- Remote development servers
- CI/CD pipelines
- Docker containers
- Headless Linux systems
- SSH-only access environments
The current browser-based flow works fine for local development but makes the tool unusable in many production and remote development scenarios.
Workaround needed: Is there currently any way to authenticate Gemini CLI in headless environments that I'm missing?
I'm having the same issue, and the verification keeps staying:
... Waiting for auth... (Press ESC to cancel)
And then it failed
我遇到了同样的问题,验证一直停留在: ...等待授权...(按 ESC 取消) 然后就失败了 me too
@Mox301 Same issue
I'm facing same issue now. I hope that the URL for authentication going to be gave. ☕
the auth callback can not open successfully
same😔
I got the authentication URL with --debug option! :tada:
$ gemini --debug
...
...
...
Flushing log events to Clearcut.
███ █████████ ██████████ ██████ ██████ █████ ██████ █████ █████
░░░███ ███░░░░░███░░███░░░░░█░░██████ ██████ ░░███ ░░██████ ░░███ ░░███
░░░███ ███ ░░░ ░███ █ ░ ░███░█████░███ ░███ ░███░███ ░███ ░███
░░░███ ░███ ░██████ ░███░░███ ░███ ░███ ░███░░███░███ ░███
███░ ░███ █████ ░███░░█ ░███ ░░░ ░███ ░███ ░███ ░░██████ ░███
███░ ░░███ ░░███ ░███ ░ █ ░███ ░███ ░███ ░███ ░░█████ ░███
███░ ░░█████████ ██████████ █████ █████ █████ █████ ░░█████ █████
░░░ ░░░░░░░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░
Tips for getting started:
1. Ask questions, edit files, or run commands.
2. Be specific for the best results.
3. Create GEMINI.md files to customize your interactions with Gemini.
4. /help for more information.
Code Assist login required.
Attempting to open authentication page in your browser.
Otherwise navigate to:
https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX
Waiting for authentication...
Clearcut response: { nextRequestWaitMs: 900000 }
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ ⠼ Waiting for auth... (Press ESC to cancel) │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Part of the issue im having is the port for the oauth server it spins up uses a dynamic port. Which also means it's tricky to open that port up in the docker-compose file due to the fact it's dynamic.
So when the callback happens after you sign in, you will see
can’t establish a connection to the server at localhost:42761 or something similar.
Not sure if its possible to pin the port?
Solving Google Code Assist Authentication on Remote Machines
When you're connected to a remote server via SSH and trying to authenticate Google Code Assist (e.g., by running gemini --debug), if the authentication requires a callback to localhost, you'll need to use SSH Port Forwarding.
Step 1: Obtain the Authentication URL and Callback Port
- Run
gemini --debugon your remote server. - Locate the authentication URL in the output, which will look something like
https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX. - Copy this URL.
- Parse the
redirect_uriparameter to identify the port number afterlocalhost. For example, ifredirect_uriishttp://localhost%3A21165%2Foauth2callback, the port is21165.
Step 2: Establish SSH Local Port Forwarding
On your local machine's terminal, execute the following command:
ssh -L 21165:0.0.0.0:21165 tk
21165(first instance): This is the port on your local machine that will listen for the callback.0.0.0.0: This is the remote server's address, pointing to the remote server itself.21165(second instance): This is the port on the remote server, which must match the callback port you identified.tk: This is your SSH alias or the remote server's address (e.g.,user@your_remote_server_ip).
Keep this SSH connection active; do not close the terminal window.
Step 3: Complete Authentication
- Open the full authentication URL from Step 1 in your local machine's web browser.
- Complete the Google authentication process.
The authorization code will be forwarded through the SSH tunnel to the gemini client on your remote server, completing the authentication.
Once authentication is complete, you can close the SSH connection.
I using the terminal in vscode and forward the port, it can open the browser in local but stucks in "http://localhost:40063/oauth2callback?xxx", why?
I got the authentication URL with
--debugoption! 🎉$ gemini --debug ... ... ... Flushing log events to Clearcut. ███ █████████ ██████████ ██████ ██████ █████ ██████ █████ █████ ░░░███ ███░░░░░███░░███░░░░░█░░██████ ██████ ░░███ ░░██████ ░░███ ░░███ ░░░███ ███ ░░░ ░███ █ ░ ░███░█████░███ ░███ ░███░███ ░███ ░███ ░░░███ ░███ ░██████ ░███░░███ ░███ ░███ ░███░░███░███ ░███ ███░ ░███ █████ ░███░░█ ░███ ░░░ ░███ ░███ ░███ ░░██████ ░███ ███░ ░░███ ░░███ ░███ ░ █ ░███ ░███ ░███ ░███ ░░█████ ░███ ███░ ░░█████████ ██████████ █████ █████ █████ █████ ░░█████ █████ ░░░ ░░░░░░░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ Tips for getting started: 1. Ask questions, edit files, or run commands. 2. Be specific for the best results. 3. Create GEMINI.md files to customize your interactions with Gemini. 4. /help for more information. Code Assist login required. Attempting to open authentication page in your browser. Otherwise navigate to: https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX Waiting for authentication... Clearcut response: { nextRequestWaitMs: 900000 } ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ ⠼ Waiting for auth... (Press ESC to cancel) │ │ │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
I've succeeded! Thanks for the ideas.
I using the terminal in vscode and forward the port, it can open the browser in local but stucks in "http://localhost:40063/oauth2callback?xxx", why?
sorry, this can work, it's my network problem. i think this is the simplest way to login.
the auth callback can not open successfully
if your browser use proxy, the terminal need use proxy too.
I've successed.
Solving Google Code Assist Authentication on Remote Machines
When you're connected to a remote server via SSH and trying to authenticate Google Code Assist (e.g., by running
gemini --debug), if the authentication requires a callback tolocalhost, you'll need to use SSH Port Forwarding.Step 1: Obtain the Authentication URL and Callback Port
- Run
gemini --debugon your remote server.- Locate the authentication URL in the output, which will look something like
https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX.- Copy this URL.
- Parse the
redirect_uriparameter to identify the port number afterlocalhost. For example, ifredirect_uriishttp://localhost%3A21165%2Foauth2callback, the port is21165.Step 2: Establish SSH Local Port Forwarding
On your local machine's terminal, execute the following command:
ssh -L 21165:0.0.0.0:21165 tk
21165(first instance): This is the port on your local machine that will listen for the callback.0.0.0.0: This is the remote server's address, pointing to the remote server itself.21165(second instance): This is the port on the remote server, which must match the callback port you identified.tk: This is your SSH alias or the remote server's address (e.g.,user@your_remote_server_ip).Keep this SSH connection active; do not close the terminal window.
Step 3: Complete Authentication
- Open the full authentication URL from Step 1 in your local machine's web browser.
- Complete the Google authentication process.
The authorization code will be forwarded through the SSH tunnel to the
geminiclient on your remote server, completing the authentication.Once authentication is complete, you can close the SSH connection.
Tried this method and it didn't work for me
the auth callback can not open successfully
if your browser use proxy, the terminal need use proxy too.
I've successed.
grate! it work for me,
the auth callback can not open successfully
if your browser use proxy, the terminal need use proxy too.
I've successed.
If you are using a VPN, enable the global routing mode instead of the rule-based mode to ensure that all traffic goes through the proxy. 🛫🎯My issue was resolved this way.
Solving Google Code Assist Authentication on Remote Machines
When you're connected to a remote server via SSH and trying to authenticate Google Code Assist (e.g., by running
gemini --debug), if the authentication requires a callback tolocalhost, you'll need to use SSH Port Forwarding.Step 1: Obtain the Authentication URL and Callback Port
- Run
gemini --debugon your remote server.- Locate the authentication URL in the output, which will look something like
https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX.- Copy this URL.
- Parse the
redirect_uriparameter to identify the port number afterlocalhost. For example, ifredirect_uriishttp://localhost%3A21165%2Foauth2callback, the port is21165.Step 2: Establish SSH Local Port Forwarding
On your local machine's terminal, execute the following command: ssh -L 21165:0.0.0.0:21165 tk
21165(first instance): This is the port on your local machine that will listen for the callback.0.0.0.0: This is the remote server's address, pointing to the remote server itself.21165(second instance): This is the port on the remote server, which must match the callback port you identified.tk: This is your SSH alias or the remote server's address (e.g.,user@your_remote_server_ip).Keep this SSH connection active; do not close the terminal window.
Step 3: Complete Authentication
- Open the full authentication URL from Step 1 in your local machine's web browser.
- Complete the Google authentication process.
The authorization code will be forwarded through the SSH tunnel to the
geminiclient on your remote server, completing the authentication. Once authentication is complete, you can close the SSH connection.Tried this method and it didn't work for me
![]()
"216.239.34.223 is a Google IP address; both the local machine and the remote server also need to be able to access Google."
Hey team - we're tracking this over in #1439 if you want to find a workaround there, or discuss the specific issue more
I ran into this issue and just replaced the localhost part of the URL with the IP of my headless server and the authentication went through.
In fact, there is an easy way , use VS Code, ssh to hoast, open the terminal panel in VSCode, launch the gemini in terminal panel, since VS Code will setup a port mapping to remote, so your local PC will popup a browser when Gemini request auth..