window 平台 git 配置多个账号的 ssh-key

window 平台 git 配置多个账号的 ssh-key

配置单个账号的 ssh-key

创建一个 gitlab 的 ssh-key

ssh-keygen -t rsa -C "邮箱账号"
# 然后一路按回车键

然后会在: ~/.ssh/ 目录下看到生成的秘钥文件, 密钥文件一般存放在: C:\Users\username\.ssh 目录下

# 查看秘钥
cat ~/.ssh/id_rsa

把秘钥在 gitlab 上添加好了后, 验证是否配置成功

$ ssh -T [email protected]
Hi ****! You've successfully authenticated, but Gitlab does not provide shell access.

配置多个账号的 ssh-key

比如说在上一步的基础上, 再创建一个 github 的 ssh-key

  1. 第一步, 生成秘钥

    ssh-keygen -t rsa -C "邮箱账号"
    # 回车后会要求输入秘钥文件的名字, 为了作区分, 可以自己定义一个名字
    Enter file in which to save the key (/c/Users/Gato/.ssh/id_rsa): github_private_id_rsa
    # 然后一路按回车
    
  2. 创建配置文件(命令行操作也可换成手动创建, 秘钥存放位置一般在 C:\Users\username\.ssh 文件夹下)

    # 进入到秘钥文件夹下
    cd ~/.ssh/
    # 创建配置文件, 进入到 vim 编辑器 
    vim config
    

    config 文件配置(IdentityFile 改成自己密钥文件的实际位置):

    # ~/.ssh/config 配置多个git的ssh-key
    # 第一个默认的SSH Key
    Host gitlab.com
    HostName gitlab.com
    IdentityFile C:\Users\username\.ssh\id_rsa
    PreferredAuthentications publickey
    
    # 第二个SSH Key
    Host github.com
    HostName github.com
    IdentityFile C:\Users\username\.ssh\gitlab_private_id_rsa
    PreferredAuthentications publickey
    
  3. 验证是否配置成功

    $ ssh -T [email protected]
    Hi ****! You've successfully authenticated, but GitHub does not provide shell access.
    

猜你喜欢

转载自blog.csdn.net/zjhcxdj/article/details/105511153