一、背景
今天在linux上执行git clone git@bitbucket.org:xxxxxxxxx.git
时,一直卡住不动,等了十几分钟之后出现如下的错误:
ssh: connect to host bitbucket.org port 22: Connection timed out fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
意思是让我们检查repository是否存在,或者是否有权限访问这个repository。好吧,这个就是我创建的,怎么可能不存在呢?那就是权限的问题,那就配置ssh吧。
二、SSH配置
1. ssh目录不存在
[root@192 ~]# cd ~/.ssh -bash: cd: /root/.ssh: No such file or directory
- 解决办法,执行如下命令
[root@192 ~]# ssh localhost
- 原因:
.ssh
是记录密码信息的文件夹,如果没有登录过root的话,就没有.ssh
文件夹,因此登录 localhost ,并输入密码就会生成了。
2. 如何配置Bitbucket的ssh
- 生成RSA算法的公私钥
[root@192 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): #这里是输入公私钥存放路径,默认就行-直接enter
- 输入私钥口令
[root@192 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: 4c:80:61:2c:00:3f:9d:dc:08:41:2e:c0:cf:b9:17:69 emmap1@myhost.local The key's randomart image is: +--[ RSA 2048]----+ |*o+ooo. | |.+.=o+ . | |. *.* o . | | . = E o | | o . S | | . . | | . | | | | | +-----------------+
- 查看是否生成成功
[root@192 ~]# ls ~/.ssh id_rsa id_rsa.pub
- 开启SSH代理服务器
[root@192 ~]# eval `ssh-agent` Agent pid 9700
- 添加密钥
$ ssh-add ~/.ssh/id_rsa
- 添加config文件
[root@192 ~]# cd ~/.ssh [root@192 .ssh]# vim config
- config文件内容如下:
host bitbucket.org # git地址 User git #登录名,这里需要填写你自己的,其它可以不改 IdentityFile ~/.ssh/id_rsa #密钥路径
- 将公钥添加到Account settings
- 检验是否配置成功
[root@192 .ssh]# ssh -T git@bitbucket.org logged in as BobTom You can use git or hg to connect to Bitbucket. Shell access is disabled
3. 执行ssh -T git@bitbucket.org卡住
这里有可能是网络卡住的问题,解决办法如下:
vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 203.208.39.99 github.com 104.192.141.1 bitbucket.org
追加最后面一条就可以解决问题。
三、结果检验
[root@192 test]# ll total 0 [root@192 test]# git clone git@bitbucket.org:BobJavacfox/car_wash_api.git Cloning into 'car_wash_api'... remote: Counting objects: 170, done. remote: Compressing objects: 100% (80/80), done. remote: Total 170 (delta 76), reused 142 (delta 67) Receiving objects: 100% (170/170), 23.14 MiB | 3.10 MiB/s, done. Resolving deltas: 100% (76/76), done. [root@192 test]# ll total 4 drwxr-xr-x. 7 root root 4096 Dec 10 00:12 car_wash_api [root@192 test]#
执行速度超级快,而且不用每次都输入密码,是不是很方便。很有用是吧!!!