Search code examples
gitgithubsshcygwin

`ssh-add` is reset every time at Cygwin


I am using cygwin terminal at Windows 10. But I am facing a problem of git via ssh. Although Git Bash terminal works every time, cygwin needs ssh-add every time. What's happening in my 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 [email protected]
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: [email protected]
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] 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
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

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

I found windows command prompt works correctly.

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

Update

I referred VonC's link, then created ~/.bash_profile, not bashrc. I am not sure .bashrc didn't work. Then, I can pull! The passphrase only needs one time for every login to Windows. Auto-launching ssh-agent on Git for Windows


Solution

  • If you have set your .bash_profile on Cygwin, it will ask your passphrase on each new Cygwin session, and your git pull will succeed.

    On Windows, apparently, you are entering your passphrase each time, but you can avoid it with a similar ~/.bashrc from the Windows git bash.

    As I 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 is only read by a shell that's both interactive and non-login. But opening a cygwin session might be considered as a "login", meaning only .bash_profile is read.
    That is why most people end up telling their .bash_profile to also read .bashrc with something like:

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