Clone A Git Repository Using A Non-Standard Port

Ok, now thats out of the way. We want to clone our Git repository to the local machine. However, the savvy and security-conscious sysadmin dude has changed the default SSH port on the server. Here’s how we clone a repository through SSH on a non-standard port.

In case you didn’t already know, the standard SSH port is 22. Often server administrators will change this in a bid to improve security. Otherwise, anyone who can type ssh root@domain.com can have a million cracks at trying to break the root password. And, if they get in; well…

Not cool.

Launch Cygwin. Did I mention that I love Cygwin? Navigate to your sites directory. I will be breaking every rule in the geeks handbook of 2010 because I’m using WAMP. So, we do the following:

But, in our case that would eventually timeout and fail. That is because Git will hook into SSH and use it to connect to the remote host. A straight piggy-back. It will attempt to connect via the default port of 22 for SSH, since we didn’t specify a different one.

Here’s the command we need in our case:

1

$ git clone ssh://user@8.8.8.8:2222/home/repo/mydomain.git .

We are explicitly telling Git to use SSH (which it would do anyway). We are also specifying a connection port with :2222, followed by the path/to/repo.

Obviously, 2222 is never a good port to use because it’s easy to guess. I’m just demonstrating the functionality here.