iTranslated by AI
ssh-agent Starts but Fails to Work Properly on WSL2
Overview
Background
On WSL2, I should have a configuration where I only need to enter the password once correctly for SSH using a public key, and after that, the password is not requested unless the daemon is restarted or I log out. However, this has not been working correctly recently.
Target Audience
- People for whom SSH has not been working correctly recently
- Using SSH with public keys on WSL2 (hereafter referred to as WSL)
- Managing SSH keys via
ssh-agentthroughkeychain
Non-target Audience
- People for whom SSH has not been working correctly recently, but who do not meet the two conditions mentioned above.
Environment
The environment for this article is as follows.
$ wsl.exe --version
WSL バージョン: 2.2.4.0
カーネル バージョン: 5.15.153.1-2
WSLg バージョン: 1.0.61
MSRDC バージョン: 1.2.5326
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.26091.1-240325-1447.ge-release
Windows バージョン: 10.0.22631.3880
Also, the distribution is Arch Linux.
$ cat /eNAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
BUILD_ID=rolling
VERSION_ID=20231001.0.182270
ANSI_COLOR="38;2;23;147;209"
HOME_URL="https://archlinux.org/"
DOCUMENTATION_URL="https://wiki.archlinux.org/"
SUPPORT_URL="https://bbs.archlinux.org/"
BUG_REPORT_URL="https://bugs.archlinux.org/"
PRIVACY_POLICY_URL="https://terms.archlinux.org/docs/privacy-policy/"
LOGO=archlinux-logotc/os-release
Furthermore, the terminal used is fish.
$ fish --version
fish, version 3.7.1
I will also list the versions for keychain and systemd.
$ keychain --version
* keychain 2.8.5 ~ http://www.funtoo.org
Copyright 2002-2006 Gentoo Foundation;
Copyright 2007 Aron Griffis;
Copyright 2009-2017 Funtoo Solutions, Inc;
lockfile() Copyright 2009 Parallels, Inc.
Keychain is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
$ systemctl --version
systemd 256 (256.2-1-arch)
+PAM +AUDIT -SELINUX -APPARMOR -IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBCRYPTSETUP_PLUGINS +LIBFDISK +PCRE2 +PWQUALITY +P11KIT +QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +BPF_FRAMEWORK +XKBCOMMON +UTMP -SYSVINIT +LIBARCHIVE
Main Discourse
Sharing Prerequisites
While the environment is as mentioned above, I will also describe the specific configuration.
Referring to this article [1], it was managed using eval \ssh-agent``.
Solution
When I looked into it, I found the following articles, but they were not effective for me.
- Using ssh-agent via keychain on Ubuntu in WSL2
- I want to skip SSH passphrases! ~ Whether to configure config or automate Keychain registration ~
The method that worked was to perform the following two settings and then restart using wsl --shutdown.
Setting 1
I referred to the following page, so please check it for details.
[Unit]
Description=SSH key agent
[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK
[Install]
WantedBy=default.target
The command to enable this setting is as follows.
systemctl --user enable --now ssh-agent.service
Next, set the environment variables as well.
set -gx SSH_AUTH_SOCK "$XDG_RUNTIME_DIR/ssh-agent.socket"
Setting 2
Add a statement to enable SSH keys in ~/.ssh/config. You can specify this for each host.
AddKeysToAgent yes
Note: While researching, I came across articles that included the following; however, these are settings for macOS and are not intended for WSL2 on Arch.
AddToAgent yes
UseKeychain yes
Additional Notes
With these two settings, it is no longer necessary to use keychain, so you may uninstall or disable it.
Cautions for disabling or uninstalling
Checking dependencies:
If other settings or scripts depend on keychain,
they may stop working.
It is recommended to check the scope of impact before disabling it.
Use on other systems:
Be cautious if you share SSH configurations across different environments.
If you are using keychain on a non-WSL2 system,
this could have a critical impact.
Also, keep in mind that these two settings utilize systemd. This is because the compatibility between WSL2 and systemd is not always perfect, which might lead to complications in the future.
Discussion