• github和sshagent


    在用ssh访问github的时候,需要设置ssh的代理程序 ssh-agent。

    因为git的安全连接需要ssh提供,而ssh为其他程序提供安全连接的功能由 ssh-agent 完成。

    SSH 只是一种协议,其开源实现有 OpenSSH,并且存在服务端(sshd) 和 客户端 (ssh),Windows 中的客户端有 PuTTY;而这两种客户端都有各自的 ssh agent:

    ssh-agent 命令:是客户端 ssh 的默认代理
    Pageant : 是 客户端 PuTTY 的代理。Pageant是用于PuTTY,PSCP,PSFTP和Plink的SSH身份验证代理。 Pageant会存储您的私钥,只要它正在运行,它就会为PuTTY或其他工具(如IntelliJ IDEA)提供解锁的私钥。(当然ssh-agent也能做)

    把私钥交给 ssh agent 管理的好处:

    当 其他程序 需要身份验证的时候 可以将验证申请交给 ssh-agent 来完成整个认证过程 。如 git

    避免重复输入密码:如果您的私钥使用密码短语来加密了的话,每一次使用 SSH 密钥对 进行登录的时候,您都必须输入正确的密码短语。而 SSH agent 程序能够将您的已解密 的私钥缓存起来,在需要的时候提供给您的 SSH 客户端。

    总之,ssh-agent 就是为程序提供ssh服务的代理,这里的程序包括ssh自己的客户端。

    ssh-agent的用法:

    eval `shell-agent` ,或 eval $(ssh-agent) : 它并不会启动一 个子 shell,而是直接启动一个 ssh-agent 进程;

    此时当我们退出当前 bash 后 ,ssh-agent 进程并不会自动关闭。我们可以在当前 bash 退出之前,使用 ssh-agent -k ,或者在当前 bash 退出之后,使用 kill 命令,关闭对应的 ssh-agent 进程。

    ssh-agent 相关问题
    当我们在中尝试使用 Git 并通过 SSH 协议进行 push 或 pull 时,如果远程 Github 服务 器无法使用 SSH agent 提供的密钥进行身份验证,则可能会收到下面的某一条消息:

    Permission denied (publickey)
    No suitable response from remote
    repository access denied
    可能的两种原因:

    你的 公钥 并没有添加到 Github 服务器中。检查 GitHub 是否有添加。

    您的密钥未加载到 ssh agent 中 。解决方法:

    检查相应的 ssh 密钥是否被加载:
    ssh-add -l
    如果没有被加载,则使用下面的命令加载私钥
    #后面可以同时跟多个私钥
    ssh-add ~/.ssh/<private_key_file>
    运行 ssh-add 时, 如果提示 “Could not open a connection to your authentication agent.” 说明你的ssh-agent并没有运行;使用下面的命令运行 ssh agent,再使用ssh-add命令添加你的 ssh key。
    # 先启动,再运行
    # macOS/Linux
    $ eval `ssh-agent`
    ssh-add ~/.ssh/other_id_rsa

    # 在Windows中的git-bash中
    $ eval $(ssh-agent)
    ssh-add ~/.ssh/other_id_rsa

    配置 ssh-agent 自动启动

    git bash
    方式一: Git for windows 提供的方式

    在 .profile 或 .bashrc 添加 :

    # 在.profile 或 .bashrc 添加
    # Git for windows 提供的方式
    # ssh-agent auto-launch (0 = agent running with key; 1 = w/o key; 2 = not run.)
    agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
    if [ $agent_run_state = 2 ]; then
    eval $(ssh-agent -s)
    ssh-add ~/.ssh/one_rsa # 添加你自己的私钥
    ssh-add ~/.ssh/two_rsa
    elif [ $agent_run_state = 1 ]; then
    ssh-add ~/.ssh/one_rsa
    ssh-add ~/.ssh/two_rsa
    fi
    # 记得还要在 ~/.bash_logout 中添加,来关闭 ssh-agent
    # ssh-agent -k
    新建 ~/.bash_logout 文件,添加:

    # 记得还要在 ~/.bash_logout 中添加,来关闭 ssh-agent
    ssh-agent -k

    方式二:GitHub 提供的方式

    复制以下行并将其粘贴到 Git shell 中的 ~/.profile 或 ~/.bashrc 文件中:

    env=~/.ssh/agent.env

    agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

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

    agent_load_env

    # agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
    agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

    if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add ~/.ssh/one_rsa # 添加你自己的私钥
    elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add ~/.ssh/one_rsa
    fi

    unset env
    现在,当您初次运行 Git Bash 时,系统将提示您输入密码:

    > Initializing new SSH agent...
    > succeeded
    > Enter passphrase for /c/Users/you/.ssh/id_rsa:
    > Identity added: /c/Users/you/.ssh/id_rsa (/c/Users/you/.ssh/id_rsa)
    > Welcome to Git (version 1.6.0.2-preview20080923)
    >
    > Run 'git help git' to display the help index.
    > Run 'git help ' to display help for specific commands.
    ssh-agent 进程将继续运行,直到您注销、关闭计算机或终止该进程。

    ssh-agent 相关命令解释

    # 在子shell 创建并运行 ssh-agent
    ssh-agent $SHELL
    
    # 单独启动一个 ssh-agent 进程
    eval `ssh-agent`
    # 在windows 中为
    eval $(ssh-agent)
    
    # 杀死当前正在运行的代理 (注意使用条件)
    ssh-agent -k
    
    # 一般是使用默认shell进行输出
    # 强制使用 C-shell 进行输出
    ssh-agent -c
    # 强制使用 sh 进行输出
    ssh-agent -s
    
    # 进入 debug 模式
    ssh-agent -d
    
    # 指定用于生成SSH密钥指纹的算法。 有效值包括md5和sha256
    ssh-agent -E <fingerprint_hash>

    ssh-add 相关命令解释

    # 将私钥添加到 ssh-agent
    ssh-add ~/.ssh/key_name
    
    # 添加时同时指定私钥失效时间
    ssh-add -t <seconds>
    
    # 查看ssh-agent中的私钥
    ssh-add -l
    
    # 查看ssh-agent中的公钥
    ssh-add -L
    
    # 测试连接(这里是github)
    ssh -T git@github.com
    
    # 移除指定私钥
    ssh-add -d ~/.ssh/key_name
    
    # 移除ssh-agent 中的所有私钥
    ssh-add -D
    
    # 锁定ssh-agent :创建锁时需要设置密码
    ssh-add -x
    
    # 解锁 ssh-agent :输入密码
    ssh-add -X

    为私钥添加或更改密码

    $ ssh-keygen -p
    # Start the SSH key creation process
    > Enter file in which the key is (/Users/you/.ssh/id_rsa): [Hit enter]
    > Key has comment '/Users/you/.ssh/id_rsa'
    > Enter new passphrase (empty for no passphrase): [Type new passphrase]
    > Enter same passphrase again: [One more time for luck]
    > Your identification has been saved with the new passphrase.

    相关阅读和参考:

    https://www.ssh.com/ssh/agent
    https://help.github.com/en/github/authenticating-to-github/working-with-ssh-key-passphrases
    https://github.com/git-for-windows/git/wiki/Auto-launching-ssh-agent-when-git-starts
    https://www.thisfaner.com/p/ssh-agent/
  • 相关阅读:
    hdu1161 欧拉路
    ZOJ 3204 Connect them(字典序输出)
    [POJ1936]All in All
    [POJ1035]Spell checker
    [POJ2485]Highways
    [洛谷P3697]开心派对小火车
    【AIM Tech Round 5 (Div. 1 + Div. 2) 】
    What are the differences between an LES-SGS model and a RANS based turbulence model?
    How to permanently set $PATH on Linux/Unix?
    tar解压命令
  • 原文地址:https://www.cnblogs.com/litifeng/p/16090123.html
Copyright © 2020-2023  润新知