配置ssh key公匙实现免密提交到多个远程仓库

2018-07-07 宋洋葱 宋洋葱

查看已经安装的公钥 C:\Users[用户].ssh

enter description here

或者可以通过git bash 查看:ls ~/.ssh

enter description here

创建一个github的key

#-t:加密算法;-f:文件名;-C:账号
ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "sxy9103@gmail.com"

操作完成后,该目录会多出 id_rsa_githubid_rsa_github.pub 两个文件。

编辑config文件,配置不同的仓库指向不同的密钥文件(不配置会使用默认的~/.ssh/id_rsa文件):vi ~/.ssh/config,例子如下:

Host github.com
    User songxueyan
    IdentityFile ~/.ssh/id_rsa_github

Host:简称(@后面的字符串)
HostName:域名或者ip(如果host填写了域名,这里可以不填)
不配置会出现错误:git@github.com: Permission denied (publickey)

enter description here

github添加刚刚生成的公钥cat ~/.ssh/id_rsa_github.pub

enter description here

使用git bash 测试是否能免密码连接ssh -vT git@github.com

git bash

如果commit时还需要密码,请在项目的config里把url改为ssh格式

ssh url

提交文件到远程仓库:

echo "# project name" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:smile365/myproject.git
git push -u origin master

参考