概述
记录几个问题:
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=autocolor.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@ user.name=yyy user.email=yy@ core.repositoryformatversion=0 core.filemode=false core.bare=false core.logallrefupdates=true
出现了两组user.name/ user.email,主要是因为笔者在对应的本地仓库目录下使用该命令,本地用户为yy/。
$ cat .git/config [user] name = yyy email = yy@
而前面的 xx/xx@ 则为全局配置的用户/邮箱,可以通过相关git config 指令配置。
官方git config指令相关链接文档: https://git-/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@":
$ ssh-keygen -t rsa -C "a@" 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
如此无需每次远程操作都输入密码。 (本地密码保存应该是进行了加密,具体位置不清楚。)
参考资料
Git官方手册