最近在push代码的时候时常会遇到问题,去google下,把别人的解决办法一个一个试,糊糊涂涂地解决了。
下次遇到的时候,还是要去google,原因是我不知道错误的真正原因,今天算是明白了点,所以我要把我明
白的东西记录下来,希望下次遇到的时候,能够快速解决。
ssh机制,简单的说,要使本地主机和远程服务器通过ssh连接,需要一对密钥,一个是私钥,存在于本地主
机,另一个是公钥,放在远程服务器,连接前还要确认ssh服务开启了没有(windows上运行的是ssh-agent.exe)
下面是我在使用git和heroku时曾遇到的问题
Permission denied (publickey).
fatal: The remote end hung up unexpectedly.
Permission denied (publickey).
fatal: Could not read from remote repository.
解决办法:请确保以下2步均做到
1、远程服务器已经有了对应于本地主机上公钥
2、本地主机ssh服务开启(windows上是ssh-agent.exe运行),并且本地主机私钥要包含在ssh服务列表中
具体细节:
为远程服务器(heroku)添加公钥的方法:可以到~/.ssh 下查看是否存在密钥对,如id_rsa和id_rsa.pub,可以
直接运行
heroku keys:add
如果不存在的话,打命令生成一对
ssh-keygen -t rsa
然后再
heroku keys:add
检查本地私钥是否存在于ssh服务中
eval `ssh-agent -s`
如果运行这句出现Could not open a connection to your authentication agent,那么就先运行
ssh-agent bash
再运行
ssh-add -l
如果不存在,需要添加进去
ssh-add ~/.ssh/id_rsa
如果是push到git的话,给git添加公钥的话,可以手动把~/.ssh/id_rsa.pub的内容复制好,然后如下
Now that you have the key copied, it's time to add it into GitHub:
- In the user bar in the top-right corner of any page, click Account settings.
- Click SSH Keys in the left sidebar.
- Click Add SSH key.
- In the Title field, add a descriptive label for the new key. For example, if you're using a personal Mac, you might call this key "Personal MacBook Air".
- Paste your key into the "Key" field.
- Click Add key.
- Confirm the action by entering your GitHub password.
解决该问题方法链接如下,上面的内容都可以在下面的链接中找到(看链接最后几个单词可以知道该链接的内容)
http://www.cnblogs.com/sheldonxu/archive/2012/09/17/2688281.html
https://help.github.com/articles/error-permission-denied-publickey
https://help.github.com/articles/generating-ssh-keys
https://devcenter.heroku.com/articles/keys#adding-keys-to-heroku
http://stackoverflow.com/questions/4269922/permission-denied-publickey-when-deploying-heroku-code-fatal-the-remote-end