André Jarboe II
André Jarboe II

Reputation: 838

How to get github ssh to work CORRECTLY on WSL (Windows Subsystem for Linux)

I need to run windows for video editing but I also do some coding. Linex is a beast and I would love to be able to use WSL on my windows computer and be able to do ssh from WSL. I am trying to start a thread where people can go to and follow the steps for WSL and Github ssh on windows. Can someone help me figure out what I am doing wrong so others can use WSL and Github as well?

My steps:

  1. Reset windows
  2. go to this site and install WSL https://learn.microsoft.com/en-us/windows/wsl/install-win10 I chose Ubuntu 18.04 LTS: https://www.microsoft.com/en-us/p/ubuntu-1804-lts/9n9tngvndl3q?rtc=1&activetab=pivot:overviewtab
  3. set up and upgrade Ubuntu:
    sudo apt update && sudo apt upgrade
  4. Installed VS Code using this website: https://code.visualstudio.com/docs/remote/wsl
  5. INstalled the Remote Development Pack: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
  6. Set up gitbash on windows using defaults
  7. Set up git ssh with this site:https://help.github.com/en/github/authenticating-to-github/checking-for-existing-ssh-keys
  8. Did a git clone made changes and pushed to master just to make sure everything work, It does.
  9. added a .gitattributes file to repo
* text=auto eol=lf
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf

(is there a way to make this automatic?)

  1. Then I ran this command because it suggested it on the site...
git config --global core.autocrlf input
  1. Git still works

  2. dis these steps to share credentials

Configure the credential manager on Windows by running the following in a Windows command prompt or PowerShell:

 git config --global credential.helper wincred
Configure WSL to use the same credential helper, but running the following in a WSL terminal:

 git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-wincred.exe"
  1. Ran git push from wsl and goterror:
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.      

Please make sure you have the correct access rights
and the repository exists.

Git push works on windows side

Upvotes: 29

Views: 47257

Answers (4)

TrojanName
TrojanName

Reputation: 5355

Just adding to the other excellent answers here.

First turning on verbose mode (ssh -vT) was very helpful. It allows you to see the paths that ssh is trying when it looks for your keys.

C:\Users\Me\.ssh>ssh -vT [email protected]
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
debug1: Reading configuration data C:\\Users\\Me/.ssh/config
debug1: C:\\Users\\Me/.ssh/config line 1: Applying options for github.com
debug1: Connecting to github.com [140.82.121.3] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\Me\\.ssh\\id_ed25519 type 3
debug1: identity file C:\\Users\\Me\\.ssh\\id_ed25519-cert type -1
debug1: identity file /c/Users/Me/.ssh/id_ed25519 type -1
debug1: identity file /c/Users/Me/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1

If those paths aren't what you expect, that gives you a direction to start digging deeper.

Another thing that caught me on Windows was that there was an old version of ssh in my path that was screwing things up. You can verify which ssh you are running with:

C:\Users\Me\.ssh>where ssh
C:\Windows\System32\OpenSSH\ssh.exe

Finally, with verbose mode I was able to see that an unexpected key was being used instead of the one I wanted. I edited the .ssh/config file and added a section for Host github.com and a line like this:

IdentityFile C:\Users\Me\.ssh\id_ed25519

WSL also has its own .ssh/config file and the paths there may need to be different.

Upvotes: 1

smoore4
smoore4

Reputation: 4866

After trying numerous other solutions, this is what worked for me. Specifically, I thought I could copy from my local Windows folder to Ubuntu with this command:

cp -r /mnt/c/Users/<username>/.ssh ~/.ssh

But this did not work for me. It is only when I opened the WSL file structure with explorer.exe . did I see that there were two .ssh directories.

With Windows Explorer open, I moved the subfolder files in the second .ssh directory to the /home/<username>/.ssh directory. Then it worked.

sam@SAM-SAM:~$ explorer.exe .
sam@SAM-SAM:~$ ssh -T [email protected]
Warning: Permanently added the RSA host key for IP address '140.82.113.4' to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/sam/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/sam/.ssh/id_rsa": bad permissions
[email protected]: Permission denied (publickey).
sam@SAM-SAM:~$ sudo chmod 600 /home/sam/.ssh/id_rsa
[sudo] password for sam:
sam@SAM-SAM:~$ ssh -T [email protected]
Hi foouser! You've successfully authenticated, but GitHub does not provide shell access.
sam@SAM-SAM:~$

Upvotes: 6

gprathour
gprathour

Reputation: 15333

The steps would be like,

  1. Get SSH working on your Windows machine. For this please follow the official guide.
  2. Copy the SSH files from Windows to WSL.

For this you can try with the following commands,

cp -r /mnt/c/Users/<username>/.ssh ~/.ssh

If the directory copy doesn't work, then you can try to copy single file as well.

cp -r /mnt/c/Users/<username>/.ssh/<file-name> ~/.ssh
  1. Fix the permission

If it gives error for permission, then change the permission to 600 for the file for which it shows error.

chmod 600 ~/.ssh/<file-name>
  1. Finally run the following

Last step should be the following command,

ssh -T [email protected]

You should be good to go now.

Upvotes: 12

Andr&#233; Jarboe II
Andr&#233; Jarboe II

Reputation: 838

Eureka I got it!!

After all the steps above go into your windows terminal and run:

$ ssh -T [email protected]

You should get:

You've successfully authenticated, but GitHub does not provide shell access.

Then on ubuntu enter:

$ cd
$ cd .ssh 
$ code .

This will open up your ubuntu .ssh folder in VS code.

Then open power shell and run:

> cd .ssh
> code .

This will open up your windows .ssh folder in VS code.

Then you can drag and drop your windows fils to ubuntu through vs code.

Now go back to your WSL terminal and run:

$ ssh -T [email protected]

If you get:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0666 for '/home/andre/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/andre/.ssh/id_rsa": bad permissions
[email protected]: Permission denied (publickey).

Then run:

$ sudo chmod 600 ~/.ssh/id_rsa
$ sudo chmod 600 ~/.ssh/id_rsa.pub

Now when you run:

$ ssh -T [email protected]

You should get:

You've successfully authenticated, but GitHub does not provide shell access.

Upvotes: 24

Related Questions