6

I want to connect to github using SSH protocol in my WSL, i followed every single step from the github documentation here. The key was generated, I've tested my connection, but it still doesn't work. I still have to put username or password everytime i push my code to github. Can anyone help me on this?

I also did this.

# start the ssh-agent in the background
$ eval $(ssh-agent -s)
> Agent pid 59566

$ ssh-add ~/.ssh/id_rsa

PS: I don't know if this is related or not, but I have two .ssh directories in my WSL and my bash root folder. It works in my bash because i guess VS Code generated the SSH key for me.

Above is my SSH in WSL, and below is my SSH in bash (and this one is working)

Edit: I guess it's the ssh-agent that's not working

4 Answers 4

7

On WSL 2, I was using SSH to commit to GitHub and found that I was asked for my password every time. After hours of searching and trying several methods (which worked with caveats I couldn't live with), this is what worked for me.

Install keychain:

sudo apt install keychain

Then add the following line to your shell's configuration file (likely ~/.bashrc or ~/.zshrc):

eval `keychain --quiet --eval --agents ssh id_rsa`

Now you will only have to enter your password when booting WSL!

Credit to Birk Holland with this article.

Sign up to request clarification or add additional context in comments.

Comments

6

If you're being prompted for a username and password, it's possible that you're trying to use the HTTPS protocol instead of SSH. You can see what the URL looks like by using git remote -v. If you want to change to use the SSH protocol, you'd use something like git remote set-url origin [email protected]:git/git.git (where git/git.git is the repository you're pushing to).

In general, trying to connect to GitHub over SSH won't prompt you for a username and password, because GitHub doesn't support password authentication over SSH.

Comments

1

This is followed by eamodio/vscode-gitlens issue 909

I am trying to use git through SSH in a WSL remote.
The SSH connection is protected with a key pair which is password-protected.
How do I start an SSH agent and input that passphrase for a shell in which GitLens operates?

By default a bash/wsl shell is opened in terminal window on WSL remote connection.
I start an SSH agent there and add the key, but it seems like this is not the shell in which GitLens is sending its git commands.

A workaround would be to regenerate those keys (after opening first a session with the existing keys) and:

  • make sure the new keys are not passphrase-protected
  • add the new public one in the ~/.ssh/authorized_keys file of the WSL session.

1 Comment

thanks! it didn't work before because i cloned the repo using HTTPS now it does
1

1️⃣ Check If Git Is Even Seeing Your SSH Config

Run:

GIT_SSH_COMMAND="ssh -v" git clone [email protected]:hpk992/your-repo.git

This enables verbose SSH debugging. Look for lines like:

debug1: Reading configuration data /home/youruser/.ssh/config
debug1: Connecting to github.com

If it's not using SSH at all, then Git is hardwired to HTTPS somewhere.

2️⃣ Check If Git Is Overriding SSH with HTTPS

Run:

git config --show-origin --get-all url."https://github.com/".insteadOf

If this returns any result, it means Git is forcing HTTPS. Remove it:

git config --global --unset-all url."https://github.com/".insteadOf
sudo git config --system --unset-all url."https://github.com/".insteadOf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.