How to Add SSH in Git Remote

Abdul Jabbar Feb 02, 2024
  1. Secure Shell (SSH) Key
  2. Add Another SSH Port Git Remote
How to Add SSH in Git Remote

In this era, the most widely used version control system is Git, operated by most developers within the team structure. This is mainly used for code efficiency, no matter how huge or critical the project is.

In this piece of block, we will learn how to add the SSH key to Git.

Secure Shell (SSH) Key

SSH is an initialism that stands for Secure Shell. It is a key produced to introduce a secure substitute to unsecured remote communication for network protocol.

The SSH key is used to give its users, especially system administrators, a platform to transmit remote files, can be used to manage the network, and can also be used for remote operating system access.

In this brief guide, we will know how to add a git remote with other SSH ports. The SSH server works on port 22 by default.

If we run it on any different port, we will face an error, i.e., error ssh: connect to host 192.158.xx.xx port 22: Connection refused. So, we can also change the port, which is available in the system.

git remote add

When we start the local repository, it is necessary to add a remote repository where we can push our codes efficiently without any hustle. This can be accomplished through the Git command git remote add.

The command git remote add is used to build an interrelation record to a new remote repository. This command, by default, needs a local server username and IP address to add a new remote repository as follows:

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

Example:

git remote add origin ssh://root@156.168.28.xxx/var/repositories/lau.git

A new remote has been added through the above command named origin. If we have changed the SSH port, our code won’t be pushed to the remote, and we will face the error as follows:

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

Make sure you have the rights
and the repository exists.

That means when adding git remote, tried to connect to default SSH port 22. As we are facing an error, port 22 is modified.

Now we will set down the different ports when adding a remote.

Add Another SSH Port Git Remote

Here we will specify the other SSH port for adding a new remote.

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

Example:

git remote add origin ssh://root@156.168.28.xxx:5254/var/repository/lau.git

Now we can see that the above command will push our code to the new remote on the different SSH port specified, that is, 5254.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Remote