GIT: USING A NONSTANDARD SSH PORT ON A REMOTE REPOSITORY
Overview
Remote hosts sometimes require us to use non-standard ssh ports (namecheap hosting generally uses port 21098). It can be a problem figuring out how to specify this port with some applications. SSH has solved this and this article will explain how (on *NIX) systems.
Modifying the SSH config
Typically SSH defaults to port 22. But for specific hosts you may need another port and you don't want to have to figure out how to specify an alternative port with the utility you are using (and in some cases it may not work). For some utilities it is not standard and do you have the time to experiment. The 'git' utility is one example and it may vary based on system age and *NIX environment. In this case we want to add a remote repository but it can only be accessed via SSH and only on port 21098... How do you specify that. It varies. But if you want to be able to use a "standard" command like below, you need to modify the SSH config file (see below).
$ git remote add origin <user>@<server>:/<path-to-repository-directory>
$ git ls-remote
... (hangs in limbo, can't connect)
It hung because it was trying to connect to port 22, but port 22 is not listening. Some have suggested that you can add the port after the "<server>:<port>/<path-to...>" but that does not always work. The solution we provide ALWAYS works and is 100% compatible with everything.
$HOME/.ssh/config
If you do not have this file, just create it and the affects are immediate. No restart is needed, nothing.
This is what my $HOME/.ssh/config file looks like:
$ cat $HOME/.ssh/config
Host cvs.company.com
Port 21098
Host *
Port 22
$
This says, for the host "cvs.company.com" use Port 21098 (so you no longer have to specify) and then it says for ALL (*) other hosts, use port 22. Any variation you would put above the last ("Host *") definition.
And you can find out more about modifying this file with the MAN page:
$ man 5 ssh_config
Namecheap... and others-- my .ssh has bad permissions
This is because Namecheap and probably some other hosting vendors don't want you to have user-based local configuration files. In this case you need to become root (through "Terminal" in WHM) or if you have visudo and create /etc/ssh/ssh_config.d/mysshconfig.conf so it looks like this:
# cat /etc/ssh/ssh_config.d/mysshconfig.conf
Host cvs.company.com
Port 21098
Host *
Port 22
#
So now you can even SSH to the host like "normal":
$ ssh -l myname cvs.company.com
It will ssh on port 21098.