Specify SSH Port for Git

Solution 1:

Just have a look at how to set up your ~/.ssh/config file correctly (man 5 ssh_config). You can specify different settings for different hosts easily. To solve your problem you would set

Host github.com
Port 22
Host *
Port 1234

Do have a look at the ssh_config manual page, it explains everything you need to know on the first few pages.

Solution 2:

Setting up a section in ~/.ssh/config is a fine solution, but it may be useful to know about another method.

The common scp-like syntax of user@host:path does not have a place for a port, but Git also supports an ssh: URL scheme that can be used to specify the port:

ssh://[email protected]:22/asdf/asdf.git

While an ssh: URL supports port specification, it does not support relative paths (e.g. there is no direct equivalent to the scp-like syntax of user@host:path where path does not start with a slash).

GitHub treats relative and absolute paths identically, so it works for them, but it may not work for all SSH-based Git repositories. For simple SSH-based hosting, you may need to insert /home/username/ or /Users/username/ when switching from relative to absolute paths. Some hosting systems may not handle absolute paths at all (though I would tend to call such lack of support a bug).


Solution 3:

(Love it when I find the answer right after asking it.)

I modified my ssh config to specify the port for each host instead of being a global setting:

Host asdf.com
    Port 1234

Host github.com
    User git
    Hostname github.com
    Port 22

Solution 4:

Found this link, and although it was helpful my blog entry might help clarify it:

https://prestongarrison.com/change-port-git-is-using-for-ssh/

Basically i think its much better to just edit your .git/config file and make the changes.

Tags:

Git

Ssh