Git Remote Add With Another SSH Port (Not 22)

In this brief article, you will learn to add a git remote with another SSH Port. By default, the SSH server listens on port 22. If the SSH server runs on any other port, the command to add remote will fail with error ssh: connect to host 192.158.xx.xx port 22: Connection refuse.

What is Git?

Git is a free & open-source version control system that almost all developers use today. No matter what project you’re working on, Git can help you manage your code efficiently.

It is faster than any other way of transmitting code to a remote location. Its lightning-fast performance can transmit the entire application within seconds to the production server, staging server, or a bitbucket repository.

To learn more about git, read our Learn Git series.

Git remote add

After initializing the local repository, one needs to add at least one remote repository to push code to that remote. By default, git requires a server username and IP address to add a remote repository.

git remote add remote_name ssh://username@ip_address/path-to-git-repo/repo.git

Example –

git remote add production ssh://root@192.168.28.xxx/var/repositories/lau.git

The above command will add a remote named production. If you changed the SSH port, the above remote will fail to push code and return the following error –

ssh: connect to host 192.168.28.xxx port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

It means git tried to connect to ssh on port 22. Since port 22 has been modified, we need to specify another port when adding a remote.

Git Remote Add With Another SSH Port

git remote add production ssh://username@ip_address:port/path-to-git-repo/repo.git

Example –

git remote add production ssh://root@192.168.28.xxx:3234/var/repository/lau.git
Git Remote Add With Another SSH Port

As you can see, the custom port needs to be specified while adding the remote. The above command will add a remote that will push code to the repository on SSH port 3234.