您的位置:首页 > 编程语言

Ubuntu 用ssh免密码登录github失败解决 (去坑版)

2016-12-16 10:55 330 查看
http://blog.csdn.net/isinstance/article/details/53690467

前言:因为笔者有两个开发环境,一个是Ubuntu16.04的虚拟机,一个是Linux Mint 18的物理机,然后Mint的好弄,基本就是按照教程来就行了,就是这个Ubuntu的有点逻辑问题,什么问题,下面说

第一步肯定是生成id_rsa.pub啦

Github官方的做法是这样的

ssh-keygen -t rsa -b 4096 -C "you_email@mail.com"


但是如果你这样在Ubuntu上做了,后面你test链接的时候就会

code@code-VirtualBox:~/Downloads$ ssh -T git@github.com
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is SHA256:------------------------.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/code/.ssh/known_hosts).
Load key "/home/code/.ssh/id_rsa": Permission denied
Permission denied (publickey).


没错,看到了没有,Permission denied

然后又看到这个P D, 加sudo

code@code-VirtualBox:~/Downloads$ sudo ssh -T git@github.com
Permission denied (publickey).


为什么会这样呢?

因为第一个是因为你没有权限去用
/home/code/.ssh/
下的pub公钥

加了sudo之后,你变root了,现在调用的是
/root/.ssh/
选的pub公钥了

所以这里有个逻辑漏洞

第二步解决

直接用
/root/.ssh/
下的公钥

先生成

code@code-VirtualBox:~/.ssh$ sudo ssh-keygen -t rsa -b 4096 -C "xxx@yyy.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:-----------------------xxx@yyy.com
The key's randomart image is:
+---[RSA 4096]----+
|                 |
|                 |
|     这里省略     |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+


然后你是进不去
/root/.ssh/


方法就是把这pub铐出来

code@code-VirtualBox:~/.ssh$ cd /root/.ssh/
bash: cd: /root/.ssh/: Permission denied


我是把它铐到Downloads目录下

code@code-VirtualBox:~/.ssh$ sudo cp /root/.ssh/id_rsa.pub /home/code/Downloads/


然后用gedit打开,复制到github上的ssh-key哪里

然后测试一下

code@code-VirtualBox:~/.ssh$ sudo ssh -T git@github.com
Hi SuperSuperSuperSuper5! You've successfully authenticated, but GitHub does not provide shell access.


说明我们链接上了,测试成功,以后push代码的时候,加上sudo就行了

ok,本次课程到此结束
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  github ubuntu ssh