47

I use a non-standard ssh port (1234) for most of the servers I connect to. So in my ssh config file I have:

Port 1234

But github.com uses port 22. When I try to connect to github it obviously tries to use port 1234. Right now I have to edit my ssh config to get things to work.

Here is a snippet from my git config:

[remote "origin"]
        url = [email protected]:asdf/asdf.git

5 Answers 5

50

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.

1
  • 1
    This solution would not work if user needs to reach more than 1 port on same host. Commented Feb 20, 2023 at 1:19
54

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

3
  • Thanks Chris. I like this path best because it doesn't require mucking with my ssh config. Thanks!
    – James Ward
    Commented Jan 5, 2011 at 15:17
  • 1
    fyi, if the server has a "bare" repo then the connection string would look more like ssh://[email protected]:22/asdf/asdf (without the .git)
    – Xeoncross
    Commented Jan 23, 2011 at 19:06
  • This doesn't work for all repo software types, though - some require a path component after the ":" without preceding them with "/" - in which case the ssh-config solution still works, but not this one.
    – foo
    Commented Dec 9, 2022 at 19:25
6

(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
6

Had a similar issue trying to connect to my git server

( have a gitea server in a docker container with ssh-port configured to 2022, instead of standard 22, here as an example my-git-server.lan ).

  1. create ssh key-pair (quiet, without password)
$ ssh-keygen -q -N '' -b 4096 -f ~/.ssh/mykeyfile

(this will create two files: private-key mykeyfile and public-key mykeyfile.pub)

  1. display contents of the public-key and copy/paste it to your profile's SSH keys in your git-server (similar to how you would do it on Github )
$ cat ~/.ssh/mykeyfile.pub
  1. add following lines to ssh-config to specify git-server's hostname, port and key-file
$ nano ~/.ssh/config
Host my-git-server.lan
  HostName my-git-server.lan
  User git
  Port 2022
  IdentityFile ~/.ssh/mykeyfile

(notice that the username is always git, regardless of your actual username on your git-server)

  1. test ssh connection to your git-server using public-key, .. and receive a success message
$ ssh -T [email protected]

(again, notice that the username is always git)

  1. specify your remote address ssh://[email protected]:2022/alex/myproject.git for your local git repository (again, notice the user git and the port 2022), .. check remote configuration
$ cd your/local/git/repository/folder
$ git remote add my-git-server ssh://[email protected]:2022/alex/myproject.git
$ git remote -v

( here you also see that on my git-server my actual user is alex and repository is myproject )

Done! You can now work with your git-server .. fetch/commit/push etc.

1
  • 1
    After a lot of research I came across this answer, and this works for me, also even if I used only the IP address of the server where Gitea docker runs, since I haven't defined any other textual Hostname for it. I just put the IP address also as the HostName in the .ssh/config file (as I had configured in Gitea on the installation first page). This way I can clone also from the same machine where Gitea docker is running.
    – marcor92
    Commented Sep 10, 2023 at 2:05
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .