19

I have a strange problem with cloning a git repository from an amazon ec2 server. It works without any problems on one of my computers running ubuntu 12.04, while on another one using 12.10 it gives me an error:

ssh: Could not resolve hostname ec2server: Name or service not known  
fatal: The remote end hung up unexpectedly

It's like it is not recognizing my config file. I use the following git command to clone :

sudo git clone ec2server:/var/www/project.git

or

sudo git clone ec2xxx.compute-1.amazonaws.com:/var/www/project.git

The two config files are identical on both computers, inside the ~./ssh with the following content:

Host ec2server
   Hostname ec2XXX.compute-1.amazonaws.com
   User ubuntu
   IdentityFile ~/.ssh/mykey.pem

If I substitute the ec2server with the actual address I get the following error:

Cloning into 'project'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Thanks in advance.

Pio
  • 4,044
  • 11
  • 46
  • 81

2 Answers2

23

It usually is a permission issue.
The chmod on the parent directories of your config file might be different between your two computers.
(and I am not talking about just the immediate parent directory .ssh, but also all the parent directories)

See "Git SSH authentication", but also know that if any of the parent directories is writable for group or world, ssh won't work.


Note also that your second command is not right, and should be:

git clone ubuntu@ec2xxx.compute-1.amazonaws.com/var/www/project.git

no ':' (a ':' means using a config file, with an scp-like syntax)

it can only work if you have ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub though.
If you have mykey.pem, then you need a config file for ssh to know where are your public and private keys, which means only this can work:

git clone ec2server:/var/www/project.git

One other chack (after this thread and this forum) is to check if there is any DNS/DHCP issue (a bit like in "Working with git behind a dynamic DNS").

Host ec2server
   Hostname 1xx.xxx.xxx.xxx # ip address of ec2XXX.compute-1.amazonaws.com
   User ubuntu
   IdentityFile ~/.ssh/mykey.pem
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • But the parrent directory is the home directory – Pio Jan 20 '13 at 10:45
  • @Pio You mean your `config` file in in `/home/yourLogin`, instead of `/home/yourLogin/.ssh`? – VonC Jan 20 '13 at 10:46
  • @Pio see http://stackoverflow.com/a/10042145/6309 or http://stackoverflow.com/a/8172628/6309 for examples of config files for ssh. – VonC Jan 20 '13 at 10:48
  • @Pio then, my answer stands. – VonC Jan 20 '13 at 10:51
  • Yes, the permissions were different. I made them to be the same. But still, the same error pops up. I think there might be a misconfiguration in git since it does not recognize the hostname of the server if I do not explicitly use it. – Pio Jan 20 '13 at 10:56
  • @Pio Just fyi `~` is a shortcut for the current users home directory (e.g. `/home/youruser/`). Are you able to SSH into the box using this key and username? (e.g. using [Putty](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) from a windows machine) – Michel Feldheim Jan 20 '13 at 11:13
  • Yes, I am aware of the meaning of ~ . I am also able to log into the amazon ec2 server via ssh. It works perfectly. – Pio Jan 20 '13 at 11:23
  • @Pio I have edited my answer: can you check if `git clone ubuntu@ec2xxx.compute-1.amazonaws.com/var/www/project.git` works on both your computers? (source: http://kuttler.eu/post/git-clone-ssh-could-not-resolve-hostname/ or http://yuanjie.name/entry/git-complains-ssh-hostname-resolving) – VonC Jan 20 '13 at 11:49
  • I tried it, but it is not working. The syntax is not right: since like this it searches for a repository named ubuntu@ec2xxx.compute-1.amazonaws.com/var/www/project.git and gives me the error of not finding the repository. If I put : between then I get the error mentioned in my post of permission denied. – Pio Jan 20 '13 at 11:56
  • @Pio I understand. It is best then to stick with the scp-syntax and your config file. – VonC Jan 20 '13 at 11:58
  • @Pio following https://openshift.redhat.com/community/forums/openshift/ssh-connection-closed-by, would using the IP instead of the amazon server name works better? I have edited the answer to mention that possibility. – VonC Jan 20 '13 at 12:03
  • 1
    After reading 100 different articles, using "git clone ec2server:/var/www/project.git" solved it for me. THANK YOU. – Marina Martin Apr 07 '13 at 17:47
6

Recently I had to re-install the system which held the working configuration and I have not managed to replicate it, so it's not about the linux version (very likely). What I managed to do, is that I managed to use the mentioned original script to clone the repository into my home directory. Here it picked up the name correctly, but if I navigated into /var/www, what I was doing before, it just gave me the same error: could ec2server not found ... . So I figure the problem has to do something with the permissions + commands combination. If anyone can figure it out how to make it work, I will mark he's response as a correct answer, until then I am marking mine as the correct one, since this is the closest to the correct.

UPDATE

I figured it out what was the problem: I had to change the permission on /var/www folder in order to be able to clone into that directory. Now it's 777 ( used only for dev on a local machine).

Pio
  • 4,044
  • 11
  • 46
  • 81