专业的编程技术博客社区

网站首页 > 博客文章 正文

Git ssh 多 key 配置(git ssh keygen)

baijin 2024-10-10 04:22:35 博客文章 8 ℃ 0 评论

1. 查看配置的 SSH-Key

cd ~/.ssh
ls
    id_rsa
  id_rsa.pub
  known_hosts


2. 生成 SSH-Key

生成多个重复以下步骤

# 生成默认的
ssh-keygen -t rsa -C "example@email.com"

# 生成指定为 github 的 ssh key
ssh-keygen -t rsa -C "example@email.com" -f ~/.ssh/id_rsa_github

# 生成指定为 gitlab 的 ssh key
ssh-keygen -t rsa -C "example@email.com" -f ~/.ssh/id_rsa_gitlab


3. 配置config

在 ~/.ssh 目录下新建一个config文件

# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
 
# gitlab
Host gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlab


4. 添加 SSH-Key

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

ssh-agent bash

ssh-add ~/.ssh/id_rsa_github


5. 复制 SSH-Key

可以直接找到 .ssh 目录下生成的 id_rsa.pub 文件,使用 vscode 打开后复制文本

# 复制 github 的 ssh key
cat ~/.ssh/id_rsa_github

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表