目录:
  1. 使用ssh_config管理ssh密钥
    1. 前言
      1. 什么是ssh_config?
        1. 生成密钥
          1. 配置
            1. 生成配置文件~/.ssh/config
            2. 配置文件示例
            3. 目标主机权限验证
          2. git仓库配置

          使用ssh_config管理ssh密钥

          阅读时间:全文 847 字,预估用时 5 分钟
          创作日期:2022-07-21
          文章标签:
           
          BEGIN

          前言

          我们用ssh公钥与git仓库做代码存储交互,提供代码存管的平台有很多,如Github、Bitbucket、Gitlab等,通常我们生成一次key后在多平台共用,但是如果是单个平台就有多个用户管理多个仓库时这种方式就不适用了。

          网上有很多这方面的教程,中文的基本上都是这个抄袭那个抄来抄去,还云里雾里,因此做下记录,提供用法来源和使用示例

          可直接访问ssh_config 🔗或者执行open x-man-page://5/ssh_config获取配置文档,建议养成看官方文档的习惯~

          什么是ssh_config?

          ssh_config顾名思义是ssh的配置文件,文件存在~/.ssh/config或者/etc/ssh/ssh_config

          配置优先级如下:

          1. 命令行选项
          2. 当前用户配置文件 (~/.ssh/config)
          3. 所有用户配置文件 (/etc/ssh/ssh_config)

          生成密钥

          首先我们要在~/.ssh目录下生成公钥和私钥,这里使用-f指定密钥文件

          ssh-keygen -t rsa -f ~/.ssh/sunday -C "邮箱地址"

          加上原先默认生成的,现在有两个密钥

          .
          ├── id_rsa
          ├── id_rsa.pub
          ├── sunday
          └── sunday.pub

          配置

          生成配置文件~/.ssh/config

          配置文件需要配置的关键key有Host、HostName、IdentityFile、PreferredAuthentications、AddKeysToAgent和User等

          1. Host([sortname] host1): 指定需要匹配的主机,后接第一个参数可设置为简称,其余为匹配规则
          2. HostName: 指定要登录的真实主机名,可以为IP地址
          3. IdentityFile: 指定认证文件,如果不设置则自动查找~/.ssh/id_rsa~/.ssh/id_ecdsa~/.ssh/id_ecdsa_sk~/.ssh/id_ed25519~/.ssh/id_ed25519_sk~/.ssh/id_dsa
          4. PreferredAuthentications: 指定客户端尝试身份验证,如password表示密码验证,publickey表示公钥认证
          5. User: 指定要登录的用户,需要注意的是,github只能设置为git
          6. Port: 指定端口号,默认为22
          7. AddKeysToAgent(yes|no): 是否将密钥自动添加到ssh-agent,在旧版本是默认有的
          8. IgnoreUnknown: 由于ssh版本不同存在字段有无的问题,如旧版本没有AddKeysToAgent,可使用该配置避免报错

          配置文件示例

          使用#添加注释

          # 别名为default,匹配github用户gh_user1的代码仓库
          Host default gh_user1.github.com
            # github的主机名称
            HostName github.com
            # 认证文件为id_rsa
            IdentityFile ~/.ssh/id_rsa
            PreferredAuthentications publickey
            AddKeysToAgent yes
            # github的User必须为git
            User git
          
          # 别名为sunday,匹配github用户pysunday的代码仓库
          Host sunday pysunday.github.com
            HostName github.com
            # 认证文件为sunday
            IdentityFile ~/.ssh/sunday
            PreferredAuthentications publickey
            AddKeysToAgent yes
            User git
          
          # 别名为bit,匹配bitbucket用户bt_user1的代码仓库
          Host bit bt_user1.bitbucket.org
            # bitbucket的主机名称
            HostName bitbucket.org
            IdentityFile ~/.ssh/id_rsa
            PreferredAuthentications publickey
            AddKeysToAgent yes
            # bitbucket的User为实际用户名
            User bt_user1

          目标主机权限验证

          执行ssh 别名验证是否有权限与仓库主机交互

          1. Github返回Hi gh_user1! You've successfully authenticated表示校验成功
          2. bitbucket返回You can use git to connect to Bitbucket.表示校验成功
           $ ssh default      
          PTY allocation request failed on channel 0
          Hi gh_user1! You've successfully authenticated, but GitHub does not provide shell access.
          Connection to github.com closed.
          
           $ ssh sunday                                                                     
          PTY allocation request failed on channel 0
          Hi pysunday! You've successfully authenticated, but GitHub does not provide shell access.
          Connection to github.com closed.
          
           $ ssh bit   
          PTY allocation request failed on channel 0
          authenticated via ssh key.
          
          You can use git to connect to Bitbucket. Shell access is disabled
          Connection to bitbucket.org closed.

          git仓库配置

          我们匹配的规则是通过Host的配置完成的,因此项目中的.git/config配置也要符合要求,及主机名前面加上用户名

          拉取新项目:git clone git@User.HostName:User/ProjectName.git

          已有项目修改.git/config中的url字段在HostName前面加上用户名

          FINISH

          随机文章
          人生倒计时
          default