Connecting to github using PuTTY generated SSH key in Windows

108

Solution 1

I have put together a step-by-step guide to get Git setup for windows using PuTTY's Plink application for SSH authentication.

Follow along below:


Setup

  1. Install putty.zip which is available at the PuTTY Download Page or you can download individually.

    • PuTTY: putty.exe (or by FTP)

      The SSH and Telnet client itself.

    • Plink: plink.exe (or by FTP)

      A command-line interface to the PuTTY back ends.

    • Pageant: pageant.exe (or by FTP)

      An SSH authentication agent for PuTTY, PSCP, PSFTP, and Plink.

    • PuTTYgen: puttygen.exe (or by FTP)

      An RSA and DSA key generation utility.

  2. Generate RSA and PPK Keys

    1. Using the Git Bash, use ssh-keygen to generate a pair of RSA public/private keys. More information on how to do this can be found on the official Generating SSH keys article.
    2. In PuTTYgen, import your existing ~/.ssh/id_rsa (private) key, via ConversionsImport key.
    3. Save the imported key via the Save private key button as ~/.ssh/id_rsa.ppk.
    4. You should now have the following keys in your ~/.ssh directory:

      • id_rsa: Private (OpenSSH) RSA key
      • id_rsa.pub: Public (OpenSSH) RSA key
      • id_rsa.ppk: Private (PuTTY) key
  3. Install Git for Windows.

    Make sure that you choose to use Plink.

    Git setup

    Note: If you have already installed Git, you can just run the installer again and set Plink to be your default SSH application.

  4. Set your Environment paths.

    1. In Control Panel, navigate to the System view.
    2. Choose Advanced system settings.
    3. In the System Properties window, click the Advanced tab.
    4. Click Environment variables….
    5. Add the following System variables (if not already set):

      • GIT_HOME: C:\Program Files\Git
      • GIT_SSH: C:\Program Files (x86)\PuTTY\plink.exe
    6. Append the Git binary directory to the system path.

      • Path: %Path%;%GIT_HOME%\bin
  5. Open Pageant and load the ppk key located at ~/.ssh/id_rsa.ppk.

    Note: Once Pageant has started, you can click on its icon in the system tray located in the taskbar, next to the time, on the right.

  6. Open Putty and connect to test your connection via SSH and add the server's key as a known host.

    Putty

    Examples hostnames:

  7. Start Git Bash.

    You should be able to push and pull from your remote host without entering a password each time.


Shortcut

You can place a shortcut in your startup directory to auto-load your key each time you log into your Windows account.

Via Batch Script

This idea was inspired by an answer to this question:

Super User: How to make a shortcut from CMD?.

REM |==================================================================|
REM | Pageant Autoload.bat                                             |
REM |                                                                  |
REM | This script creates a shortcut for auto-loading a PPK (key) in   |
REM | Pageant by writing a temporary VB script and executing it. The   |
REM | following information below is added to the shortcut.            |
REM |                                                                  |
REM | Filename  : Pageant Autoload                                     |
REM | Target    : pageant.exe                                          |
REM | Arguments : id_rsa.ppk                                           |
REM | Start in  : ~/.ssh                                               |
REM |==================================================================|
@echo off

REM |==================================================================|
REM | Global Values - Do not touch these!                              |
REM |==================================================================|
SET VBSCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
SET STARTUP_DIR=Microsoft\Windows\Start Menu\Programs\Startup
SET STARTUP_USER_DIR=%APPDATA%\%STARTUP_DIR%
SET STARTUP_ALL_USERS_DIR=%PROGRAMDATA%\%STARTUP_DIR% REM Alternative

REM |==================================================================|
REM | Shortcut Values - You can change these to whatever you want.     |
REM |==================================================================|
SET FILENAME=Pageant Autoload.lnk
SET TARGET=%PROGRAMFILES(x86)%\PuTTY\pageant.exe
SET ARGUMENTS=id_rsa.ppk
SET START_IN=%%USERPROFILE%%\.ssh
SET DESCRIPTION=Autoload PuTTY key with Pageant on startup (Ctrl+Alt+S)
SET HOTKEY=CTRL+ALT+S

REM |==================================================================|
REM | Write a new VB script, on the fly; execute and delete it.        |
REM |==================================================================|
ECHO Set oWS = WScript.CreateObject("WScript.Shell") >> %VBSCRIPT%
ECHO sLinkFile = "%STARTUP_USER_DIR%\%FILENAME%" >> %VBSCRIPT%
ECHO Set oLink = oWS.CreateShortcut(sLinkFile) >> %VBSCRIPT%
ECHO oLink.TargetPath = "%TARGET%" >> %VBSCRIPT%
ECHO oLink.Arguments = "%ARGUMENTS%" >> %VBSCRIPT%
ECHO oLink.WorkingDirectory = "%START_IN%" >> %VBSCRIPT%
ECHO oLink.Description = "%DESCRIPTION%"  >> %VBSCRIPT%
ECHO oLink.HotKey = "%HOTKEY%" >> %VBSCRIPT%
ECHO oLink.Save >> %VBSCRIPT%
CScript //Nologo %VBSCRIPT%
DEL %VBSCRIPT% /f /q

Via Windows Explorer

  1. Navigate to the startup directory in Windows Explorer.

    • User Startup/ directory (preferred) is located at:

      %AppData%\Microsoft\Windows\Start Menu\Programs\Startup
      
    • All Users Startup/ directory is located at:

      %ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup
      
  2. Right-click inside the folder and select NewShortcut

  3. In the Create Shortcut dialog, enter the following information.

    • Location: "C:\Program Files (x86)\PuTTY\pageant.exe"
    • Name: Pageant Autoload
  4. Right-click the new shortcut and choose Properties from the context menu.

  5. Modify the following fields under the Shortcut tab:

    • Target: "%PROGRAMFILES(x86)%\PuTTY\pageant.exe" id_rsa.ppk
    • Start in: %USERPROFILE%\.ssh

     
    Notes:

    1. If you are using a 32-bit Windows OS, you should use the %PROGRAMFILES% environment variable instead of %PROGRAMFILES(x86)%.

    2. If you placed your shortcut in the All Users startup directory, make sure that the current user has an id_rsa.ppk key in their ~/.ssh directory or the key will not auto-load.


Closing Remarks

There you have it. Next time you log into your Windows profile, you will be greeted with a Pageant prompt to enter the password for your key. If you did not set a password on your key, then your key should be loaded automatically without a prompt.

If you are not sure if your key loaded view the current keys in Pageant by selecting View Keys from the context menu for Pageant in the system tray.

Solution 2

You are confusing two entirely separate programs: PuTTY and OpenSSH.

  • plink and Pageant are part of PuTTY. The ssh command is part of OpenSSH. It is unclear which program is being used by Git; you need to check the %GIT_SSH% environment variable for that.

  • The programs use different agent protocols; OpenSSH cannot use PuTTY's Pageant; it has its own ssh-agent (which unfortunately is somewhat complicated to use on Windows).

  • PuTTY and plink store the session settings in registry, editable in PuTTY's interface. They do not use anything in ~/.ssh/; this directory is only used by OpenSSH.

  • The private key formats used by OpenSSH and PuTTY are different; you cannot use a .ppk key with OpenSSH. If you generated the key in PuTTYgen, you have to use its "Export → OpenSSH" command.

    $ ssh -vvvT [email protected]
    OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
    ...
    debug2: key_type_from_name: unknown key type 'PuTTY-User-Key-File-2:'
    

Solution 3

In plain English

debug3: Not a RSA1 key file /c/Users/Radu/\.ssh\github.ppk.

debug2: key_type_from_name: unknown key type 'PuTTY-User-Key-File-2

Puttygen can build different of keys, Github wants SSH1-RSA (?, I use ssh2 keys with Pageant on github)

Adds

See also this post about debugging pageant issues with Github

>plink.exe -v -agent [email protected]
Looking up host "github.com"
Connecting to 207.97.227.239 port 22
Server version: SSH-2.0-OpenSSH_5.1p1 Debian-5github2
Using SSH protocol version 2
We claim version: SSH-2.0-PuTTY_Release_0.62
Doing Diffie-Hellman group exchange
Doing Diffie-Hellman key exchange with hash SHA-256
Host key fingerprint is:
ssh-rsa 2048 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA1 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA1 server->client MAC algorithm
Pageant is running. Requesting keys.
Pageant has 1 SSH-2 keys
Using username "git".
Trying Pageant key #0
Remote debug message: Forced command: gerve lazybadger
Remote debug message: Port forwarding disabled.
Remote debug message: X11 forwarding disabled.
Remote debug message: Agent forwarding disabled.
Remote debug message: Pty allocation disabled.
Authenticating with public key "github/lazybadger" from agent
Sending Pageant's response
Remote debug message: Forced command: gerve lazybadger
Remote debug message: Port forwarding disabled.
Remote debug message: X11 forwarding disabled.
Remote debug message: Agent forwarding disabled.
Remote debug message: Pty allocation disabled.
Access granted
Opened channel for session
Server refused to allocate pty
Started a shell/command
Hi lazybadger! You've successfully authenticated, but GitHub does not provide shell access.
Server sent command exit status 1
Disconnected: All channels closed
Share:
108

Related videos on Youtube

jordan
Author by

jordan

Updated on September 18, 2022

Comments

  • jordan
    jordan almost 2 years

    I am about to complete a text based java board game (Very basic). And I am stuck on this last thing -

    Basically what happens is when a user has types 'roll' to roll the virtual die. Then depending on the results an array gets updated to change the location of 'o' (which makes it look like there counter has been moved). I have figured out how to make this for the first roll.

    But what I cannot figure out is how to make the second roll and first roll add up (and so on with third and fourth roll).

    (example -

    Roll 1: Player rolls the die - gets a 3 - moves to 3rd position on the board (solved)

    Roll 2: The game remembers that the last roll was a 3 - player rolls a 4 - updates the board and moves the counter to number 7)

    Here is a snippet of the relevant code:

        public void play(String p1Name2, String p2Name2){
    
        Scanner user_input = new Scanner(System.in);
        Random rn = new Random();
        String p1Roll = null,p2Roll;
        int[] p1r = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
    
        int b= 0,c= 0,d = 0,e = 0,g = 0;
    
        while(array1[10] != 'o'){
            c++;
    
                if(b==0) {
                    int i =1;
                    System.out.println("Player 1, type roll to roll the die. ");
                    p1Roll = user_input.next();
    
                        if(p1Roll.equalsIgnoreCase("roll")){
                            p1r[c] = rn.nextInt(6);
                            p1r[c] += 1;
                            System.out.println("You rolled a: " + p1r[c]);
    
    
                            for(int f = 0; f< 10; f++ ){
                                if(array1[f] == 'o'){
                                  array1[f] = 'x';
                                  array1[p1r[c]] = 'o';
                                  if(i ==1){
                                      this.board();
                                      i = 0;
    
                                }
                            }                       
                    }
                }
    
    
                if(b==1) {      
    
                }
    
        }   
        }
    }
    

    and

        public void board(){
    
    
        System.out.println("           1  2  3  4  (5)  6  7  8  9  10  11");
        System.out.println("Player 1:  " + array1[0]+ "  " + array1[1] + "  " + array1[2]+ "  " + array1[3] + "   " + array1[4]+ "   " + array1[5] + "  " + array1[6]+ "  " + array1[7] + "  " + array1[8]+ "  " + array1[9] + "   " + array1[10]);
        System.out.println("Player 2:  " + array2[0]+ "  " + array2[1] + "  " + array2[2]+ "  " + array2[3] + "   " + array2[4]+ "   " + array2[5] + "  " + array2[6]+ "  " + array2[7] + "  " + array2[8]+ "  " + array2[9] + "   " + array2[10]);
    
    
    }
    

    Finally, here is the console output:

    Player 1, type roll to roll the die. 
    roll
    You rolled a: 4
               1  2  3  4  (5)  6  7  8  9  10  11
    Player 1:  x  x  x  x   o   x  x  x  x  x   x
    Player 2:  o  x  x  x   x   x  x  x  x  x   x
    Player 1, type roll to roll the die. 
    roll
    You rolled a: 4
               1  2  3  4  (5)  6  7  8  9  10  11
    Player 1:  x  x  x  x   o   x  x  x  x  x   x
    Player 2:  o  x  x  x   x   x  x  x  x  x   x
    Player 1, type roll to roll the die. 
    roll
    You rolled a: 2
               1  2  3  4  (5)  6  7  8  9  10  11
    Player 1:  x  x  o  x   x   x  x  x  x  x   x
    Player 2:  o  x  x  x   x   x  x  x  x  x   x
    Player 1, type roll to roll the die.
    
    • Lazy Badger
      Lazy Badger over 12 years
      with -vT, please, less verbosity level
    • Tschallacka
      Tschallacka about 8 years
      Is there a requirement to use array or are you free to use objects too? if you'd use objects it'd be a helluva lot easier and cleaner for you.
    • jordan
      jordan about 8 years
      @engineer I tried, after one roll the counter ends up at the end.
    • jordan
      jordan about 8 years
      @MichaelDibbets I could use objects, I am sorta new to java and unfamiliar with objects - could you suggest how I could do it with them?
    • Tschallacka
      Tschallacka about 8 years
      gimme a moment to build up something @jordan
  • user1686
    user1686 over 12 years
    SSH1 is obsolete and has numerous security holes. Github does not use it.
  • Radu
    Radu over 12 years
    I am aware of the difference and as indicated in the original post, git has been set to use plink when installed. I have verified that this has changed the proper environment variable to point to plink.exe. I'd like to use PuTTY keys (and this has worked in the past!) with git, and the fact that its not working despite plink being used is weird...
  • Radu
    Radu over 12 years
    Essentially, right now I can get it to work with OpenSSH keys, but not with PuTTY keys. I have tried reinstalling PuTTY and also generating new keys. TThat doesn't seem to be problem. Any idea what it could be?
  • user1686
    user1686 over 12 years
    @Radu: plink does not support OpenSSH key format. If your version does, then it's not plink you are using; it's something else.
  • user1686
    user1686 over 12 years
    @Radu: Also, the original post indicates the opposite of your claim. First you generate a key with PuTTYgen and load it to Pageant, but then you go with configuring OpenSSH and complain that "ssh prompts for pass-phrase despite being loaded into Pageant". You are confusing the two programs.
  • Radu
    Radu over 12 years
    I never said that plink supports OpenSSH - Git Bash, however, does, since by default it uses OpenSSH. Reading back, you are correct, I have been confusing things since as you said, ~/.ssh/config is not used by PuTTY. However, I'd like to reiterate that GIT_SSH is pointed to plink, and yet if I modify the ssh config file for OpenSSH the changes are reflected in Git Bash when attempting to connect to github. Essentially, I believe that the GIT_SSH environment variable is being ignored and I suppose it's something specific to my OS. I'll try on a fresh image tomorrow.
  • user1686
    user1686 over 12 years
    Something is indeed being ignored. How are you setting the variable? Are the changes reflected in git bash, when checking with echo $GIT_SSH? Does setting the var with export GIT_SSH='plink.exe' help?
  • Epu
    Epu over 11 years
    This equivalent to 'ssh -T [email protected]' is sadly missing from the github howto documentation. Thank you for posting.
  • Vomit IT - Chunky Mess Style
    Vomit IT - Chunky Mess Style over 8 years
    Great detailed instructions! +1 from me!
  • Saurabh Kumar
    Saurabh Kumar over 7 years
    Importing the private key in puttygen was my missing link
  • user46193
    user46193 almost 7 years
    Had to create a connection in Putty to enable the step in the setup process from the picture.
  • jgalak
    jgalak about 6 years
    Fantastic! I had a similair issue with gitlab.com and this resolved it. Git was using putty whereas the key I had uploaded was the OpenSSH one. Thank you!
  • Matt Borja
    Matt Borja about 4 years
    Had to actually do the "connection test" with PuTTY to accept the fingerprint before git clone would work on the command line (PowerShell), otherwise plink.exe just hung.
  • masiton
    masiton almost 3 years
    I don't understand why is use of OpenSSH and Putty mixed here. pageant.exe has nothing to do with the .ssh directory and I don't understand how are these connected in this manual. Either you put your keys as files into .ssh dir, or you load them up into pageant.exe and you use either one approach to authenticate. I do not understand what's the deal with somehow using both.