36

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 = git@github.com:asdf/asdf.git

5 Answers 5

42

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.

0
39

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://git@github.com: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).

2
  • Thanks Chris. I like this path best because it doesn't require mucking with my ssh config. Thanks!
    – James Ward
    Jan 5, 2011 at 15:17
  • 1
    fyi, if the server has a "bare" repo then the connection string would look more like ssh://git@github.com:22/asdf/asdf (without the .git)
    – Xeoncross
    Jan 23, 2011 at 19:06
5

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

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 git@my-git-server.lan

(again, notice that the username is always git)

  1. specify your remote address ssh://git@my-git-server.lan: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://git@my-git-server.lan: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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.