Git- How to kill ssh-agent properly on Linux [closed]
Asked Answered
H

5

33

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 [email protected] 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?

Hypercorrection answered 31/3, 2017 at 9:43 Comment(4)
How is this related to ssh-agent?Ephrayim
@Ephrayim Because it's git via ssh, and the ssh-agent help to handle username & password via ssh keys automatically.Hypercorrection
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.Ephrayim
@Ephrayim 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.Hypercorrection
S
31

You can kill ssh-agent by running:

ssh-agent -k

!NB, -k argument works only if $SSH_AGENT_PID environment variable is set. eval "$(ssh-agent -s)" sets the variable, but there're also other methods to start the agent without setting the environment variable. E.g., connecting to a remote server implicitly starts the agent without setting the variable. In this case, you need to use pidof ssh-agent or pgrep ssh-agent to find the ssh-agent PID and then kill it.

Sarad answered 6/1, 2021 at 13:12 Comment(4)
Doesn't work: SSH_AGENT_PID not set, cannot kill agentCerellia
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?Sarad
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.Cerellia
@EduardMukans Please add also that the -k only works if the environment variable SSH_AGENT_PID is set properly, that's what eval "$(ssh-agent -s)" actually does. BTW: that was already discussed in comments a long time ago. So please consider adding this valuable information with another update. Thanks in advance.Bellflower
C
9

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

Cerellia answered 14/3, 2021 at 23:15 Comment(1)
killall did not do anything in my case. kill -9 $(pidof ...) worked. Thanks.Vicereine
S
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
Sharmainesharman answered 31/3, 2017 at 9:50 Comment(1)
I just tried this on my linux, the ssh-agent process become defunct after using the script, same result as kill -9 command.Hypercorrection
C
4

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

Centrifugate answered 15/6, 2021 at 17:37 Comment(0)
F
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
Ful answered 24/5, 2019 at 0:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.