git hub添加两个key

git hub上多个代码仓库不允许添加相同的deploy key,导致创建多个仓库后,必须要在本地创建多个public key,然后添加到git hub,比较繁琐。
本文参考:https://www.cnblogs.com/lixiaoxu/p/12532805.html

本地创建多个public key

root@592a3d1e06e8:~/hello_word_jun# ssh-keygen -t rsa -C "xxx@xxx.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.

ssh-keygen -t rsa -C "xxx@xxx.com"不带文件路径参数时,默认使用~/.ssh/id_rsa文件,因此无法添加多个key。增加-f参数后,可以自定义文件,创建多个key。

root@592a3d1e06e8:~/.ssh# ll
total 24
drwx------ 2 root root 4096 Jan  9 14:21 ./
drwx------ 1 root root 4096 Jan  9 14:21 ../
-rw------- 1 root root 2610 Jan  9 11:58 id_rsa
-rw-r--r-- 1 root root  577 Jan  9 11:58 id_rsa.pub
-rw-r--r-- 1 root root  444 Jan  9 12:20 known_hosts
root@592a3d1e06e8:~/.ssh# vi id_rsa.pub 
root@592a3d1e06e8:~/.ssh# ssh-keygen -t rsa -C "xxx@xx.com" -f 2id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in 2id_rsa
Your public key has been saved in 2id_rsa.pub
The key fingerprint is:
SHA256:9ByzDdA7qpVnsU6w2UInMMZfNr2Fgn1tsqQ8SJTWnE0 xxx@xx.com
The key's randomart image is:
+---[RSA 3072]----+
|     . .oB =Eo   |
|      = =.@ B +  |
|     . *.=+B *   |
|       .*oO*o    |
|       .SX+=.    |
|        B *      |
|       o *       |
|      .   .      |
|                 |
+----[SHA256]-----+
root@592a3d1e06e8:~/.ssh# ll
total 32
drwx------ 2 root root 4096 Jan  9 14:30 ./
drwx------ 1 root root 4096 Jan  9 14:27 ../
-rw------- 1 root root 2590 Jan  9 14:30 2id_rsa
-rw-r--r-- 1 root root  564 Jan  9 14:30 2id_rsa.pub
-rw------- 1 root root 2610 Jan  9 11:58 id_rsa
-rw-r--r-- 1 root root  577 Jan  9 11:58 id_rsa.pub
-rw-r--r-- 1 root root  444 Jan  9 12:20 known_hosts

配置不同key的用途

root@592a3d1e06e8:~/.ssh# ll
total 36
drwx------ 2 root root 4096 Jan  9 14:42 ./
drwx------ 1 root root 4096 Jan  9 14:48 ../
-rw------- 1 root root 2590 Jan  9 14:30 2id_rsa
-rw-r--r-- 1 root root  564 Jan  9 14:30 2id_rsa.pub
-rw-r--r-- 1 root root   97 Jan  9 14:42 config
-rw------- 1 root root 2610 Jan  9 11:58 id_rsa
-rw-r--r-- 1 root root  577 Jan  9 11:58 id_rsa.pub
-rw-r--r-- 1 root root  444 Jan  9 12:20 known_hosts
root@592a3d1e06e8:~/.ssh# 

创建config文件

root@592a3d1e06e8:~/.ssh# vi config 

Host 2.git
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/2id_rsa

Host 2.git只是一个标记,通过这个标记映射到HostName github.com,在git clone的时候用2.git替换github.com

把public key添加到github仓库

Screen Shot 2022-01-09 at 13.24.17.png

clone代码

从网页上复制ssh地址后,把github.com替换成2.git

root@592a3d1e06e8:/usr1/shell# git clone git@2.git:junbenjun/shell_Jun.git
Cloning into 'shell_Jun'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
root@592a3d1e06e8:/usr1/shell/shell_Jun# git remote -v
origin  git@2.git:junbenjun/shell_Jun.git (fetch)
origin  git@2.git:junbenjun/shell_Jun.git (push)

push代码

root@592a3d1e06e8:/usr1/shell/shell_Jun# git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 270 bytes | 135.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 2.git:junbenjun/shell_Jun.git
   81f5b89..10987e5  main -> main
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容