Skip to content

Instantly share code, notes, and snippets.

@apappas1129
Last active June 16, 2023 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apappas1129/19df04334590cc9267d34e3d9546aa07 to your computer and use it in GitHub Desktop.
Save apappas1129/19df04334590cc9267d34e3d9546aa07 to your computer and use it in GitHub Desktop.
My experience in setting up SSH Git & TortoiseGit on Windows 10 WSL2

My experience in setting up SSH Git & TortoiseGit on Windows 10 WSL 2

My current projects were using GitLab but you can easily translate equivalent steps for GitHub. Here are the steps I took and the mistakes along the way:

  1. I went to Gitlab Account Settings > SSH Keys
  2. I clicked Learn more and followed through the steps:

(in Ubuntu WSL Terminal)

  1. I generated an SSH key pair (2048-bit RSA) by running:

    $ ssh-keygen -t rsa -b 2048 -C "floki-ubuntu-ssh"
    
  2. I copied the public key by running $ cat <path-to>/floki-ubuntu-ssh.pub and saved it to Gitlab SSH Keys (Set expiry date to Never)

  3. I configured ssh to point to correct directory because at this time I was not generating the SSH key pair files on the default directory for some reason.

    $ eval `ssh-agent -s`
    $ ssh-add <path-to>/floki-ubuntu-ssh
    

Now I am not sure why I did this or how it made sense to me to do this thing. And if it's correct/necessary to do this step

  1. I wanted to verify if I can now connect to gitlab and I saw GitLab: SSH host keys fingerprints.

    6.1. $ ssh-keyscan gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf

    6.2. $ ssh-keyscan gitlab.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsj2bNKTBSpIYDEGk9KxsGh3mySTRgMtXL583qmBpzeQ+jqCMRgBqB98u3z++J1sKlXHWfM9dyhSevkMwSbhoR8XIq/U0tCNyokEi/ueaBMCvbcTHhO7FcwzY92WK4Yt0aGROY5qX2UKSeOvuP4D6TPqKF1onrSzH9bx9XUf2lEdWT/ia1NEKjunUqu1xOB/StKDHMoX4/OKyIzuS0q/T1zOATthvasJFoPrAjkohTyaDUz2LN5JoH839hViyEG82yB+MjcFV5MU3N1l1QL3cVUCh93xSaua1N85qivl+siMkPGbO5xR/En4iEY6K2XPASUEMaieWVNTRCtJ4S8H+9

    6.3. $ ssh-keyscan gitlab.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFSMqzJeV9rUzU4kWitGjeR4PWSa29SPqJ1fVkhtj3Hw9xjLVXVYrU9QlYWrOLXBpQ6KWjbjTDTdDkoohFzgbEY=

    6.4. $ ssh -T git@gitlab.com

    6.5. The first time I connected, I needed to verify the authenticity of the GitLab host since I got the following message:

    The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
    ED25519 key fingerprint is SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8.
    This key is not known by any other names
    Are you sure you want to continue connecting (yes/no/[fingerprint])?
    

    6.6. So I entered yes and then tried $ ssh -T git@gitlab.com again. Then I got the welcome message:

    Welcome to GitLab, @a.pappas!
    
  2. I tried cloning $ git clone git@gitlab.com:example/my-project.git But I get:

    git@gitlab.com: Permission denied (publickey).
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.
    

At this point I am not sure what to do so I thought to myself, maybe I should try making my git work successfully on the host OS first (Windows)

  1. So I also setup my ssh on the main OS, Windows 10, same steps done on #3 to 7. Then tried cloning on a windows directory, but still the same result above.

I had to take a break and continue the next day so at this point I shut down my computer and the next day...

  1. I found out that the ssh-add I did on Ubuntu instance was not persistent. Meaning, my custom ssh key pair "floki-ubuntu-ssh" was not added tp ssh-agent's list permanently. I discoverd this by runninv the verbose version of the command:

    $ ssh -Tvvv git@gitlab.com
    

    The output does not say it tried "floki-ubuntu-ssh". But I've realized and learned about these default stuffs as printed by the verbose logs:

    debug1: Will attempt key: /home/floki/.ssh/id_rsa
    debug1: Will attempt key: /home/floki/.ssh/id_ecdsa
    debug1: Will attempt key: /home/floki/.ssh/id_ecdsa_sk
    debug1: Will attempt key: /home/floki/.ssh/id_ed25519
    debug1: Will attempt key: /home/floki/.ssh/id_ed25519_sk
    debug1: Will attempt key: /home/floki/.ssh/id_xmss
    debug1: Will attempt key: /home/floki/.ssh/id_dsa
    debug2: pubkey_prepare: done
    ...
    git@gitlab.com: Permission denied (publickey).
    
  2. So for now I renamed the files into id_rsa and id_rsa.pub and moved it to the .ssh folder since it also does not exist yet there.

  3. Run ssh -T git@gitlab.com and got greeted with the Welcome again and git clone finally worked.

Other things to consider and I did this but not sure it helped me at all:

  1. Make sure the Ubuntu user owns the all the folders/directory where you wish to execute your git commands. I my case I have a folder named "code" (where I will run my git clone) in the root directory, and my username is "floki"

    $ cd ~/
    $ sudo chown -r floki code
    
  2. Make sure the Ubuntu user owns the SSH files

    $ ls -l ~/.ssh
    

    The expected output in my case should be something like "-r-------- 1 floki floki ..." :

    -rw------- 1 floki floki 1876 Jan  1 23:27 id_rsa
    -rw-r--r-- 1 floki floki  394 Jan  1 23:27 id_rsa.pub
    -rw-r--r-- 1 floki floki  142 Jan  2 00:02 known_hosts
    

Setting up my TortoiseGit.

  1. I tried to use the TortoiseGit right-click Context Menu to execute a git pull on my cloned repo. But I get an error that tells me to do this on git bash terminal:

    $ git config --global --add safe.directory '%(prefix)///wsl$/Ubuntu/home/floki/code/my-project'
    
  2. Tried to pull again with TortoiseGit Context Menu and got this error:

    ---------------------------
    TortoiseGitPlink Fatal Error
    ---------------------------
    No supported authentication methods available (server sent: publickey)
    ---------------------------
    OK   
    ---------------------------
    
  3. Tried to check the SSH key list on Git Bash:

    $ eval `ssh-agent -s`
    $ ssh-add -l
    

    Output:

    The agent has no identities.
    
  4. Just out of curiosity, I also tried to check the SSH key list on Windows CMD:

    start-ssh-agent
    

    But the SSH runtime thing ends immediately so I cant run sssh-add command :

    ssh-add -l
    

    I don't what should have been done to make this work. please let me know in the comments.

  5. I realized my WSL is WSL 1. not WSL2. So I went through this article. And on step 5 in the article, I downloaded the WSL2 Linux kernel update package for x64 machines and run it.

  6. Opened Powershell and run: wsl --set-default-version 2 Output: The operation completed successfully.

  7. Installed Ubuntu 22.04.1 LTS https://www.microsoft.com/store/apps/9PN20MSR04DW (via Microsoft Store)

  8. Clicked Launch button and do first time setup. (named the user "floki22")

  9. To get a GUI on the root directory use: $ explorer.exe .

  10. Setup ssh again but this time simply run ssh-keygen and I hit enter consecutively because the default output is ~/.ssh/id_rsa and I don't want to add passphrase.

    NOTE: If you have generated your ssh key pair with a passphrase, you will be asked for it every time you execute a git command or any other command that uses the same ssh key pair. If you dont want this, you can simply update your passphrase to blank with $ ssh-keygen -p and just reset it to empty.

  11. Copy the public SSH key (again, the file with ".pub" on its name) and saved to gitlab settings

At this point. git clone and other git commands via Ubuntu WSL2 terminal works. So I've already cloned my repo via terminal.

  1. Opened TortoiseGit Settings (via context menu) > Git > Edit global .gitconfig

  2. added under "[safe]":

    directory = %(prefix)///wsl.localhost/Ubuntu-22.04/home/floki22/code/my-project
    

    NOTE: This is the same as the commands executed on step 1 but with GUI instead.

  3. git pull/fetch still fails. Aand.. I'm stuck again.

Somehow I eventually ended up learning that I have to setup my TortoiseGit's PuTTYgen properly.

  1. I went to C:\Program Files\TortoiseGit\bin and opened puttygen.exe
  2. Clicked Load and loaded the private key file, id_rsa
  3. I saved the ppk format for private key, id_rsa.ppk (on the same folder, .ssh, so it won't get lost when I need to find it for some reason in the future.)
  4. I run pageant.exe from the same folder and then went to double click it from the system tray to open its window.
  5. Click Add key, select the .ppk recently made
  6. Tried TortoiseGit Context Menu to git pull on my repo again. And it finally worked!

But pageant does not persit the .ppk and it does not auto run on windows startup

  1. Windows + R , Enter "shell:startup"
  2. Craete Shortcut and set the target to "C:\Program Files\TortoiseGit\bin\pageant.exe" "\wsl.localhost\Ubuntu-22.04\home\floki22.ssh\id_rsa.ppk" You can have multiple ppk files (e.g. ".../pageant.exe" ".../1.ppk" ".../2.ppk" ...)
  3. Restart computer and confirm that pageant auto runs and has the ppk files already.

I haven't narrowed it down when this happened, but after a while, after switching/pulling/fetching between branches, and maybe another restart, I happened to encounter this issue:

error: cannot open .git/FETCH_HEAD: Permission denied

There must be some files generated by TortoiseGit along the way and it's not automatically owned by the Ubuntu user unlike when you create a floder via mkdir in the Terminal.

  1. All I needed to do is run (note, my projects/repos will be on folder named "code")

    $ sudo chown floki22 code --recursive
    

    (thats chown <username> <root-folder-containing-your-repository-folders>)

  2. And I also realized, when I eventually had to push some new code with VSCode Git, that the git config in Ubuntu WSL2 instance is different from the TortoiseGit. So I had to configure it via terminal and made sure I have the same use "user.name" and "user.email".

git config --global user.name "Alex Pappas"
git config --global user.email "a.pappas@email.com"

Now I can successfully commit and push changes on both Tortoise and VSCode. In terms of pushing and pulling code, I tend to use VSCode nowadays. But I still end up using Tortoise when merging and rebasing branches, using its code diff tool, etc.

How about using Tortoise Git on Windows 10 user owned directory?

I tried to git clone a fresh copy of the repo and it worked. I believe it's using the same SSH key pair saved on WSL2. Otherwise, I would have had to figure out how to generate another SSH key pair and properly add it to ssh list permanently and make sure my OS is hosting (for cmd git command to work?) aaaand setup PuTTYgen pageant again to add the .ppk for Windows.

I'm curious to how it should have been done also for Windows 10 alone. Please let me know in the comments and correct my misconceptions or any mistakes in the steps I did.

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