219

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 git@mydomain.example: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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
ryanzec
  • 27,284
  • 38
  • 112
  • 169
  • 10
    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. – Chris Johnsen Apr 24 '11 at 02:49
  • 2
    Duplicate: http://stackoverflow.com/questions/3596260/git-remote-add-with-other-ssh-port – Kzqai Jul 19 '11 at 16:22
  • On Windows to read `/Users/USERX/.ssh/config` you need to set your `HOME` local environment to `/Users/USERX`. – kenorb Mar 27 '14 at 15:25

5 Answers5

430
git clone ssh://git@mydomain.example:[port]/gitolite-admin

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Christo
  • 8,729
  • 2
  • 22
  • 16
  • This does work however Chris Johnsen correction allows for a clean url when using custom port numbers – ryanzec Apr 24 '11 at 11:34
  • 7
    :-) If you ask me, the git URL syntax is unclean, and the above is more normalized – Christo Apr 24 '11 at 11:58
  • I'll admit that the syntax you given is more descriptive but having to remember to add in the custom port number is annoying. Configuring that port number and then not having to remember that is a bit easier but to each their own (that's why it can be done both ways) :) – ryanzec Apr 24 '11 at 12:11
  • 71
    Note that it will not work if you remove the protocol. i.e if you try the following, it will not work. `git clone git@mydomain.com:[port]/gitolite-admin` – Bijay Rungta Aug 12 '13 at 08:44
  • 4
    on Windows with plink, the working syntax seems to be `git clone ssh://git@mydomain.com:port/home/user/gitolite-admin` – Roman Plášil Nov 07 '14 at 08:55
  • WARN: This answer was the correct one at the time of writing. Many years have past since then (And Google still redirects to this page). Look at my proposed solution updated to 2018. – earizon Oct 05 '18 at 12:20
  • See also my update solution using GIT_SSH_COMMAND environment variable. It allows for further customization and is more "DevOps friendly", allowing to customize the behaviour in (inmutable) container images, where, (maybe) it is not possible to directly change the git command line or any configuration file. – earizon Jul 13 '20 at 10:29
  • 1
    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 Nov 13 '21 at 00:18
  • `git clone ssh://luuk@nas:[PORT]/volume1/Share/GIT/repo` gives: "fatal: unable to access 'https://nas:[PORT]/volume1/Share/GIT/.../': OpenSSL/1.1.1s: error:1408F10B:SSL routines:ssl3_get_record:wrong version number" (Where `[PORT]` is the port number I use for SSH. So, using a different port than 22 is currently no longer an option. – Luuk Mar 25 '23 at 18:29
43

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Jim Green
  • 531
  • 4
  • 6
28

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://jack@jill.example:4242/~/projects/jillweb

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

ssh://jill@jill.example:4242/~jack/projects/jillweb

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Daniel Santos
  • 3,098
  • 26
  • 25
  • man this had be stumped for a while and was exactly what I needed to know. do you know where this is documented at? – Shaun Wilson Aug 21 '18 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. – Daniel Santos Aug 22 '18 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 Jan 13 '19 at 01: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. – Samuel Åslund Aug 20 '19 at 08: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. – Daniel Santos Aug 21 '19 at 19:48
21

(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, ...).

earizon
  • 2,099
  • 19
  • 29
  • 1
    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 Jun 08 '22 at 15:22
17

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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44
  • 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. – Arkham Angel Nov 07 '22 at 00:06