How to automatically close the ssh-agent along with closing Cygwin?

0

I am on Windows 10 Pro.

I have added the following lines to my Cygwin (fully up-to-date) ~/.bashrc file:

eval $(ssh-agent) > /dev/null
ssh-add > /dev/null 2>&1

in order for me not to have to do those each and every time as I typically don't do anything else than connect to Linux machines.

Notice, that I redirect the output of those commands to the black hole for me not really being interested in it, otherwise it would output something like:

Agent pid 11060
Identity added: /home/vlastimil/.ssh/id_rsa (/home/vlastimil/.ssh/id_rsa)

But, I noticed today, that for some reason, that those ssh-agent.exe processes remain in memory after I log out and close Cygwin.

Question is:

How to automatically close the ssh-agent along with closing Cygwin?

LinuxSecurityFreak

Posted 2018-05-07T12:56:35.793

Reputation: 2 298

Answers

0

I figured, first thing to do, is in the ~/.bashrc file, I need to save the output of:

eval $(ssh-agent)

Instead of discarding it; an example follows:

eval $(ssh-agent) > ~/.ssh-agent-stdout

Finally, create the following file:

~/.bash_logout

With contents:

ssh_agent_pid=$(awk '{ print $3 }' ~/.ssh-agent-stdout)
kill -HUP "$ssh_agent_pid"

LinuxSecurityFreak

Posted 2018-05-07T12:56:35.793

Reputation: 2 298

I think this is no viable solution if you open multiple terminals. It would kill the ssh-agent for the other terminals, too. – sweisgerber.dev – 2018-12-18T17:40:12.030

@sweisgerber.dev I since have found out, there is ssh-host-config program in Cygwin ;) – LinuxSecurityFreak – 2018-12-19T04:45:19.930

I googled after @vlastimil 's short keyword comment and found: https://docs.oracle.com/cd/E24628_01/install.121/e22624/preinstall_req_cygwin_ssh.htm#EMBSC281 section: 5.4 Configuring SSH this should help fellow ssh-agent configurators.

– sweisgerber.dev – 2018-12-19T10:58:24.910