Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass git ssh credentials from wsl to VScode dev-container

I've setup ssh keys for my gitlab account using ssh keys in WSL.

Now I want to use the same ssh git credentials in VScode dev-containers.

How do I pass ssh keys to the dev container?

like image 873
ziv Avatar asked Oct 19 '25 12:10

ziv


1 Answers

In Vscode dev-container documentation they explain how to use SSH agent to pass the shh keys to the container:

first find they keys files that you have on your system. To do that run the following in your WSL terminal (e.g. Ubuntu) ls ~/.ssh/. By default the shh key file name start with a id_. look for such a file (e.g. id_ed25519).

In the terminal run eval "$(ssh-agent -s)". Then run ssh-add ~/.ssh/id_ed25519 (replace the file name with your key file).

BTW to list the already added keys, run ssh-add -l

The problem is that in linux the ssh-agent doesn't start automatically on start up. So we need to add it to ~/.bash_profile file.

In the terminal run code ~/.bash_profile to open the file in vscode. Then add the following to the file:

if [ -z "$SSH_AUTH_SOCK" ]; then
   # Check for a currently running instance of the agent
   RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
   if [ "$RUNNING_AGENT" = "0" ]; then
        # Launch a new instance of the agent
        ssh-agent -s &> $HOME/.ssh/ssh-agent
   fi
   eval `cat $HOME/.ssh/ssh-agent`
   ssh-add ~/.ssh/id_ed25519
fi

Notice the ssh-add line at the end. This is because the ssh-agent in linux doesn't persist the keys as it does in windows.

Restart your computer or just restart WSL by running wsl --shutdown. This will prompt a message from docker for windows to restart. Open a new WSL terminal as type shh-add -l to see that the key is there.

Now start a project in a VScode dev container and in the terminal type shh-add -l. The same key as in you WSL should be listed.

like image 80
ziv Avatar answered Oct 21 '25 04:10

ziv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!