今天在另一台电脑上使用Git clone自己在bitbucket上的一个仓库时,出现了这样一个错误:
hero@hero:~$ git clone git@bitbucket.org:xb_liu/qt4.git
Cloning into 'qt4'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
自己只记得第一次在自己电脑上配置时也遇到了同样的问题,但由于过得时间比较久了,所以解决问题的方法也已经模糊,这次又重新解决了一次,特此记录!
闲言少叙,书归正传。配置的过程主要分为一下几步:
(1)输入ssh -v命令查看是否已经安装了SSH,如没安装,需先安装,安装的效果图大致如下,版本可能不同:
hero@hero:~$ ssh -v
OpenSSH_6.0p1 Debian-3ubuntu1.2, OpenSSL 1.0.1c 10 May 2012
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
(2)输入ssh-keygen 产生公钥和私钥对,保存在默认位置即可,通常在~/.ssh目录下;
hero@hero:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hero/.ssh/id_rsa):
/home/hero/.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 /home/hero/.ssh/id_rsa.
Your public key has been saved in /home/hero/.ssh/id_rsa.pub.
The key fingerprint is:
c2:0d:17:8c:21:88:7b:4a:45:79:79:4f:8f:07:88:c0 hero@hero
The key's randomart image is:
+--[ RSA 2048]----+
| .o++ ++o |
|. .E =.o.+ |
| .. . o + + |
|... . + o o |
|.o o S . |
|. . |
| |
| |
| |
+-----------------+
(3)输入命令 ps -e | grep [s]sh-agent 产看agent是否运行,如果没有,用ssh-agent /bin/bash 命令打开;
hero@hero:~$ ps -e |grep [s]sh-agent
1971 ? 00:00:00 ssh-agent
(4)采用 ssh-add ~/.ssh/id_rsa 命令加载你的身份认证信息;
(5)在bitbucket账户管理中添加你的公钥,将~/.ssh/id_rsa.pub的内容拷贝过去,点Add key;
(6)把你的仓库从HTTPS协议改变为SSH协议即可完成。
这样配置之后就可以了,git clone 执行正常。
hero@hero:~$ git clone git@bitbucket.org:xb_liu/qt4.git
Cloning into 'qt4'...
remote: Counting objects: 30, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 30 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (30/30), 13.76 KiB, done.
Resolving deltas: 100% (2/2), done.