Git- 如何在 Linux 上正确杀死 ssh-agent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43137524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:41:49  来源:igfitidea点击:

Git- How to kill ssh-agent properly on Linux

linuxgitsshssh-agent

提问by Eric Wang

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

我在 linux 上使用 git,当推送到 gitlab 时,有时它会卡在:

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

debug1:连接到 gitlab.com [52.167.219.168] 端口 22。

or

或者

debug1: client_input_channel_req: channel 0 rtype [email protected] reply 1

debug3: send packet: type 100

debug1:client_input_channel_req:通道 0 rtype [email protected] 回复 1

debug3:发送数据包:类型 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.

似乎重新启动 linux 可以解决它,但没有人喜欢重新启动机器。所以,我试图杀死 ssh-agent 进程,然后重新启动它。

But the process is always defunctafter 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?

但是进程总是defunct在kill之后,然后我根本无法通过ssh使用git,那么有没有办法重新启动ssh-agent,或者在不重新启动机器的情况下解决上述问题?



@Update

@更新

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

我使用的 ssh 密钥包括关键短语,我会在第一次使用 ssh 密钥时输入它。

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

该问题通常发生在我将 Linux 桌面从睡眠状态恢复后,从而重新连接网络后,不确定这是否重要?

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

再一次,有没有人知道如何杀死或重新启动ssh-agent代理,而不会使它变成defunct

回答by danglingpointer

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

您可以尝试使用此 bash 脚本来终止 SSH 代理:

#!/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

回答by wisbucky

It shows defunctprobably 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:

它显示defunct可能是因为它的父进程仍在监视它,所以它没有从进程表中删除。没什么大不了的,进程被杀死了。只需启动一个新的 ssh-agent:

eval $(ssh-agent)

ssh-add