单主机多git账户多rsa密钥+根据私钥生成公钥+knownhosts+.git/config相关配置

概述

记录几个问题:

1、一台电脑上存储多个git账户生成的多个rsa密钥(私钥+公钥)

2、根据已有的私钥生成对应的公钥

3、每个仓库对应的.git/config配置文件仅对本地仓库有效,可以用于多用户、多REPO等管理

如此做之后,可以根据不同的服务器+邮箱生成对应的私钥,然后只需要保存一份私钥即可,这样子不同电脑上无需重新生成对应的新私钥+新公钥并将新公钥上传至服务器,减去切换带来的交互问题。

另外文章会记录一下knowhosts的解析,以及git config的相关使用,也会记录.git/config文件的本地地配置。

多git账户处理

首先使用 git config --list 查看全局配置:

$ git config --list 
core.symlinks=false 
core.autocrlf=true 
core.fscache=true 
color.diff=auto 
color.status=auto 
color.branch=auto
color.interactive=true 
help.format=html 
rebase.autosquash=true http.sslbackend=openssl http.sslcainfo=E:/Applications/Git/mingw64/ssl/certs/ca-bundle.crt 
filter.lfs.smudge=git-lfs smudge -- %f 
filter.lfs.process=git-lfs filter-process 
filter.lfs.required=true 
filter.lfs.clean=git-lfs clean -- %f 
user.name=xxx 
user.email=xx@xx.com 
user.name=yyy 
user.email=yy@yy.com 
core.repositoryformatversion=0 
core.filemode=false 
core.bare=false 
core.logallrefupdates=true

出现了两组 user.name/ user.email,主要是因为笔者在对应的本地仓库目录下使用该命令,本地用户为 yy/yy.com

$ cat .git/config 
[user] 
    name = yyy 
    email = yy@yy.com

而前面的 xx/xx@xx.com 则为全局配置的用户/邮箱,可以通过相关git config 指令配置。

官方 git config 指令相关链接文档: https://git-scm.com/docs/git-config。

git config 可以通过 --global 和 --local 选项配置全局和本地内容,全局内容保存在用户目录/.gitconfig中(笔者的Win7系统为该名),本地内容保存在对应目录的 .git/config文件中。

部分移除指令:

# 移除全局配置账户 
git config --global --unset user.name 
#查看全局用户名 
git config --global user.name 
# 移除全局配置邮箱 
git config --global --unset user.email 
# 查看全局邮箱 
git config --global user.email 
# 移除全局密码 
git config --global --unset user.password 
# 查看全局密码 
git config --global user.password

多rsa密钥处理

rsa密钥相关需要使用 ssh-keygen 工具,对应生成的文件都在用户目录/.ssh/ 目录下。

根据邮箱生成新rsa密钥

相关指令为 ssh-keygen -t rsa -C "a@a.com" :

$ ssh-keygen -t rsa -C "a@a.com" 
Generating public/private rsa key pair. 
Enter file in which to save the key (/c/Users/--/.ssh/id_rsa):

可以指定 id_rsa_name 去生成对应的私钥和公钥,根据步骤一直回车确认即可。

MINGW64 ~/.ssh 
$ ls 
id_rsa  id_rsa.pub  id_rsa_name  id_rsa_name.pub  known_hosts

配置.ssh/config

密钥生成之后,需要对指定服务器进行配置,否则无法正常git clone等,此时在 .ssh目录下新建 config文件,添加对应内容如下:

Host 100.111.216.233 
    HostName 100.111.216.233 
    User a 
    IdentityFile /c/Users/i5/.ssh/id_rsa_name

配置之后可以正常git clone服务器文件(服务器权限自行查看)

根据私钥生成公钥

如前文所说,保存固定私钥有利于不同电脑或其他管理的便捷,省去不必要的交互。

对应的生成指令如下:

ssh-keygen -y -f [private-key-path] > [output-path]

比如要根据上文的 id_rsa_name 生成 id_rsa_name.pub:

输入 ssh-keygen -y -f id_rsa_name > id_rsa_name.pub 即可。

http账号密码本地保存

在本地仓库的.git/config中添加下列选项即可:

[credential]         
    helper = store

如此无需每次远程操作都输入密码。 (本地密码保存应该是进行了加密,具体位置不清楚。

参考资料

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luppiter.W

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值