Per git docs the following syntax is supported for git clone <directory>
command:
The following syntaxes may be used
ssh://[user@]host.xz[:port]/path/to/repo.git/
An alternative scp-like syntax may also be used with the ssh protocol:
[user@]host.xz:path/to/repo.git/
and I have in the ssh config file the following:
Host *
PubkeyAcceptedKeyTypes=+ssh-rsa
HostKeyAlgorithms=+ssh-rsa
Host aws
User ubuntu
HostName ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com
IdentityFile /Users/xxxxxxxx/.ssh/aws.pem
Port 22
Here is the result of each git command I tried:
sudo git clone ssh://ubuntu@ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com/var/www/.git
result: successsudo git clone ssh://ubuntu@aws/var/www/.git
result: Could not resolve hostname aws: nodename nor servname provided, or not known
sudo git clone ssh://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com/var/www/.git
result:fatal: protocol error: bad line length character: Plea
sudo git clone ssh://ubuntu@xxx.xxx.xxx.xxx/var/www/.git
result:successsudo git clone ssh://xxx.xxx.xxx.xxx/var/www/.git
result:same error as #3sudo git clone ssh://ubuntu@example.com/var/www/.git
result:successWhat I tried is above in the question, and am expecting the obvious result of the syntax to work as indicated in the doc but it did not.
can I use hostname from ssh config in git clone?
Not only you can — it's the recommended way.
sudo git clone ssh://ubuntu@aws/var/www/.git
Here is the main problem — you use sudo
and it runs git clone
under a different user root
so ssh
(running from git
) doesn't get access to your ~/.ssh/config
. Do not use sudo
with git
, it's almost never required and very often leads to problem like this.
why no password or private key was ever asked ?
Because ssh
running under root
cannot resolve the name and cannot connect. No need to authenticate.
PS. Additional advice: put the section under Host *
in your ~/.ssh/config
at the end of the file; ssh
reads config in different order — first executed command wins. So first put the most specific commands, put Host *
last.