美文网首页
配置多个SSH公钥,免密码连接Git服务器

配置多个SSH公钥,免密码连接Git服务器

作者: 雨桥明夜 | 来源:发表于2017-04-24 10:56 被阅读351次

    1.首先Mac上预先装有Git和openSSH。

    2.通过终端生成公钥

    ssh-keygen -t rsa
    

    3.生成GitHub对应的id_rsa_github.pub公钥和id_rsa_github私钥

    Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_github  #设置路径,如果不设置默认生成 id_rsa  和  id_rsa.pub
    

    密码可以不设直接回车跳过

    按照上面步骤生成Coding对应的id_rsa_coding.pub公钥和id_rsa_coding私钥

    4.查看系统ssh-key代理

    $ ssh-add -l
    

    如果显示The agent has no identities,表示没有ssh-key 代理
    如果有,全部删除

    $ ssh-add -D
    

    5.把 .ssh 目录下的私钥添加到ssh-key 代理

    $ ssh-add ~/.ssh/id_rsa_github
    $ ssh-add ~/.ssh/id_rsa_coding
    

    6.打开ssh 管理页面把对应的公钥保存到代码管理服务器

    cat ~/.ssh/id_rsa_github.pub | pbcopy
    

    GitHub增加公钥:https://github.com/settings/keys

    cat ~/.ssh/id_rsa_coding.pub | pbcopy
    

    Coding增加公钥:https://coding.net/user/account/setting/keys

    7.在 .ssh 目录创建 config 配置文件

    vi ~/.ssh/config
    

    输入如下配置信息
    #别名需要与Host一致

    #github
    Host github
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_github
    
    #coding
    Host coding
        HostName git.coding.net
        User git
        IdentityFile ~/.ssh/id_rsa_coding
    

    8.测试

    ssh -T github
    Hi github! You've successfully authenticated, but GitHub does not provide shel l access.
    
    ssh -T coding
    Hello coding! You've connected to Coding.net via SSH successfully!
    

    说明OK。

    9.通过设置Host别名克隆项目

    对于github帐号下的仓库:

    git clone github:githubname/repository.git
    

    (原地址是:git@github.com:githubname/repository.git,替换后应该是:github:githubname/repository.git)
    对于coding帐号下的仓库:

    git clone coding:codingname/repository.git
    

    (原地址是:git@coding.net:codingname/repository.git,替换后应该是:coding:codingname/repository.git)

    10.如果已经使用原地址克隆过了,可以使用命令修改:

    git remote set-url origin github:githubname/repository.git
    

    相关文章

      网友评论

          本文标题:配置多个SSH公钥,免密码连接Git服务器

          本文链接:https://www.haomeiwen.com/subject/bdmuzttx.html