使用ubuntu,如果想从github上clone文件,需要做一些简单的配置,不然会出现如下错误:
> git clone [email protected]:lujiaying/MovieTaster-Open.git
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
配置的过程记录如下:
一、安装工具
1、安装ssh:
这样ubuntu才能通过ssh和其他主机通信,命令如下:
sudo apt-get install ssh
2、安装git工具,命令如下:
sudo apt-get update
sudo apt-get install git
3、首先试一下ssh是否能够连接上github,命令如下:
ssh -T [email protected]
出现错误如下:
Permission denied (publickey).
出现警告,提示缺少公钥,无法访问。
接下来配置公钥:
二、配置公钥
1、进入~/.ssh,检查本地是否有SSH Key存在
> cd ~/.ssh
> ls
2、如果不存在id_rsa和id_rsa.pub两个文件,下面就要生成新的SSH key:
首先输入:
> ssh-keygen -t rsa -C "[email protected]"
# [email protected] 是你在GitHub或者GitLab注册的邮箱
回车后终端会显示:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
# 提示你保存id_rsa 的路径是/root/.ssh/id_rsa,直接按回车
Enter passphrase (empty for no passphrase):
# 提示设置 passphrase,之后每次与 Git 通信都会要求输入 passphrase,可以设置一下,或者直接回车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:AdfQ6hyFsuhgVdMiy+4OEh6vvAQGm14j0gVZmW7YWRY [email protected]
The key's randomart image is:
3、查看~/.ssh文件夹中是否生成id_rsa和id_rsa.pub,将生成的公钥id_rsa.pub打开,如下:
> cat id_rsa.pub
ssh-rsa AAB3NzaC1yc2EAAAADAQABAAABAQChztgVQdkMJsy716t8nEclf0rhqxYPz6DrmKspD/6yf+O1eq/rkkhofyjcqyi52zNCbP39lfppGMQBkm2q19pc2478aekwBCyZrIcD/R6+6kfEUnQ829bDdS+DzZDaU61OHb3d8Yv2vs/spjI6rD94sp2J5xQ5w1GAOQGbNjDLU3HJA4viOfT4K84WSVhIgR/MXs2DOoJ75qa1UudYX7IQGgq1C/O2RL5rpcyelzHl7ptpZF2agVlV9R9IbI7AVHYzLcnxG+VGZmJk9ZiXpui2GIHe/kBryUo1/rsOLNbgBqRM0mqK8gfWBWrsx23vghrxfMV6B2+WMh [email protected]
4、将这个公钥复制添加到github中:
进入github官网,并登录:setting->SSH and GPG keys->new ssh key
将复制的key粘贴到指定的文本框中,之后单击Add SSH Key添加公钥
5、然后再试一下ssh是否能够连接上github
> ssh -T [email protected]
Hi MemoryForSky! You've successfully authenticated, but GitHub does not provide shell access.
返回如上消息,则表明链接成功,可以使用git命令操作了。