1. 配置用户名和邮箱
git config --global user.name "grace"
git config --global user.email "grace@163.com"
2. 生成并查看密钥
ssh-keygen -t rsa -C "email1@xxx.com"
ssh-keygen -t rsa -C "email2@xxx.com"
生成密钥的时候不要一路回车,需要设置不同的文件名
3. 配置config
3.1 在 ~/.ssh/目录下新建文件
touch config
3.2 配置ssh的路由策略
在config文件中配置一下内容
# github
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# gitee.com
Host gitee.com
HostName gitee.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_gitee_rsa
Host
: 相当于一个别名,通过他替代将要连接的服务器地址。可以是任意的字符。访问ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的~/.ssh/ssh_config
配置信息。HostName
:真正连接的服务器地址User
:自定义的用户名PreferredAuthentications:
指定优先使用哪种方式验证,支持密码和秘钥验证方式IdentityFile
:指定本次连接使用的密钥文件
4. 密钥生成成功,在代码仓库配置sshkey
cat /Users/.ssh/id_gitee_rsa.pub
添加公钥
5. 验证
ssh -T git@gitee.com