使用git clone命令从github上同步github上的代码库时,如果使用SSH链接(如我自己的beagleOS项 目:git@github.com:DamonDeng/beagleOS.git),而你的SSH key没有添加到github帐号设置中,系统会报下面的错误:

 

Permission denied (publickey).

fatal: The remote end hung up unexpectedly

 

这时需要在本地创建SSH key,然后将生成的SSH key文件内容添加到github帐号上去。

创建SSH key的方法很简单,执行如下命令就可以:

ssh-keygen

然后系统提示输入文件保存位置等信息,连续敲三次回车即可,生成的SSH key文件保存在中~/.ssh/id_rsa.pub

 

然后用文本编辑工具打开该文件,我用的是vim,所以命令是:

vim ~/.ssh/id_rsa.pub

 

接着拷贝.ssh/id_rsa.pub文件内的所以内容,将它粘帖到github帐号管理中的添加SSH key界面中。

打开github帐号管理中的添加SSH key界面的步骤如下:

1. 登录github

2. 点击右上方的Accounting settings图标

3. 选择 SSH key

4. 点击 Add SSH key

在出现的界面中填写SSH key的名称,填一个你自己喜欢的名称即可,然后将上面拷贝的~/.ssh/id_rsa.pub文件内容粘帖到key一栏,在点击“add key”按钮就可以了。

添加过程github会提示你输入一次你的github密码

 

添加完成后再次执行git clone就可以成功克隆github上的代码库了。

 




cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory

ls
# Lists all the subdirectories in the current directory
# config  id_rsa  id_rsa.pub  known_hosts

mkdir key_backup
# Makes a subdirectory called "key_backup" in the current directory

cp id_rsa* key_backup
# Copies the id_rsa keypair into key_backup

rm id_rsa*
# Deletes the id_rsa keypair

ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key using the provided email
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/you/.ssh/id_rsa):

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Your identification has been saved in /home/you/.ssh/id_rsa.
Your public key has been saved in /home/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

sudo apt-get install xclip
# Downloads and installs xclip

xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard