SSH not working in WSL native terminal but works in VS Code

166 views Asked by At

Having a weird problem with WSL that I've never encountered before. I've added the proper commands to my .bash_profile to start ssh-agent and add my Github ssh-key, when the terminal opens I can see that the identity is added.

When I am in the native terminal, after I have confirmed that the key has been added, if I try to clone an git repo, it asks for my password. If I ssh-add -l it lists the identity. What I've noticed is that when ssh-agent is actually working, username@systemname will turn green, indicating that it is working. If I exec ssh-agent bash and then ^C, it resets my terminal and I ssh works, I just have to add the key again manually.

Now the strange part is that if I open VS Code or Cursor and connect to WSL, it adds the identity to the terminal and works straight out of the box. I don't have to do any of that other black magic to get it to work. At least this works.

Not the end of the world, but would really like to get this figured out... been combing the web for 24 hours and haven't found any good leads.

Thanks!

I added this code to my .bash_profile

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add ~/.ssh/github
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add ~/.ssh/github
fi

unset env

I expected ssh-agent to start and load my github key when the terminal opens, it does, but it doesn't work and no 'connection' is instantiated.

0

There are 0 answers