登录时启动 ssh-agent [英] Start ssh-agent on login

查看:25
本文介绍了登录时启动 ssh-agent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个站点作为使用 SSH 别名从 Bitbucket.com 拉取的远程 Git 存储库.我可以在我的服务器上手动启动 ssh-agent,但每次我通过 SSH 登录时都必须这样做.

I have a site as a remote Git repo pulling from Bitbucket.com using an SSH alias. I can manually start the ssh-agent on my server but I have to do this every time I login via SSH.

我手动启动了 ssh-agent:

I manually start the ssh-agent:

eval ssh-agent $SHELL

然后我添加代理:

ssh-add ~/.ssh/bitbucket_id

然后当我这样做时它会出现:

Then it shows up when I do:

ssh-add -l

我可以走了.有什么方法可以使这个过程自动化,这样我就不必每次登录时都这样做吗?服务器运行的是 RedHat 6.2(圣地亚哥).

And I'm good to go. Is there any way to automate this process so I don't have to do it every time I login? The server is running RedHat 6.2 (Santiago).

推荐答案

请仔细阅读这篇文章.您可能会发现这非常有用:

Please go through this article. You may find this very useful:

https://web.archive.org/web/20210506080335/https://mah.everybody.org/docs/ssh

以防万一上面的链接有一天消失,我正在捕获下面解决方案的主要部分:

Just in case the above link vanishes some day, I am capturing the main piece of the solution below:

Joseph M. Reagle 通过 Daniel Starin 提供的解决方案:

This solution from Joseph M. Reagle by way of Daniel Starin:

将以下内容添加到您的 .bash_profile

Add this following to your .bash_profile

SSH_ENV="$HOME/.ssh/agent-environment"

function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

这个版本特别好,因为它会查看您是否已经启动了 ssh-agent,如果找不到,则会启动它并存储设置,以便下次您可以使用它们启动一个shell.

This version is especially nice since it will see if you've already started ssh-agent and, if it can't find it, will start it up and store the settings so that they'll be usable the next time you start up a shell.

这篇关于登录时启动 ssh-agent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆