gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Authentication fails on remote servers/headless environments without browser access

Open Mox301 opened this issue 1 year ago • 28 comments

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

  1. SSH into a remote server that doesn't have a GUI/browser
  2. Install and run Gemini CLI
  3. Run /auth command
  4. Select "Login with Google"
  5. 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

  1. Device Code Flow: Implement OAuth device code flow that displays a URL and code for manual browser authentication
  2. URL Display: Show the authentication URL in the terminal so users can manually copy it to a browser
  3. API Key Support: Allow authentication using Google AI Studio API keys as an alternative
  4. 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 flow
  • az 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?

Mox301 avatar Jun 26 '25 01:06 Mox301

I'm having the same issue, and the verification keeps staying:
... Waiting for auth... (Press ESC to cancel)
And then it failed

hwb96 avatar Jun 26 '25 01:06 hwb96

我遇到了同样的问题,验证一直停留在: ...等待授权...(按 ESC 取消) 然后就失败了 me too

yy1234 avatar Jun 26 '25 01:06 yy1234

@Mox301 Same issue

auth bug

samucodesh avatar Jun 26 '25 01:06 samucodesh

I'm facing same issue now. I hope that the URL for authentication going to be gave. ☕

ytnobody avatar Jun 26 '25 01:06 ytnobody

Image

the auth callback can not open successfully

fayewang2014 avatar Jun 26 '25 02:06 fayewang2014

same😔

zttiannnn avatar Jun 26 '25 02:06 zttiannnn

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)                                                                                                                                                                                                                                       │
│                                                                                                                                                                                                                                                                                   │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

ytnobody avatar Jun 26 '25 02:06 ytnobody

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?

Confidenceman02 avatar Jun 26 '25 03:06 Confidenceman02


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

  1. Run gemini --debug on your remote server.
  2. Locate the authentication URL in the output, which will look something like https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX.
  3. Copy this URL.
  4. Parse the redirect_uri parameter to identify the port number after localhost. For example, if redirect_uri is http://localhost%3A21165%2Foauth2callback, the port is 21165.

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

  1. Open the full authentication URL from Step 1 in your local machine's web browser.
  2. 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.

ylck avatar Jun 26 '25 03:06 ylck

Image 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?

maokangkun avatar Jun 26 '25 03:06 maokangkun

I got the authentication URL with --debug option! 🎉

$ 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.

ylck avatar Jun 26 '25 03:06 ylck

Image 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.

maokangkun avatar Jun 26 '25 03:06 maokangkun

Image

the auth callback can not open successfully

if your browser use proxy, the terminal need use proxy too.

I've successed.

fayewang2014 avatar Jun 26 '25 03:06 fayewang2014

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

  1. Run gemini --debug on your remote server.
  2. Locate the authentication URL in the output, which will look something like https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX.
  3. Copy this URL.
  4. Parse the redirect_uri parameter to identify the port number after localhost. For example, if redirect_uri is http://localhost%3A21165%2Foauth2callback, the port is 21165.

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

  1. Open the full authentication URL from Step 1 in your local machine's web browser.
  2. 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.

Tried this method and it didn't work for me

Image

hwb96 avatar Jun 26 '25 03:06 hwb96

Image 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,

yy1234 avatar Jun 26 '25 03:06 yy1234

Image 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.

hwb96 avatar Jun 26 '25 03:06 hwb96

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

  1. Run gemini --debug on your remote server.
  2. Locate the authentication URL in the output, which will look something like https://accounts.google.com/o/oauth2/v2/auth?XXXXXXXXXXX.
  3. Copy this URL.
  4. Parse the redirect_uri parameter to identify the port number after localhost. For example, if redirect_uri is http://localhost%3A21165%2Foauth2callback, the port is 21165.

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

  1. Open the full authentication URL from Step 1 in your local machine's web browser.
  2. 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.

Tried this method and it didn't work for me

Image

"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."

ylck avatar Jun 26 '25 03:06 ylck

Hey team - we're tracking this over in #1439 if you want to find a workaround there, or discuss the specific issue more

markmcd avatar Jun 26 '25 04:06 markmcd

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.

xxdoxedxx avatar Jul 01 '25 16:07 xxdoxedxx

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..

checko avatar Jul 02 '25 05:07 checko