There are probably a billion guides for this already, but whatever. If you DON’T have a ~/.bash_profile (a file that gets executed every time you start cyg):

touch ~/.bash_profile
chmod a+x ~/.bash_profile

Now that you have the file, add this to it:

SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
	eval `$SSHAGENT $SSHAGENTARGS`
	trap "kill $SSH_AGENT_PID" 0
fi

This will start up ssh-agent for each Cygwin shell you have open. Close your Cygwin shell (if one is open) and open a new one. Now type:

ssh-add ~/.ssh/id_rsa
[enter your password]

Voila! No more typing your stupid password every time you need to ssh somewhere. Note that if you close the Cygwin window, you’ll have to ssh-add your key again! This is good security…you can close the window when you’re done and someone who happens on your computer sitting there won’t have password-less access to any of your secure logins.

Trackback

OMGOSH 6 comments

  1. Thank you. This worked for me and solved one of my problems.

  2. Super Jamie said

    Very useful, thank you so much!

    A couple of extra tips:

    If you don’t specify a password when you create your key, you don’t have to enter one when you run ssh-add. This way you can put ssh-add in your .bashrc as well.

    If this doesn’t work, check the permissions on the remote ~/.ssh/authorized_keys file. It needs to be chmod 600 or you will always get asked for your password.

  3. Jonathan Vasquez said

    Thanks :), this solved one of my problems on Windows where if I opened cygwin and typed “ssh-agent bash” or any other shell/screen, it wouldn’t find the agent. So I had to type “ssh-agent” get all the info it printed on the screen, and copy/paste. Very annoying.

  4. With a little bit of if-then and a flat file, you can do this once and have all other consoles use the same one, instead of starting one per console, and (if you use key auth) having to ssh-add each time.

    Just pipe the output of ssh-agent to a file, then run the file (e.g. with ‘source’ command), check the $SSH_AGENT_PID with ps, and if it’s not running, then run ssh-agent (again, piping its output to the file and then sourcing the file). Put your ssh-adds in there too so it will ask you on start.

    For example here’s what I did:

    if [[ -e $HOME/.sshagent.conf ]]; then
    . $HOME/.sshagent.conf
    fi

    if `ps -p ${SSH_AGENT_PID}>/dev/null`;then true;
    else
    ssh-agent >| $HOME/.sshagent.conf
    . $HOME/.sshagent.conf
    ssh-add ~/.ssh/id_dsa
    fi

  5. Thanks Keith! Just had to change ‘id_dsa’ to ‘id_rsa’ and it worked beautifully.

  6. [...] article est une traduction (assez libre et agrémentée) de SSH Agent on Cygwin via kill the [...]