一个git消费者端使用他们的邮箱账户生成多个sshkey
原创创建密钥
1.创建密钥
$ ssh-keygen -t rsa
2.输入保存ssh key 密钥的文件名 id_rsa_github 。
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa
3.输入密码两次,最低要求不低于8位。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
重复上述步骤以创建多个关键点,如您有多个git账户
id_rsa_github
id_rsa_bitbucket
id_rsa_oschina
添加到 ssh-agent
用ssh-add命令将创建的密钥添加到 ssh-agent 里
$ ssh-add ~/.ssh/id_rsa_github
$ ssh-add ~/.ssh/id_rsa_bitbucket
$ ssh-add ~/.ssh/id_rsa_oschina
可以用ssh-add -l要检查添加的效果,将出现以下提示
2048 SHA256:ueU7NSaLTzfKNMKL3EVkBKqK2/bvr/ewdNVTmBYZtxg /Users/YourName/.ssh/id_rsa_github (RSA)
2048 SHA256:xGAMHLe5Un5LLWiXp6GI84CVn23sD9g+EQBjXMQP34YA /Users/YourName/.ssh/id_rsa_bitbucket (RSA)
2048 SHA256:lJfTU9j6JfkMSM4vJpk5BdsARzkRA/d05aUnc2BdBeg /Users/YourName/.ssh/id_rsa_oschina
这证明了您之前添加的所有内容都已生效。
当然,如果这里有什么问题,你也可以使用它。 ssh-add -d 要删除一个id_rsa
保存公钥 git 服务
打开相应的git服务网站管理页面将对应的公钥提交保存到代码管理服务器(.pub 结尾)
添加 ssh 配置文件
在 ~/.ssh 目录创建 config 配置文件
nano ~/.ssh/config
添加配置信息
Host github.com
HostName github.com
User git
IdentityFile /Users/YourName/.ssh/id_rsa_github
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile /Users/YourName/.ssh/id_rsa_bitbucket
Host oschina.net
HostName oschina.net
User git
IdentityFile /Users/YourName/.ssh/id_rsa_oschina
好的,所以你可以在多个git无需担心的服务器ssh key这就是问题所在。
一、生成ssh key
1.1 生成密钥(必须)
ssh-keygen -t rsa -C "youremail@yourcompany.com"
注解:
密钥类型
可以用-t 选项
已指定。如果未指定,则使用默认内部版本。SSH-2
的RSA钥匙。此处使用的是rsa
。- 与此同时,关键中也有一个。
注释字段
,用-C
为了指定指定的注释,用户可以方便地识别密钥、指示密钥的用途或其他有用的信息。所以进入这里吧你自己的电子邮件或其他任何东西
。 - 如果你一路进入汽车(密码不能写),这将只在
~/.ssh/ 目录
下生成id_rsa 和 id_rsa.pub 两个文件
。
1.2 设置路径 (可选)
为了区分,我们设置了第一个回车后的路径,并进入第二步
id_rsa_bitbucket
id_rsa_bitbucket.pub
id_rsa_github
id_rsa_github.pub
1.3 指定秘密字符串(可选)
- 输入完成后,程序还需要输入。
一根秘密字符串(passphrase)
,空意味着没有秘密语言。然后让输入2次口令(password)
,空表示没有密码。3此时,进入下一个车厢即可完成当前步骤~/.ssh
目录中的文件已生成。 - 建议丢掉一个。这样更安全。当然,不输也没关系。任何人都不应该冒充您来修改您的代码。
之后,大概是这样的:
到目前为止,您的本地密钥对已经生成。
二、设置ssh key的代理
2.1、 首先看一下代理
ssh-add -l
若提示
Could not open a connection to your authentication agent.
那么就没有系统代理了。key,请执行以下操作
ssh-agent bash
如果系统已经有ssh-key 代理 ,可以删除
ssh-add -D
2.2、 添加私钥
ssh-add ~/.ssh/id_rsa_bitbucket
ssh-add ~/.ssh/id_rsa_github
3.添加公钥
在对应的github的ssh管理页面,添加对应的公钥(.pub 文件内容),保存到代码管理服务器。
4.添加和编辑配置文件。config
在 ~/.ssh 目录
在下面创建一个新的 config文件
touch ~/.ssh/config
添加内容
# git@bitbucket.org
Host bitbucket.org
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/github/id_rsa_bitbucket
# git@github.com
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
当然,它也可以使用。nano命令来创建和编辑。
nano ~/.ssh/config
如此,ssh将根据登录的不同域读取对应的私钥文件。
五、测试
ssh -T HostName
ssh -T git@github.com
若出现
Hi XXX! Youve successfully authenticated, but GitHub does not provide shell access.
表示成功。
若出现
permission denied (publickey)
请检查github的ssh管理中添加的公钥是否正确。
六 为不同的项目设置不同的。git账号
设置全局git所有用户git项目将使用此用户名和邮箱,设置如下:
git config --global user.name Username
git config --global user.email UserEmail
如果要在另一个项目中使用另一个git账号,为当前项目设置指定的账号和邮箱,在当前项目中设置以下命令:
git config user.name AnotherName
git config user.email AnotherEmail
使用 git config --list
您可以看到全球git配置和当前项目的git配置了
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除