Note: I used to use random unique temporary file as socket filename
(ssh-agent.%RANDOM%-%TIME:~6,5%.ssh-socket). But in this case, it’s
important that we have known SSH_AUTH_SOCK variable as we need that
variable in every shell that we open. So this variable should be
permanent.

Now, we have an active ssh-agent running in memory, and two important
environment variables set accordingly.

Stopping an ssh-agent

We can kill the ssh-agent process anytime, with -k switch. It will
kill the process based on variable SSH_AGENT_PID. It will fail if that
variable is not properly set, and this will very often happen when we
are switching CMD shell’s.

ssh-agent -k

So, when we don’t have a process PID in variable, and if we think that
some agent process is still running wild, without the reason, in
Windows we can kill them all with:

taskkill /im "ssh-agent.exe" /f

And, this is a simple and effective solution.

Adding identities to agent

Agent initially does not have any private keys. Keys are added using
ssh-add command. Simply invoking this command without arguments will add
a default ~/.ssh/id_rsa key to an agent.