3

I'm using WSL2, on Terminal, with Ubuntu installed. I have added the following to my .bashrc:

# Start ssh-agent
# check if ssh-agent is already running
if [ -z "$SSH_AUTH_SOCK" ]; then
     #start ssh-agent
    eval "$(ssh-agent -s)"
fi


# Ask for ssh-add
read -p "Do you want to add your SSH public key? (y/n) " response

if [ "$response" = "y" ]; then
  ssh-add ~/id_rsa
  ssh-add -l
elif [ "$response" = "n" ]; then
  echo "No identity on terminal session"
else
  echo "Invalid response"
fi

With the idea that every time I open a new terminal, it will ask for a prompt if an identity is needed and proceed to start ssh-agent and add the key if yes. At first, it appears as if the script work and it'll show the agent's PID, along with a prompt for my ssh password and I'm able to see the ssh-agent when i do top. However, it did not actually initialize ssh-agent nor add the key, despite seeing the output of these commands. I even did ssh-add -l and it'll output: The agent has no identities.

How do I go about making this work? This has to be a WSL issue..

4
  • Try using inside your if [ -z "$SSH_AUTH_SOCK" ] this: ssh-agent -s > ~/.sshconnection and in the ~/.bashrc file (after the first if statement) use: . ~/.sshconnection or source ~/.sshconnection Commented Mar 22, 2023 at 6:19
  • Just to clarify, do you mean to make these changes?: ` if [ -z "$SSH_AUTH_SOCK" ]; then #start ssh-agent ssh-agent -s > ~/.sshconnection fi . ~/.sshconnection` I've tested this one and it gives the same stdout as my previous entry, but still did not add my identity to the session. Commented Mar 23, 2023 at 0:04
  • Did you make any progress on this? Are you running WSL with Systemd or without? Also, is there any difference if you start with wsl ~ -e bash --norc --noprofile and then run the commands manually? Commented May 12, 2023 at 23:02
  • Similar question with multiple answers: superuser.com/questions/1808572/… Commented Feb 4 at 19:48

1 Answer 1

0

Hum... you try to load ~/id_rsa. Usually, keys are in the ~/.ssh folder. AFAIR ssh is also very picky with file and folder permissions. If a key is in a directory that ssh thinks not secured enough, it won't use it.

If your key is really in ~, move it (and the corresponding .pub file) into the ~/.ssh folder. Make sure you own the files and the ~/.ssh has 0700 mode, ~/.ssh/id_rsa should have 0600 mode. See chmod(1) if you don't know how to do that.

Also, there's no need to give a filename to ssh-add. As per the man-page:

ssh-add adds private key identities to the authentication agent, ssh-agent(1). When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ecdsa_sk, ~/.ssh/id_ed25519, ~/.ssh/id_ed25519_sk, and ~/.ssh/id_dsa. After loading a private key, ...

in short :

  • make sure your key files are in ~/.ssh folder,
  • in your .bashrc file just use ssh-add.

A quick note: Having questions in .bashrc is not necessarily a good thing. There exist situations (but maybe not in WSL) when .bashrc is loaded in non-interactive situations. If you really want to ask questions, read answers, protect the whole thing with a test on PS1

if test "$PS1"; then
  if [ -z "$SSH_AUTH_SOCK" ]; then
    #start ssh-agent
    eval "$(ssh-agent -s)"
  fi

  # Ask for ssh-add
  read -p "Do you want to add your SSH public key? (y/n) " response

  # and so on...
fi

EDIT NOTE:

I removed technical text that was not mine, but was added by someone else. Style, grammar, typos etc... corrections are always welcome. That someone adds his/her thoughts to my answer is not appreciated. thank you.

1
  • Thanks for that! I've added the test on PS1, I did not think of that so that's pretty good. Other than that, it seems no identity is still added :( my key was originally in a directory src which I've then moved to .ssh and just used ssh-add in .bashrc. I've also made sure they've got the correct permissions as you've mentioned. Not sure why it's still not adding the identity despite it saying it did when I open a new terminal. Commented Mar 23, 2023 at 0:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .