30

I am using git on linux, when pushing to gitlab, sometimes it either stuck at:

debug1: Connecting to gitlab.com [52.167.219.168] port 22.

or

debug1: client_input_channel_req: channel 0 rtype keepalive@openssh.com reply 1

debug3: send packet: type 100

Seems restarting linux could solve it, but no body like to reboot machines. So, I am trying to kill ssh-agent process, then restart it.

But the process is always defunct after kill, and then I can't use git via ssh at all, so is there any way to restart the ssh-agent, or solve the issue described above without restarting the machine?


@Update

The ssh keys that I use include key phrase, which I would input on first use of a ssh key.

The issue usually occurs after I bring the Linux desktop back from sleep, thus the network is reconnected, not sure whether this matters?

Again, does any one knows how to kill or restart a ssh-agent agent, without making it become defunct?

Community
  • 1
  • 1
Eric
  • 22,183
  • 20
  • 145
  • 196
  • How is this related to `ssh-agent`? – Jakuje Mar 31 '17 at 10:14
  • @Jakuje Because it's git via ssh, and the ssh-agent help to handle username & password via ssh keys automatically. – Eric Mar 31 '17 at 10:28
  • `ssh-agent` does not handle any passwords. Only a private keys and operations with them. But if `ssh` is hanging on `connecting to ...` it is certainly not related to the `ssh-agent`, but to some network issues, where restarting `ssh-agent` will not help you. – Jakuje Mar 31 '17 at 10:30
  • @Jakuje I mean it help to avoid username & password with ssh keys, without it you need username & password. And I believe it's related to `ssh-agent` in some way, because on windows, restarting `ssh-agent` would fix similar issue, it's just I can't restart it on linux, instead the reboot would fix it. – Eric Mar 31 '17 at 10:32

5 Answers5

23

You can kill ssh-agent by running:

eval "$(ssh-agent -k)"
Eduard Mukans
  • 857
  • 8
  • 11
  • 2
    Doesn't work: SSH_AGENT_PID not set, cannot kill agent – smac89 Mar 14 '21 at 23:10
  • 2
    Then, most likely, the ssh-agent is not running. Try to start the agent running `eval "$(ssh-agent -s)"` and then kill it. What OS and shell you're using, by the way? – Eduard Mukans Mar 15 '21 at 11:21
  • 3
    No the agent is definitely running. You assume it is only started by doing `eval "$(ssh-agent -s)"`, but that's not the only way it can be started. It is also started when you try to ssh to a remote server. In that case, `SSH_AGENT_PID` is not set in the current shell, but running `pidof ssh-agent` or `pgrep ssh-agent` shows you the PID of the one that is running. – smac89 Mar 15 '21 at 16:16
  • eval `ssh-agent -k` - shorter. – Andrey Aug 29 '23 at 07:42
6

You can try this bash script to terminate the SSH agent:

#!/bin/bash

## in .bash_profile

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

## in .logout

if [ ${SSH_AGENT_PID+1} == 1 ]; then
    ssh-add -D
    ssh-agent -k > /dev/null 2>&1
    unset SSH_AGENT_PID
    unset SSH_AUTH_SOCK
fi
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
  • 2
    I just tried this on my linux, the ssh-agent process become defunct after using the script, same result as `kill -9` command. – Eric Mar 31 '17 at 15:15
5

Many ways:

  • killall ssh-agent
  • SSH_AGENT_PID="$(pidof ssh-agent)" ssh-agent -k
  • kill -9 $(pidof ssh-agent)

pidof is from the procps project. You may be able to find it for your distro if it is packaged

smac89
  • 39,374
  • 15
  • 132
  • 179
2

yes, ssh-agent might be defunct: [ssh-agent] <defunct>

trying to kill the agent could help:

eval "$(ssh-agent -k)"

but also try to check your keyring process (e.g. gnome-keyring-daemon), restart it or even remove the ssh socket file:

rm /run/user/$UID/keyring/ssh

iman
  • 21,202
  • 8
  • 32
  • 31
0

It shows defunct probably because its parent process is still monitoring it, so it's not removed from the process table. It's not a big deal, the process is killed. Just start a new ssh-agent:

eval $(ssh-agent)

ssh-add
wisbucky
  • 33,218
  • 10
  • 150
  • 101