配置多个ssh-key
时间: 2023-10-06 08:06:14 浏览: 162
1. 生成新的ssh-key
使用以下命令生成新的ssh-key:
```
ssh-keygen -t rsa -C "your_email@example.com" -f ~/.ssh/new_key
```
在命令中:
- -t:指定使用的密钥类型,这里使用rsa;
- -C:指定注释,一般填写你的邮箱地址;
- -f:指定生成的密钥文件名和路径,这里生成的是~/.ssh/new_key。
随后,会提示你输入密钥的密码(可以为空)。
2. 添加新的ssh-key到ssh-agent
输入以下命令将新的ssh-key添加到ssh-agent:
```
ssh-add ~/.ssh/new_key
```
如果出现错误提示“Could not open a connection to your authentication agent”,则需要先启动ssh-agent:
```
eval "$(ssh-agent -s)"
```
再执行ssh-add命令。
3. 将新的ssh-key添加到GitHub
将新生成的公钥(~/.ssh/new_key.pub)添加到GitHub账户的SSH keys中。
4. 配置ssh config
打开~/.ssh/config文件,添加以下内容:
```
# Default GitHub
Host github.com
HostName github.com
IdentityFile ~/.ssh/github_rsa
# New SSH Key
Host github-new
HostName github.com
IdentityFile ~/.ssh/new_key
```
其中,Host和HostName填写GitHub的域名,IdentityFile填写对应的私钥文件路径。
5. 测试
输入以下命令测试新的ssh-key是否可以连接GitHub:
```
ssh -T git@github-new
```
如果显示以下信息,则说明连接成功:
```
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
```
阅读全文
相关推荐
















