Git on custom SSH port
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.
git clone ssh://git@mydomain.example:[port]/gitolite-admin
Note that the port number should be there without the square brackets: []
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
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
(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, ...).
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.
Github has since posted a helpful article explaining how to solve this issue.
精彩评论