Git On Custom SSH Port

GitVersion ControlSshCygwin

Git Problem Overview


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 the while I know I can give the port number when create a remote config, it seems like I can't do the same when doing a git clone. I am using gitolite so I clone commands look like:

git clone git@mydomain.com: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 like

Host mydomain.com
    Port 12345

however in cygwin, that file does not seem to exist.

Git Solutions


Solution 1 - Git

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

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

Solution 2 - Git

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

1 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.com
    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

2 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

Solution 3 - Git

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

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

For Example, if the repo is in /home/jack/projects/jillweb on the server jill.com 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

Solution 4 - Git

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

Solution 5 - Git

Github has since posted a helpful article explaining how to solve this issue.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionryanzecView Question on Stackoverflow
Solution 1 - GitChristoView Answer on Stackoverflow
Solution 2 - GitJim GreenView Answer on Stackoverflow
Solution 3 - GitDaniel SantosView Answer on Stackoverflow
Solution 4 - GitearizonView Answer on Stackoverflow
Solution 5 - GitCClarkeView Answer on Stackoverflow