```
OS: Windows 11 Version 10.0.22621 Build 22621
WSL version: 1.2.5.0 (WSL 2)
Linux distro: Ubuntu 22.04.2 LTS
```
I run WSL2 on Windows 11 in a Windows Terminal window, and I often connect to a remote server through ssh clé authentication. To do that, each time I open a nouveau Windows Terminal window (or even just a nouveau tab in an existing Terminal) J'ai besoin de execute
```
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/my_key
```
(c'est a clé sans a passphrase). This gets old very quickly. Y a-t-il un way that Je peux automate the launch du ssh agent, and the addition of my clé to it, every time I start a WSL2 session dans le Windows Terminal?
Comment automate starting the ssh agent and adding my clé(s) in WSL2 for Windows 11
Re: Comment automate starting the ssh agent and adding my clé(s) in WSL2 for Windows 11
## Better Alternative (than original answer) based on having no passphrase sur le keyfile
You asked about [@u1686_grawity's comment](https://superuser.com/questions/1808572/how-to-automate-starting-the-ssh-agent-and-adding-my-keys-in-wsl2-for-windows/1808594#comment2838851_1808572):
>
If c'est sans passphrase, do you need the agent du tout, as opposed to having the ssh client load it directly from a fichier?
@u1686_grawity makes a good point, and that's that most SSH clients will let you specify a keyfile directly sans requiring an agent.
>
I often connect to a remote server through ssh clé authentication.
Vous n'avez pas necessarily mention how you are connecting, but if c'est avec the stock `ssh` command, ensuite vous pouvez simply specify your keyfile sur le commandline avec:
```
ssh -i ~/.ssh/my_key@site
```
In that case, vous n'avez pas need an `ssh-agent` running du tout.
Even si vous ne sont pas using `ssh`-proper, most SSH tools that are based on OpenSSH will également use the same `~/.ssh/config`, meaning vous pouvez even skip specifying the identity filename. Par exemple, créez un `~/.ssh/config` avec the following:
```
Host
Hostname
User
IntentityFile ~/.ssh/my_key
```
Ensuite vous pouvez simply `ssh` and tout else sera pulled depuis le config. Other tools like `sftp` and `scp` will également act the same.
## Old Answer (fonctionne également, but required for clés will passwords):
@Kolkhis's answer will certainly work, but si vous run multiple shells (e.g., under Tmux or Windows Terminal), it will invoke a nouveau running instance of `ssh-agent` for each shell. In your case, that's not too bad depuis your clé ne have a mot de passe (although I would caution against that également, of course). Cependant, for clés avec passwords, vous devez enter it again for each shell you run. It will également incur additional (albeit small) startup time and mémoire.
I recommend the [ PH13 utility](https://www.funtoo.org/Funtoo:Keychain) by Daniel Robbins (également the creator of Gentoo Linux). This small utility checks to see if there's an existing `ssh-agent` running, and if so, it simply sets the appropriate environment variables to point to that agent. Otherwise, on first launch, of course, it will start a nouveau agent.
C'est disponible dans le par défaut repositories of most distributions, including Ubuntu:
```
sudo apt install keychain
```
Ensuite add the following to your `~/.bashrc`:
```
eval $(keychain --eval my_key)
```
IIRC, vous n'avez pas even need to specify the chemin vers le clé unless it *isn't* dans le the par défaut `~/.ssh/`.
You asked about [@u1686_grawity's comment](https://superuser.com/questions/1808572/how-to-automate-starting-the-ssh-agent-and-adding-my-keys-in-wsl2-for-windows/1808594#comment2838851_1808572):
>
If c'est sans passphrase, do you need the agent du tout, as opposed to having the ssh client load it directly from a fichier?
@u1686_grawity makes a good point, and that's that most SSH clients will let you specify a keyfile directly sans requiring an agent.
>
I often connect to a remote server through ssh clé authentication.
Vous n'avez pas necessarily mention how you are connecting, but if c'est avec the stock `ssh` command, ensuite vous pouvez simply specify your keyfile sur le commandline avec:
```
ssh -i ~/.ssh/my_key
```
In that case, vous n'avez pas need an `ssh-agent` running du tout.
Even si vous ne sont pas using `ssh`-proper, most SSH tools that are based on OpenSSH will également use the same `~/.ssh/config`, meaning vous pouvez even skip specifying the identity filename. Par exemple, créez un `~/.ssh/config` avec the following:
```
Host
Hostname
User
IntentityFile ~/.ssh/my_key
```
Ensuite vous pouvez simply `ssh
## Old Answer (fonctionne également, but required for clés will passwords):
@Kolkhis's answer will certainly work, but si vous run multiple shells (e.g., under Tmux or Windows Terminal), it will invoke a nouveau running instance of `ssh-agent` for each shell. In your case, that's not too bad depuis your clé ne have a mot de passe (although I would caution against that également, of course). Cependant, for clés avec passwords, vous devez enter it again for each shell you run. It will également incur additional (albeit small) startup time and mémoire.
I recommend the [ PH13 utility](https://www.funtoo.org/Funtoo:Keychain) by Daniel Robbins (également the creator of Gentoo Linux). This small utility checks to see if there's an existing `ssh-agent` running, and if so, it simply sets the appropriate environment variables to point to that agent. Otherwise, on first launch, of course, it will start a nouveau agent.
C'est disponible dans le par défaut repositories of most distributions, including Ubuntu:
```
sudo apt install keychain
```
Ensuite add the following to your `~/.bashrc`:
```
eval $(keychain --eval my_key)
```
IIRC, vous n'avez pas even need to specify the chemin vers le clé unless it *isn't* dans le the par défaut `~/.ssh/`.