Skip to content

Login with a web browser on headless machine #5500

Closed

Description

@pdostal

Describe the feature or problem you’d like to solve

If I have no browser installed I should still be able to run gh auth login and select Login with a web browser as I can open the URL on another machine.

Proposed solution

$ gh auth login
? What account do you want to log into? GitHub.com
- Logging into github.com
? How would you like to authenticate? Login with a web browser

! First copy your one-time code: XXXX-XXXX
No browser detected, please visit following URL:
  https://github.com/cli-auth

How will it benefit CLI and its users?

A lot of people use headless machines to do their work. This would make the login process simplier for those.

Additional context

The current status looks like this:

$ gh auth login
? What account do you want to log into? GitHub.com
- Logging into github.com
? How would you like to authenticate? Login with a web browser

! First copy your one-time code: 36A4-6DE3
- Press Enter to open github.com in your browser... 
/usr/bin/xdg-open: line 871: www-browser: command not found
/usr/bin/xdg-open: line 871: links2: command not found
/usr/bin/xdg-open: line 871: elinks: command not found
/usr/bin/xdg-open: line 871: links: command not found
/usr/bin/xdg-open: line 871: lynx: command not found

Activity

  1. added
    enhancement
    on Apr 22, 2022
  2. added
    needs-triage
    on Apr 22, 2022
  3. mislav commented on Apr 22, 2022

    @mislav
    Contributor

    Hi, if xdg-open returns a nonzero exit status upon failure (as it should), then that will trigger this error condition in gh, and it will print the URL so you can open it on any computer with a web browser:

    if err := browser.Browse(url); err != nil {
    fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", cs.Red("!"), url)
    fmt.Fprintf(w, " %s\n", err)
    fmt.Fprint(w, " Please try entering the URL in your browser manually\n")
    }

    Did that not happen to you? Can you check the exit status of xdg-open and see if you're running an up-to-date GitHub CLI?

    You can also try setting BROWSER=false gh auth login to make it known that this is a headless environment.

  4. pdostal commented on Apr 23, 2022

    @pdostal
    ContributorAuthor

    The BROWSER=false parameter works for me but it's not very straightforward. The output looks like:

    $ BROWSER=false gh auth login
    ? What account do you want to log into? GitHub.com
    - Logging into github.com
    ? How would you like to authenticate? Login with a web browser
    
    ! First copy your one-time code: C22A-D9BC
    - Press Enter to open github.com in your browser... 
    ! Failed opening a web browser at https://github.com/login/device
      exit status 1
      Please try entering the URL in your browser manually
    

    The xdg-open 'http://github.com/' command will open the page in w3m web browser for me.

    I use this machine mainly in headless mode via SSH but it has basic graphical environment installed as well.
    What about modifying the dialog by adding third option? So it would look like:

    $ gh auth login
    ? What account do you want to log into? GitHub.com
    - Logging into github.com
    ? How would you like to authenticate?  [Use arrows to move, type to filter]
    > Login with a web browser
      Login with an external web browser
      Paste an authentication token
    
  5. rpadaki commented on Apr 30, 2022

    @rpadaki
    Contributor

    Alternatively, could we always print the target URL https://github.com/login/device when opening in a web browser? So maybe a less-clunky version of the below:

    ! First copy your one-time code: C22A-D9BC
    - Press Enter to open github.com in your browser,
      or manually go to https://github.com/login/device
  6. added
    core
    and removed
    needs-triage
    on May 4, 2022
  7. mislav commented on May 4, 2022

    @mislav
    Contributor

    @pdostal I like your proposal. I would reword the options to be more clear about where the browser in question runs:

    ? How would you like to authenticate?  [Use arrows to move, type to filter]
    > Login with a web browser on this machine
      Login with a web browser on a different machine
      Paste an authentication token
    

    @rpadaki That's also an interesting idea, but I'm gravitating to the above flow because it requires more explicit intention from the user.

  8. ssbarnea commented on Jun 21, 2022

    @ssbarnea

    As long the full url is displayed we should be fine. IMHO, even Press Enter to open https://github.com/login/device in your browser would suffice.

    No need to be too smart (detect) about it as long the message is smart-enough to allow the user to go manual.

  9. callumfrederiksen commented on Jul 5, 2022

    @callumfrederiksen

    I think that this is something that GitHub themselves may have to address since they are the ones hosting the site.

    However, doing something like this may work using an api to an external web browser accessible on any device. This could potentially be done through the use of HTTP[S] GET/POST requests if executed in the right manner with security issues in mind if directly linked to an account.

    Or you could do a command line login and not JUST an auth token?

  10. ulfjack commented on Mar 6, 2023

    @ulfjack
    Contributor

    This is also broken for me. My default browser is Chrome on Ubuntu, and I have multiple Chrome profiles. It pops open a profile selector, but the URL doesn't actually load after selecting a profile.

  11. mislav commented on Mar 7, 2023

    @mislav
    Contributor

    It pops open a profile selector, but the URL doesn't actually load after selecting a profile.

    That sounds like a problem with your browser, and not something that our tool should necessarily solve?

    You could always try passing additional Chrome parameters in GH_BROWSER:

    GH_BROWSER='google-chrome-stable --profile-directory="Profile 1"' gh auth login
    

    To find out your profile name, I think you can determine that by opening the chrome://version URL.

  12. ulfjack commented on Mar 7, 2023

    @ulfjack
    Contributor

    You could always try to make your tool with as many configurations as possible. In any case, I'd be happy if the tool printed the URL, and I sent you a PR for that (it's literally a one-line change). That would also help with some of the use cases above.

  13. ElanHasson commented on Jun 13, 2023

    @ElanHasson

    Did that not happen to you? Can you check the exit status of xdg-open and see if you're running an up-to-date GitHub CLI?

    This doesn't work for everyone. I use the desktop environment when I'm at the machine, and ssh when I am not there.

    I therefore never hit the failure scenario.

    Why can't we just output the link ahead of time as suggested here: #5500 (comment)

    Also, even when I do goto the URL manually and enter the code, gh hangs and doesn't ever acknowledge the code was entered. There is a bug here where a separate goroutine should be waiting for the code and cancel the other one that failed to launch the browser.

    I love a great developer experience, however this endless bike shedding isn't fun.

    > gh auth login
    ? What account do you want to log into? GitHub.com
    ? What is your preferred protocol for Git operations? HTTPS
    ? Authenticate Git with your GitHub credentials? Yes
    ? How would you like to authenticate GitHub CLI? Login with a web browser
    
    ! First copy your one-time code: ABC-123F
    Press Enter to open github.com in your browser... 
    [20013:20013:0612/204112.489039:ERROR:ozone_platform_x11.cc(239)] Missing X server or $DISPLAY
    [20013:20013:0612/204112.489167:ERROR:env.cc(255)] The platform failed to initialize.  Exiting.
    
  14. hilbix commented on Jul 26, 2023

    @hilbix

    Come on, its over a year now for such a simple thing!

    This problem here hits me so often, that I already add a mandatory bookmark to https://github.com/login/device in all of my browsers which I use to login to GH. I nearly dare to request Firefox Sync to be taken out of the disallowed browser applications at my side due to this only issue with GH to get the bookmarks synced, because it is extremely annoying that the URL is not printed at all and I always have to type that in or add the bookmark manually.

    If the URL would be printed into the terminal, it is just a single click to open the real local correct browser (terminology anyone?), because the local terminal always knows the right browser, but the deeply buried bash on the remote ssh session does not. Also it does not help if xdg-open opens a browser on the remote display (greetings from dbus), which then isn't logged into GH (if you happen to see the remote display at all, not all systems have x11vnc installed).


    And if you are on it, please explain how to revoke the apropriate CLI again via web. Without access to the CLI.

    AFAICS the inverse is https://github.com/settings/applications and remove the entire GitHub Cli.

    But this is clumsy, as this revokes access for all CLIs alltogether!
    Is there a way to individually remove a single CLI?

    Note that gh auth logout does not log out other CLIs, only the local one. What if you want to remove all others but the current one?

    No, this is NOT a request to add this feature (as this would be a new issue) but, if you are on it, please add the URL to the reverse, too!. This could go into the same line ..

  15. added
    gh-auth
    on Oct 2, 2023
  16. FloHu commented on Jun 21, 2024

    @FloHu

    I have not been able to resolve this. I work on a remote machine and can log in via remote desktop but this is extremely annoying. Why can I not authenticate on the CLI with my token? Reading the above comments I managed to get a browser open with w3m but after entering the one-time code I have to authorize GitHub to access data from my organisation (I don't know why I am asked every time) and then it fails because apparently JavaScript needs to work for that.
    Not everyone has full administrative control over their computers. I am in a corporate environment and have now spent several hours already fiddling around with authentication on remote servers. Also VSCode with ssh connection is not able to authenticate GitHub copilot properly.

  17. daviewales commented on Jul 19, 2024

    @daviewales

    Note: I encounter this issue in WSL if I have curses-based browsers installed such as links or lynx. If I uninstall these, then update-alternatives sets the www-browser to wslview, which automatically opens URLs in the default Windows browser.

    So, on WSL at least, you should be able to work around this problem with the following:

    sudo update-alternatives --set www-browser /usr/bin/wslview
    
  18. evilhamsterman commented on Jul 19, 2024

    @evilhamsterman

    Note: I encounter this issue in WSL if I have curses-based browsers installed such as links or lynx. If I uninstall these, then update-alternatives sets the www-browser to wslview, which automatically opens URLs in the default Windows browser.

    So, on WSL at least, you should be able to work around this problem with the following:

    sudo update-alternatives --set www-browser /usr/bin/wslview
    

    You have to ensure the wslu package is installed to get the wslview app

  19. added
    discuss
    on Sep 27, 2024
  20. Karinza38 commented on Oct 24, 2024

    @Karinza38
  21. corneliusroemer commented on Sep 22, 2025

    @corneliusroemer
  22. 6A31 commented on Mar 10, 2026

    @6A31

    if BROWSER=false gh auth login still shows same failing behaviour, GH_BROWSER=false gh auth login can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    corediscussenhancementgh-auth

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions