Remember git passphrase in WSL

后端 未结 3 573
無奈伤痛
無奈伤痛 2021-01-11 14:52

I run Windows 10 with WSL. I have the desired behaviour on one computer, but cannot replicate elsewhere. Here\'s what I\'m after:

  1. First time I run a remote g
相关标签:
3条回答
  • 2021-01-11 15:13

    I tried the option to add AddKeysToAgent yes to ~/.ssh/config but it doesn't keep it between new tabs on the terminal.

    The best solution I found so far is to do the following:

    sudo apt install keychain
    

    Find your hostname using the terminal:

    hostname
    

    Then add the following to your ~/.bashrc or ~/.zshrc file:

    /usr/bin/keychain --nogui ~/.ssh/id_rsa
    source $HOME/.keychain/YOUR-HOSTNAME-HERE-sh
    

    Now, each time you reboot, you’ll have to enter your passphrase. But you only have to do it one time until you reboot or terminate WSL.

    If you want to use the same key you already have on Windows you can follow this post Sharing SSH keys between Windows and WSL 2

    0 讨论(0)
  • 2021-01-11 15:34

    I tried both methods in previous answers (as well as others found elsewhere) on WSL 2 and they either did not work or had 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!

    Thank you Birk Holland for this article.

    0 讨论(0)
  • 2021-01-11 15:37

    I found the answer!

    First, make sure you have ssh-agent running all the time by adding eval $(ssh-agent) to your .bash_profile.

    Then add AddKeysToAgent yes to your ssh config:

    touch ~/.ssh/config
    chmod 600 ~/.ssh/config
    echo "AddKeysToAgent yes" >> ~/.ssh/config
    

    You'll get prompted when you first do some ssh, but the passphrase will be automatically added to the ssh-agent so you won't have to type it again until you end your session and start a new one.

    0 讨论(0)
提交回复
热议问题