Git-SSH配置

Git-SSH配置(来自个人早期的OneNote笔记,目前已经迁移到语雀);语雀查看链接

Git 连接(可创建一个 Key 同时使用GitEE、GitHub、GitLab等)

创建 SSH key

将邮件地址替换为你 Gitee 或者 Github 使用的邮件地址(GitHub 和 GitEE 的邮箱地址相同也没事)

**ssh-keygen -t rsa -C "xxx@xxx.com"

进入到 .ssh 路径下:

  • Windows: **~/.ssh (即为路径:C:\Users\你电脑用户名\.ssh)
  • Linux: ** ${HOME}/.ssh
# Window系统为 
cd ~/.ssh
# Linux 系统则为
cd ${HOME}/.ssh

# 进到用户家的目录下的 .ssh 路径下
$ ~/.ssh
ssh-keygen -t rsa -C "chxlay@gmail.com"

这一步如果默认回车,会生成名为 id_rsa 的文件,你可以输入不同的名字来便于识别文件,比如生成 Gitee 的 ssh key 可以设置为 id_rsa_gitee,设置 Github 的 ssh key 可以设置为 id_rsa_github ,我这里设置为 id_rsa_github,你也可以直接命名为 id_rsa_git 同时使用到 github、gitee、gitlab 共用了。

这里重命名是非必须的,你也可以直接回车使用默认的key 命名,然后一路回车

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Dell/.ssh/id_rsa):id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github
Your public key has been saved in id_rsa_github.pub
The key fingerprint is:
SHA256:6rrngVpF748P6tI6NUBkJTeTaFattYtvW3c2197YQjU chxaly@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
|   .++*o         |
|   .=o.oo        |
|   +  .o .       |
|    .....      E.|
|     ...S.     ..|
|     o+o.     . .|
|    ooooo . ..+ o|
|   oo.ooo= . o.*.|
|  . +X=.ooo   ..+|
+----[SHA256]-----+
 
$Dell@DESKTOP-KD6I8AQ MINGW64 ~/.ssh

成功生成SSH秘钥文件

添加 Key到Git

将以上创建的 public key 添加到 Git 中,找到用户目录下的 .ssh 文件夹,查看并复制创建好的 id_rsa_gitee.pub 或 id_rsa_github.pub 的内容。

可以直接编辑器打开或者命令查看 cat id_rsa_github.pub

$ cat id_rsa_github.pub
ssh-rsa 
AAAAB3NzaC1yc2EAAAADAQABAAABgQCedndnjnSv1kL1mjz+20/DXROCLuF5o9nrMasKhXlkOr3Zvbz
oH+uBpKAijVXiTgQNRQoemzdw8jEFb2NrOLVFKOqBhsZpyEJYQybhQ1WOR7Poli4iD20K/hwIxe3uizsu
9JQlrOqU5Piaieoc9A8B3mETA55bjRm0zmTdGm/T1o3iPZljdSjTRlfmKQ9pRmU0wWcICuZOv8qgVOWz
5l4JdnPDK7RFNf5bqeohUWV9IABmHh29DcLllARTNjfZpXHN3T3b/MVjtCw6kJEbPGc+ASmCdqBFZ0H
m2tByx6gnVLGqz4RmCWtQNnhSMlWzfSHEO34JgdHDh/z8+LTr+Yh5sMYSOHTpLZvX6+gvImEFoeyN0tFN4
zaQI2awcU7iNTy0oYNh2OyTrho4kksGftmtjxUvpfZGXigfH+6ohG/Xx5DUNbjznvaXACuIy2dWgOZpH9M
B8RIBE46DD6/FO/YIJI5eWstFOQlN9LhgU+HmhI26hkB/PsJlKBYfn13EWhE= chxaly@gmail.com
 
Dell@DESKTOP-KD6I8AQ MINGW64 ~/.ssh

配置Git SSH Keys

  • GitHub

  • GitEE

添加配置文件

内容如下:

gitlab.yourdomain.com 更换为你自己的gitlab域名

# gitee码云
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_git
IdentitiesOnly yes
 
#  GitHub
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_git
IdentitiesOnly yes

#  GitLab
Host gitlab.yourdomain.com
HostName gitlab.yourdomain.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_git
IdentitiesOnly yes

连接测试

  • GitHub
ssh -T git@github.com

The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Hi chxlay! You've successfully authenticated, but GitHub does not provide shell access.
  • GItEE
ssh -T git@gitee.com

Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
Hi chxlay! You've successfully authenticated, but GITEE.COM does not provide shell access.
  • GitLab

gitlab.yourdomain.com 更换为你自己的gitlab域名

ssh -T git@gitlab.yourdomain.com
posted @ 2025-01-09 11:41  Vermeer  阅读(50)  评论(0)    收藏  举报