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

Add function to account authentication without GUI

Open LinCompbio opened this issue 1 year ago • 17 comments

Another choice for large context model integrated coding tool , great !

However I tried in current setting I can't login in with my subscribed account on server without GUI, will U plan to fix it later?

LinCompbio avatar Jun 25 '25 14:06 LinCompbio

This would be huge for me too

brianbest avatar Jun 25 '25 14:06 brianbest

Yeah, currently gemini-cli tries to use xdg-open which requires GUI.

For now you can log in on a Windows machine and copy cached tokens from your local .gemini folder over to the Linux server you want to use them on.

sevosik avatar Jun 25 '25 14:06 sevosik

Ty @sevosik

brianbest avatar Jun 25 '25 14:06 brianbest

If you login with oauth, you can copy ~/.gemini/oauth_creds.json to login

ryanjsalva avatar Jun 25 '25 14:06 ryanjsalva

If you login with oauth, you can copy ~/.gemini/oauth_creds.json to login

Thanks, this works.

LinCompbio avatar Jun 25 '25 14:06 LinCompbio

This is a known limitation documented in authentication.md:

Note that the web login must be done in a browser that can communicate with the machine Gemini CLI is being run from. (Specifically, the browser will be redirected to a localhost url that Gemini CLI will be listening on).

We do plan on allowing this soon, however.

scidomino avatar Jun 25 '25 14:06 scidomino

This is blocking Firebase Studio customers from being able to use the CLI in our code view. We'd really love it if this could be prioritized to unblock this in-browser terminal experience.

LyalinDotCom avatar Jun 25 '25 14:06 LyalinDotCom

This is a known limitation documented in authentication.md:

Note that the web login must be done in a browser that can communicate with the machine Gemini CLI is being run from. (Specifically, the browser will be redirected to a localhost url that Gemini CLI will be listening on).

We do plan on allowing this soon, however.

This could use some clarification. The above is true when running in a devcontainer for example, but the error still happens.

regner avatar Jun 25 '25 14:06 regner

This issue is a big deal. It's incredibly difficult to login right now on a linux server. I've spent the last 30 minutes trying to get it working with no success.

jamespinkerton avatar Jun 25 '25 15:06 jamespinkerton

I was able to patch my way around this with the following:

Create an xdg-open stub that logs URLs instead and put it in $PATH

#!/bin/bash
# xdg-open stub installer for logging URLs instead of opening them

STUB_DIR="$HOME/xdg-stub"
LOG_FILE="$HOME/xdg-open-urls.log"

mkdir -p "$STUB_DIR"

cat > "$STUB_DIR/xdg-open" <<EOF
#!/bin/bash
echo "\$(date +'%Y-%m-%d %H:%M:%S')  \$1" >> "$LOG_FILE"
exit 0
EOF

chmod +x "$STUB_DIR/xdg-open"

if [[ ":$PATH:" != *":$STUB_DIR:"* ]]; then
  export PATH="$STUB_DIR:\$PATH"
  echo "PATH modified. You may want to add this line to your shell profile:"
  echo "  export PATH=\"$STUB_DIR:\$PATH\""
fi

echo "Stub installed. URLs will be logged to: $LOG_FILE"

Run gemini-cli

$ npx https://github.com/google-gemini/gemini-cli

Attempt a Google account login in gemini-cli

You might need to run /auth to reselect

View the log file in a separate SSH session/tab and click the latest link

cat ~/xdg-open-urls.log

Log in to a Google account (make sure not to select a Workspace account)

You'll see an error from your browser since it can't connect to localhost - that's expected. Determine what port number it's trying to connect to from the URL bar, and note that down

Add an SSH port-forward for that port

Press Enter-Enter, then ~ then C in an SSH session and you'll drop to the SSH commandline For me the port number was 45265, so let's use that: Enter -L 45265:localhost:45265 then strike Enter twice

Reload the browser OAuth2 callback page, you're now logged in!

There is a timeout, so don't take too much time running through this. :)

MUSTARDTIGERFPV avatar Jun 25 '25 16:06 MUSTARDTIGERFPV

I'm not sure why, but I'm getting connection failed: Connection refused on my tunnel logs and browser still shows ERR_CONNECTION_RESET

cyb0rgk1tty avatar Jun 25 '25 18:06 cyb0rgk1tty

A quick workaround for those like me who dev in docker container (on your host), the quick workaround is to login once on you host

npx https://github.com/google-gemini/gemini-cli 

then mount or copy your host ~/.gemini/ to your container.

For me it looks like this in my docker-compose:

services
  dev-env:
    build: 
      # ...
    volumes:
      # ...
      - ~/.gemini:/root/.gemini    # Gemini CLI cache persistence

PaulSenon avatar Jun 26 '25 09:06 PaulSenon

@MUSTARDTIGERFPV In your Create an xdg-open stub that logs URLs instead and put it in $PATH step, change

export PATH="$STUB_DIR:\$PATH" to export PATH="$STUB_DIR:$PATH"

And then your script works for me. However, I'm still stuck on the "Waiting for auth..." screen.

seanghaeli avatar Jun 26 '25 22:06 seanghaeli

I'm not sure why, but I'm getting connection failed: Connection refused on my tunnel logs and browser still shows ERR_CONNECTION_RESET

I had the same issue. I worked out that I was using the wrong url. I was using the localhost:xxxxx url instead of the https://accounts.google.com that gemini gave me. Once I used that (after doing ssh command), it worked.

lockyt avatar Jun 28 '25 00:06 lockyt

I ran into this issue and just replaced the localhost part of the second URL with the IP of my headless server and the authentication went through.

xxdoxedxx avatar Jul 01 '25 16:07 xxdoxedxx

Is there a plan to fix Gemini-cli so that workarounds are not necessary on headless or remote environments?

mikecaronna avatar Jul 11 '25 19:07 mikecaronna

I was able to patch my way around this with the following:

Create an xdg-open stub that logs URLs instead and put it in $PATH

#!/bin/bash

xdg-open stub installer for logging URLs instead of opening them

STUB_DIR="$HOME/xdg-stub" LOG_FILE="$HOME/xdg-open-urls.log"

mkdir -p "$STUB_DIR"

cat > "$STUB_DIR/xdg-open" <<EOF #!/bin/bash echo "$(date +'%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE" exit 0 EOF

chmod +x "$STUB_DIR/xdg-open"

if [[ ":$PATH:" != ":$STUB_DIR:" ]]; then export PATH="$STUB_DIR:$PATH" echo "PATH modified. You may want to add this line to your shell profile:" echo " export PATH="$STUB_DIR:$PATH"" fi

echo "Stub installed. URLs will be logged to: $LOG_FILE"

Run gemini-cli

$ npx https://github.com/google-gemini/gemini-cli

Attempt a Google account login in gemini-cli

You might need to run /auth to reselect

View the log file in a separate SSH session/tab and click the latest link

cat ~/xdg-open-urls.log

Log in to a Google account (make sure not to select a Workspace account)

You'll see an error from your browser since it can't connect to localhost - that's expected. Determine what port number it's trying to connect to from the URL bar, and note that down

Add an SSH port-forward for that port

Press Enter-Enter, then ~ then C in an SSH session and you'll drop to the SSH commandline For me the port number was 45265, so let's use that: Enter -L 45265:localhost:45265 then strike Enter twice

Reload the browser OAuth2 callback page, you're now logged in!

There is a timeout, so don't take too much time running through this. :)

Thanks, this worked at my Linux Server, add the gemini api key on .bashrc

IhsanFikri avatar Jul 16 '25 03:07 IhsanFikri