`ssh-add` 每次在 Cygwin 都会被重置

`ssh-add` is reset every time at Cygwin

我在 Windows 10 使用 cygwin 终端。但是我遇到了 git via ssh 的问题。虽然 Git Bash 终端每次都工作,但 cygwin 每次都需要 ssh-add。我的 cygwin 发生了什么?

$ which git
/cygdrive/c/Program Files/Git/cmd/git
$ ssh-add -l
Could not open a connection to your authentication agent.
$ eval `ssh-agent`
Agent pid 13900
$ ssh-add -l
The agent has no identities.
$ ssh -vT git@github.com
OpenSSH_7.5p1, OpenSSL 1.0.2k  26 Jan 2017
debug1: Connecting to github.com [192.30.253.112] port 22.
debug1: Connection established.
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /cygdrive/c/Users/jef/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
debug1: Remote protocol version 2.0, remote software version libssh_0.7.0
debug1: no match: libssh_0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /cygdrive/c/Users/jef/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /cygdrive/c/Users/jef/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
Enter passphrase for key '/cygdrive/c/Users/jef/.ssh/id_rsa':
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.253.112]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi jojef! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2664, received 1848 bytes, in 0.1 seconds
Bytes per second: sent 35785.4, received 24824.1
debug1: Exit status 1

$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /cygdrive/c/Users/jef/.ssh/id_rsa:
Identity added: /cygdrive/c/Users/jef/.ssh/id_rsa (/cygdrive/c/Users/jef/.ssh/id_rsa)
$ git pull
Already up-to-date.

Restart cygwin terminal

$ git pull
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我发现 windows 命令提示符工作正常。

C:\Users\jef\work\movie_dialog>git pull
Enter passphrase for key '/c/Users/jef/.ssh/id_rsa':
Already up-to-date.
C:\Users\jef\work\movie_dialog>where git
C:\Program Files\Git\cmd\git.exe

更新

我引用了 VonC 的 link,然后创建了 ~/.bash_profile,而不是 bashrc。我不确定 .bashrc 是否有效。然后,我可以拉!每次登录 Windows 只需要输入一次密码。 Auto-launching ssh-agent on Git for Windows

如果您有 set your .bash_profile on Cygwin,它会在每个新的 Cygwin 会话中询问您的密码,并且您的 git 拉取会成功。

在 Windows 上,显然,您每次都输入密码,但您可以使用 Windows git bash.[=26 中的 similar ~/.bashrc 来避免它=]

正如我mentioned before, git is likely to be different between Cygwin and Git for Windows.


created ~/.bash_profile, not bashrc. I am not sure .bashrc didn't work. Then, I can pull!

As mentioned here.bashrc 仅由交互式和非登录的 shell 读取。但是打开 cygwin 会话可能会被视为 "login",这意味着只读取 .bash_profile
这就是为什么大多数人最终告诉他们的 .bash_profile 也阅读 .bashrc 的原因:

[[ -r ~/.bashrc ]] && . ~/.bashrc