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 [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 like
Host mydomain.com
Port 12345
however in cygwin, that file does not seem to exist.
This question is related to
git
version-control
ssh
cygwin
git clone ssh://[email protected]:[port]/gitolite-admin
Note that the port number should be there without the square brackets: []
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
(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, ...).
Above answers are nice and great, but not clear for new git users like me. So after some investigation, i offer this new answer.
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
It's cool, the only thing we should know is that there two syntaxes for ssh url in git
ssh://[user@]host.xz[:port]/path/to/repo.git/
[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
Source: Stackoverflow.com