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.

1 Answer 1


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`
  • 3
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.