233

My VPS provider recommends that I leave my SSH port to the custom port number they assign it by default (not 22). The thing is, while I know I can provide the port number when creating a remote config, it seems I can't do the same when doing a Git clone. I am using gitolite so the clone commands look like:

git clone [email protected]:gitolite-admin

Is there a way to covert this to using the custom SSH port number?

I should also mention I am running Cygwin on Windows. I have seen multiple places saying to add the custom port to the ~/.ssh/config file:

Host mydomain.example
    Port 12345

However in Cygwin, that file does not seem to exist.

3
  • 11
    The file’s pathname is .ssh/config (not conf) in your user’s home directory. It is not required for basic operation, and thus does not exist by default. You will have to create it. Try vim ~/.ssh/config (or use your preferred, installed text editor) to open/create it. Commented Apr 24, 2011 at 2:49
  • 2
    Duplicate: stackoverflow.com/questions/3596260/…
    – Kzqai
    Commented Jul 19, 2011 at 16:22
  • On Windows to read /Users/USERX/.ssh/config you need to set your HOME local environment to /Users/USERX.
    – kenorb
    Commented Mar 27, 2014 at 15:25

5 Answers 5

453
git clone ssh://[email protected]:[port]/gitolite-admin

Note that the port number should be there without the square brackets: []

10
  • 2
    This does work however Chris Johnsen correction allows for a clean url when using custom port numbers
    – ryanzec
    Commented Apr 24, 2011 at 11:34
  • 7
    :-) If you ask me, the git URL syntax is unclean, and the above is more normalized
    – Christo
    Commented Apr 24, 2011 at 11:58
  • 75
    Note that it will not work if you remove the protocol. i.e if you try the following, it will not work. git clone [email protected]:[port]/gitolite-admin Commented Aug 12, 2013 at 8:44
  • 4
    on Windows with plink, the working syntax seems to be git clone ssh://[email protected]:port/home/user/gitolite-admin Commented Nov 7, 2014 at 8:55
  • 2
    with git update to newer version 2.33.1 - there is no way to use an alternate port when ssh connect. The only solution is not to use the native git ssh client, replace it, for example, with openssh. I chuckle at the git dev group;
    – mshakurov
    Commented Nov 13, 2021 at 0:18
49

Above answers are nice and great, but not clear for new Git users like me. So after some investigation, I offer this new answer.

What's the problem with the SSH config file way?

When the config file does not exists, you can create one. Besides port the config file can include other SSH config option:user IdentityFile and so on, the config file looks like

Host mydomain.example
    User git
    Port 12345

If you are running Linux, take care the config file must have strict permission: read/write for the user, and not accessible by others

What about the SSH URL way?

It's cool, the only thing we should know is that there two syntaxes for SSH URL in Git

  • standard syntax ssh://[user@]host.xz[:port]/path/to/repo.git/
  • scp like syntax [user@]host.xz:path/to/repo.git/

By default Gitlab and GitHub will show the scp like syntax URL, and we can not give the custom SSH port. So in order to change SSH port, we need use the standard syntax

1
  • It might be worth to note that this is a GIT setting that can also be done on Windows in the file C:\Program Files\Git\etc\ssh\ssh_config. That solved the problem for me. Commented Mar 10 at 22:47
31

When you want a relative path from your home directory (on any UNIX) you use this strange syntax:

ssh://[user@]host.example[:port]/~[user]/path/to/repo

For Example, if the repo is in /home/jack/projects/jillweb on the server jill.example and you are logging in as jack with sshd listening on port 4242:

ssh://[email protected]:4242/~/projects/jillweb

And when logging in as jill (presuming you have file permissions):

ssh://[email protected]:4242/~jack/projects/jillweb

5
  • man this had be stumped for a while and was exactly what I needed to know. do you know where this is documented at?
    – wilson0x4d
    Commented Aug 21, 2018 at 23:04
  • @Shaun Wilson Sorry, I don't quite recall. But it took me a while to find the answer so I figured I should share it here, as this was one of my dead-ends. Commented Aug 22, 2018 at 23:05
  • 1
    Thanks! Especially if you are on a shared hosting plan the ~[user] part is important. This helped me clone my repo.
    – Jim Wilson
    Commented Jan 13, 2019 at 1:42
  • 2
    @ShaunWilson the path spec is a standard UNIX path. If you are logged in to a UNIX system the ~ is a shorthand for the home-directory it's useful both to save a lot of typing and since the home directory might be located in non-standard places. The above might not work on a Windows server. Commented Aug 20, 2019 at 8:04
  • @Samuel Åslund Thank you, I've updated my answer. I often forget about Windows because, ... well I don't mean to be tacky, but it's horrible in comparison. None the less, this is an important distinction. Commented Aug 21, 2019 at 19:48
23

(Update: a few years later Google and Qwant "airlines" still send me here when searching for "git non-default ssh port") A probably better way in newer git versions is to use the GIT_SSH_COMMAND ENV.VAR like:

GIT_SSH_COMMAND="ssh -oPort=1234 -i ~/.ssh/myPrivate_rsa.key" \ git clone myuser@myGitRemoteServer:/my/remote/git_repo/path

This has the added advantage of allowing any other ssh suitable option (port, priv.key, IPv6, PKCS#11 device, ...).

1
  • 3
    This GIT_SSH_COMMAND="ssh -oPort=1234 -i ~/.ssh/myPrivate_rsa.key" is perfect for us running local gitlab instance on a different port. Thanks
    – LFMekz
    Commented Jun 8, 2022 at 15:22
22

In case you are using a custom port (forwarding) for SSH, the correct solution is

git clone ssh://git@url:2222/user/repo.git

The ssh:// header is the trick.

1
  • This answer helped me set git remote add origin ssh://git@url:2222/user/repo.git after setting up the appropriate key and .ssh/config file for it. I was going crazy using https. Commented Nov 7, 2022 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.