Why does "ssh-agent && ssh-add" only work once?

0

Have a look at the following shell session:

niklas@llw ~ % ssh-agent && ssh-add
SSH_AUTH_SOCK=/tmp/ssh-ZzWYJAmK8809/agent.8809; export SSH_AUTH_SOCK;
SSH_AGENT_PID=8810; export SSH_AGENT_PID;
echo Agent pid 8810;
Enter passphrase for /home/niklas/.ssh/id_rsa: 
Identity added: /home/niklas/.ssh/id_rsa (/home/niklas/.ssh/id_rsa)

niklas@llw ~ % killall ssh-agent

niklas@llw ~ % ssh-agent && ssh-add
SSH_AUTH_SOCK=/tmp/ssh-TNMkMvgP8880/agent.8880; export SSH_AUTH_SOCK;
SSH_AGENT_PID=8881; export SSH_AGENT_PID;
echo Agent pid 8881;
Could not open a connection to your authentication agent.

Why can I run ssh-agent && ssh-add only once?

nh2

Posted 2013-02-12T20:41:05.070

Reputation: 797

Answers

1

First I suggest you double check and make sure you didn't already have an agent running. I suspect you had an agent already running. Then you started a second, without properly updating the environment. Your ssh-add communicated with the original agent. Then you killed all the agents, and tried starting a new one without properly updating the environment, the following ssh-add couldn't contact the original agent, which you had killed.

Next you need to understand that when the SSH agent starts, it spits out a script that needs to be used to update your environment. This is how client programs know how to contact the agent. One way to do this is with a command like below.

. <(ssh-agent)
ssh-add

Zoredache

Posted 2013-02-12T20:41:05.070

Reputation: 18 453

You are absolutely right. There was already an agent running, which is started before I log in, namely with the use-ssh-agent in /etc/X11/Xsession.options and its implementation in /etc/X11/Xsession.d/90x11-common_ssh-agent. I now simply run ssh-add. – nh2 – 2013-02-25T01:25:44.950