Mac下生成ssh keys连接Github

所属:Tutorials, Mac

Mac下一般都自带安装了git,在这就不继续说如何安装git了.

检查是否存在ssh keys

1
$ cd ~/.ssh

如果显示No such file or directory,则直接跳到第三步去

如果存在.ssh目录,请先备份好你的ssh keys

1
2
3
mkdir ssh_key_backup // 创建备份
cp id_rsa* ssh_key_backup // 移动到备份文件夹下
rm id_rsa*

生成一个新的ssh keys

1
2
3
4
$ ssh-keygen -t rsa -C “your_email@example.com” // 输入您的邮箱
# Creates a new ssh key, using the provided email as a label
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] // 按下Enter键即可
  • 按完Enter键后,会出现下面的提示
1
2
Enter passphrase (empty for no passphrase): [Type a passphrase] // 输入密码
Enter same passphrase again: [Type passphrase again] // 再次输入密码确认
  • 接着会出现类似下面的提示
1
2
3
Your identification has been saved in /Users/you/.ssh/id_rsa.
Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
The key fingerprint is:01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

说明已生成成功,在 ./ssh 目录下面会多出 id_rsa id_rsa.pub 这两个文件

GitHub网站上添加你的ssh keys

  • 找到刚才生成的 id_rsa.pub,复制里面的内容
  • 打开 GitHub 网站并登录,找到 “Account Settings”,点击 “SSH Keys”,然后”Add SSH key”, 把刚才id_rsa.pub里面的内容粘贴进去就可以了

验证你的GitHub连接

1
$ ssh -T git@github.com

然后有又一堆提示,输入yes就可以了

最后验证成功的话,会出现类似下面的提示

1
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

大功告成..!
少年觉得如何?简单吧,使劲的折腾吧..!

####参考资料