Logging in to GitHub CLI on a Headless VM

by · ·2 min read ·222 words
Table of Contents

I use gh countless times throughout the day, and occasionally I need to log in or re-authenticate from some VM that doesn’t have a GUI. I run gh auth login and, as part of the flow, I inevitably end up with lynx having been fired up. As much as I love lynx, this is kind of annoying. What I really want is GH_BROWSER="echo". That just emits the URL instead of launching lynx, so I can open it locally and get the job done.

featured

"Cobalt RaQ2 Debian" by Fo0bar is licensed under CC BY-SA 3.0. Cropped from the original.

#The script

#!/usr/bin/env bash

set -eu -o pipefail

GH_BROWSER="echo" exec gh auth login \
    --hostname github.com \
    --git-protocol ssh \
    --web \
    --skip-ssh-key \
    "$@"

#The parts

Contains LLM-generated content not written by Olaf
  • GH_BROWSER="echo": gh “opens” the URL by echoing it
    • GH_BROWSER takes precedence over BROWSER (gh help environment)
  • --web: device flow; copy the one-time code, open the URL on another machine
  • --hostname github.com: skips the GitHub.com vs. Enterprise prompt
  • --git-protocol ssh: skips the HTTPS vs. SSH prompt
  • --skip-ssh-key: skips the generate/upload SSH key prompt
  • exec: replace the shell process with gh
  • "$@": pass extra flags through (e.g. --scopes)
// end LLM-generated content

Related posts: