使用ssh key验证github的好处就是不用每次提交代码的时候都要输入用户名和密码,由于着在必定程度上对效率有很大的影响,虽然这么作能够防止代码提交的次数过多,但这也看我的的习惯吧。git
今天尝试了一下配置ssh keygithub
OS:mac osshell
打开控制台,建一个目录,好比~/.ssh(其实叫什么都无所谓,网上搜的基本上都这么配置)dom
在该目录下输入命令:ssh
ssh-keygen -t rsa -C "youname@example.com"
注意:双引号换成本身的邮箱,若是遇到权限问题,只需在前面加上sudoide
Generating public/private rsa key pair. Enter file in which to save the key (/var/root/.ssh/id_rsa): ./id_rsa#这里填的是保存文件的路径 Enter passphrase (empty for no passphrase):#直接enter跳过 Enter same passphrase again: #同上 Your identification has been saved in ./id_rsa. Your public key has been saved in ./id_rsa.pub. The key fingerprint is: 25:68:54:4a:f1:03:ab:78:15:3f:f7:9d:f0:bf:08:ee bsn.huang@gmail.com The key's randomart image is: +--[ RSA 2048]----+ | *o. | | o O | | * * o . | | . + * . + . | | . o S . + | | . . | | . .| | . . . .| | .E . . | +-----------------+
这个时候,在.ssh目录下有两个文件测试
id_rsa id_rsa.pub
其中id_rsa是私钥 id_rsa.pub是公钥spa
而后,执行下面的命令,将生成的key添加code
ssh-add id_rsa
而后将id_rsa.pub里面的内容复制下来,在github上的settings里面找到add keys,将其粘贴到key便可,title随便填blog
这个时候能够在控制台上测试一下
$ ssh -T git@github.com The authenticity of host 'github.com (192.30.252.128)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Failed to add the host to the list of known hosts (/Users/Bsn/.ssh/known_hosts). Hi bsnwong! You've successfully authenticated, but GitHub does not provide shell access.
其余不用管,只要看到最后一行,说明验证成功了
后话:刚开始忘记执行ssh-add id_rsa 命令,一直验证不成功,困在这里很久