sh 登录时自动添加ssh密钥:将此文件保存到〜/ .bashrc

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 登录时自动添加ssh密钥:将此文件保存到〜/ .bashrc相关的知识,希望对你有一定的参考价值。

# Note: ~/.ssh/environment should not be used, as it
#       already has a different purpose in SSH.

env=~/.ssh/agent.env

# Note: Don't bother checking SSH_AGENT_PID. It's not used
#       by SSH itself, and it might even be incorrect
#       (for example, when using agent-forwarding over SSH).

agent_is_running() {
    if [ "$SSH_AUTH_SOCK" ]; then
        # ssh-add returns:
        #   0 = agent running, has keys
        #   1 = agent running, no keys
        #   2 = agent not running
        ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
    else
        false
    fi
}

agent_has_keys() {
    ssh-add -l >/dev/null 2>&1
}

agent_load_env() {
    . "$env" >/dev/null
}

agent_start() {
    (umask 077; ssh-agent >"$env")
    . "$env" >/dev/null
}

if ! agent_is_running; then
    agent_load_env
fi

# if your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
    agent_start
    ssh-add ~/.ssh/work_laptop
elif ! agent_has_keys; then
    ssh-add ~/.ssh/work_laptop
fi

unset env

以上是关于sh 登录时自动添加ssh密钥:将此文件保存到〜/ .bashrc的主要内容,如果未能解决你的问题,请参考以下文章

远程使用 key 证书登录

sh 将SSH密钥添加到代理。此步骤也可用于避免在执行git push / pull时输入SSH密码

sh 将密码添加到SSH密钥

Centos7 Putty SSH密钥登录

linux下ssh免密码登录设置

无密钥登录的自动脚本实现(ssh-copy-idexpect免交互输入脚本)