美文网首页
MAC下配置多个SSH-KEY

MAC下配置多个SSH-KEY

作者: Kevin_bin | 来源:发表于2017-08-25 22:45 被阅读314次

    日常工作中难免遇到开发环境中需要配置不同的ssh-key对应不同的环境,如同时在gitlab、github上项目在开发。下面我们来看看具体的操作:

    • 生成不同的SSH-Key

    $ ssh-keygen -t rsa -C "youremail@yourcompany.com” -f ~/.ssh/id_rsa_github

    在~/.ssh/目录会生成id_rsa_github和id_rsa_github.pub私钥和公钥。 我们将id_rsa_github.pub中的内容粘帖到github服务器的SSH-key的配置中.

    • 添加私钥

    $ ssh-add ~/.ssh/id_rsa_github

    如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:

    $ ssh-agent bash

    然后再运行ssh-add命令。

    # 可以通过 ssh-add -l 来确私钥列表
    $ ssh-add -l
    # 可以通过 ssh-add -D 来清空私钥列表
    $ ssh-add -D
    
    • 修改配置文件
      在 ~/.ssh 目录下新建一个config文件
      touch config
      在config中添加如下内容 vim config 添加内容:
    # 添加config配置文件
    # 配置文件参数
    # Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件
    # HostName : 要登录主机的主机名
    # User : 登录名
    # IdentityFile : 指明上面User对应的identityFile路径
    
    # github
    Host Build software better, together
        HostName Build software better, together
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa_github
    # 按此格式可添加多个
    
    • 目录结构
    kevin@bogon:~/.ssh|⇒  pwd
    /Users/kevin/.ssh
    kevin@bogon:~/.ssh|⇒  ls -al
    total 32
    drwx------   6 kevin  staff   204  8 25 18:55 .
    drwxr-xr-x+ 65 kevin  staff  2210  8 25 22:10 ..
    -rw-r--r--   1 kevin  staff   421  8 25 18:55 config
    -rw-------   1 kevin  staff  1679  8 25 18:37 id_rsa_github
    -rw-r--r--   1 kevin  staff   399  8 25 18:37 id_rsa_github.pub
    

    相关文章

      网友评论

          本文标题:MAC下配置多个SSH-KEY

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