生成的秘钥可以同时在osc和GitHub上同时使用
-
Generating a new SSH key
- 填写邮箱
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- 填写秘钥文件名称
Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
- 填写密码
Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
- 填写邮箱
-
Adding your SSH key to the ssh-agent
- Ensure the ssh-agent is running:
eval $(ssh-agent -s)
- Add your SSH private key to the ssh-agent
ssh-add ~/.ssh/<私有秘钥文件>
- Ensure the ssh-agent is running:
-
Add the SSH key to your GitHub account.
- 复制公有秘钥到GitHub上
clip < ~/.ssh/秘钥文件.pub
-
解决Windows下使用github需要每次都要ssh-add的问题
在 .ssh 目录下创建一个 config 文本文件,每个账号配置一个Host节点,主要配置项说明:
Host # 主机别名
HostName # 服务器真实地址
IdentityFile # 私钥文件路径
PreferredAuthentications # 认证方式
User # 用户名
config的内容
# ~/.ssh/config 配置多个git的ssh-key
# 第一个默认的SSH Key
Host github.com
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa
PreferredAuthentications publickey
# 第二个SSH Key
Host gitee.com
HostName gitee.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa
PreferredAuthentications publickey
-
只对当前窗口有效
将下面的配置复制粘贴到git的bash.bashrc文件中
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add
fi
unset env
-
终端测试SSH Key
通过终端测试SSH Key是否生效,分别输入以下命令:
ssh -T git@github.com
ssh -T git@gitee.com