I'm trying to troubleshoot a sudden outbreak of short-lived SSH connections. These connections are initiated from my Mac (MBP, Catalina 10.15.6, zsh) to some of the Linux "appliances" that live on my network here - in particular the Raspberry Pies. After the connections terminate, they leave the message client_loop: send disconnect: Broken pipe in the terminal.

However: That's offered only as background in case it's related to my immediate question:

Why does the command ssh-agent -k not kill ssh-agent?

It appears to fail because the environment variable SSH_AGENT_PID is not set. I have guessed that based on what I see in my Mac terminal app:

1. list ssh-related processes:

% pgrep -f ssh       
% ps 2138     
 2138   ??  S      0:00.26 /usr/bin/ssh-agent -l

PID 2138 is ssh-agent, the other PIDs are active ssh connections to Raspberries or Ubuntu box - all Linux.

2. kill ssh-agent IAW man ssh-agent:

From man ssh-agent:

ssh-agent [-c | -s] -k
-k Kill the current agent (given by the SSH_AGENT_PID environment variable).

% ssh-agent -k 
SSH_AGENT_PID not set, cannot kill agent  
% sudo ssh-agent -k  
SSH_AGENT_PID not set, cannot kill agent



I can kill ssh-agent using kill 2138, or with pkill ssh-agent, and so it seems that perhaps the answer is Apple's version of ssh-agent doesn't provision the environment variable SSH_AGENT_PID. But if that's the answer, it raises another question: Is there a valid reason for not assigning the environment variable SSH_AGENT_PID?

Also note that a related question. However, the OP for that question didn't identify his SSH client host was macOS. He also indicated SSH_AGENT_PID was not set, but seemed concerned only with how ssh-agent was started. In my case (macOS), ssh-agent is started when an ssh connection is initiated. I found other Q&A in the "Similar Questions" suggestion box; I read a few of them, but none seem the same as mine.

2 Answers 2


See this answer.

From the man page:

When ssh-agent is started, it prints the shell commands required to set its environment variables, which in turn can be evaluated in the calling shell, for example eval `ssh-agent -s`

So running ssh-agent just shows the commands to set the environment variables, not actually sets them, unless you use eval.

  • Could you please explain why ssh-agent would only print the shell commands - rather than actually running those commands. That just does not seem like a logical thing for ssh-agent to do.
    – Seamus
    Aug 2, 2022 at 7:34
  • 1
    I'm not an expert but I think it has something to do with the ability of programs to affect the shell. I think setting environment (and shell) variables is something only the shell can do itself or by using export. Since ssh-agent can't set the variables for you, the next best thing is to show you how.
    – skube
    Aug 3, 2022 at 10:47

I had the same issue on ubuntu 18.04. After using pidof ssh-agent to get the IDs, the following worked for me:

export SSH_AGENT_PID=<pid>
eval `ssh-agent -k`
  • 5
    It's been a while since I asked this question, so bear with me, but I think my question was, "Why isn't the environment set properly in macOS?" Unless I'm missing something, your answer sets the env var manually, but that must be repeated at each boot??
    – Seamus
    Aug 25, 2021 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .