Git Pull & Push Using A Custom Port

back to tech articles
CentOS 6, Git 1.7

This is a further addition to the article Secure SSH on CentOS 6 And RHEL.

By default, Git will try to connect to a remote host over port 22 (the default SSH port). As a security measure, and as a part of securing your server, we need to be able use Git on an alternate port, view the article for details.

There are a few options available to you, but the only one that works reliably for me is the following method. You should use whatever you are comfortable with. On the local machine, we can tell Git to connect to the remote host on a different port by specifying the port number in our git add declaration.

1
$ git remote add myserver ssh://8.8.8.8:443/path/to/repo.git

Alternatively, you can directly edit the Git config file.

1
$ vi .git/config

And then specify the port number and the ssh:// connection method.

1
2
3
4
5
6
7
# a default git remote entry:
[remote "myserver"]
    url = root@8.8.8.8:/path/to/repo.git

# using a custom port (other than 22):
[remote "myserver"]
    url = ssh://root@8.8.8.8:443/path/to/repo.git

And that’s it. You don’t need to reload anything, it should work straight away. Just use your regular git pull and git push commands.

As I said, there are other methods to get this done. For example, you could just add the following to a ~/.ssh/config file:

1
2
host myserver
port 443

I prefer NOT to use the ~/.ssh/config method because I found it to be unreliable in the long run.

Your choice tho 🙂