使用 ssh key 验证 github
使用ssh key验证github的好处就是不用每次提交代码的时候都要输入用户名和密码,因为着在一定程度上对效率有很大的影响,虽然这么做可以防止代码提交的次数过多,但这也看个人的习惯吧。
今天尝试了一下配置ssh key
OS:mac os
打开控制台,建一个目录,比如~/.ssh(其实叫什么都无所谓,网上搜的基本上都这么配置)
在该目录下输入命令:
ssh-keygen -t rsa -C "youname@example.com"
注意:双引号换成自己的邮箱,如果遇到权限问题,只需在前面加上sudo
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是公钥
然后,执行下面的命令,将生成的key添加
ssh-add id_rsa
然后将id_rsa.pub里面的内容复制下来,在github上的settings里面找到add keys,将其粘贴到key即可,title随便填
这个时候可以在控制台上测试一下
$ 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 命令,一直验证不成功,困在这里好久