How to Add SSH in Git Remote

Abdul Jabbar Mar 11, 2025 Git Git Remote
  1. Understanding SSH and Git
  2. Adding SSH to Git Remote Through Default Port
  3. Adding SSH to Git Remote Through a Custom Port
  4. Verifying SSH Connection
  5. Conclusion
  6. FAQ
How to Add SSH in Git Remote

Adding SSH to your Git remote repository can significantly enhance your workflow, making it more secure and efficient. SSH, or Secure Shell, allows for encrypted communication between your local machine and the remote server, ensuring that your code and credentials remain safe.

In this tutorial, we’ll walk you through the process of adding SSH to your Git remote, even while working through different SSH ports. Whether you’re a seasoned developer or just starting with Git, this guide will provide you with the essential steps and commands to get your SSH setup running smoothly.

Understanding SSH and Git

Before diving into the implementation, it’s crucial to understand what SSH is and how it integrates with Git. SSH is a protocol that allows secure access to a computer over an unsecured network. When combined with Git, it allows you to interact with remote repositories securely. This means you can clone, push, and pull changes without exposing sensitive information.

When you add SSH to your Git remote, you typically do this by specifying the SSH URL of your repository. This can be particularly useful when working with different SSH ports, which may be necessary in certain network configurations. Let’s explore how to do this step by step.

Adding SSH to Git Remote Through Default Port

Most of the time, SSH operates over the default port 22. If your remote repository is set up to use this standard port, adding SSH to your Git remote is quite straightforward. Here’s how you can do it:

First, ensure that you have an SSH key generated on your local machine. If you haven’t created one yet, you can do so using the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Once your SSH key is created, you can add it to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Now, you can add your SSH key to your Git remote repository. Use the following command to set the remote URL:

git remote set-url origin git@github.com:username/repository.git

Output:

remote origin updated

This command sets your remote repository to use SSH instead of HTTPS. By doing this, you can push and pull changes securely without entering your username and password each time.

In this method, it’s essential to ensure that your SSH key is added to your GitHub or GitLab account, depending on where your repository is hosted. This step is crucial for authentication. If you run into issues, double-check your SSH configuration and ensure your public key is properly added to your account settings.

Adding SSH to Git Remote Through a Custom Port

In some cases, you may need to connect through a custom SSH port other than the default port 22. This situation often arises in corporate environments or specific server configurations. The process is similar, but you’ll need to specify the port number in your SSH URL. Here’s how to do it:

First, ensure that your SSH key is set up as described in the previous section. Then, you can add the remote repository using a custom port. Here’s the command:

git remote set-url origin ssh://git@github.com:PORT/username/repository.git

Replace PORT with your specific port number. For example, if your custom port is 2222, the command would look like this:

git remote set-url origin ssh://git@github.com:2222/username/repository.git

Output:

remote origin updated with custom port

This command tells Git to connect to the remote repository using the specified port. Make sure that the server you are connecting to is configured to accept SSH connections on this port. If you encounter any issues, verify your network settings and firewall rules, as they may block connections on non-standard ports.

Using a custom port can enhance security by obscuring your SSH service from automated attacks that typically target the default port. Always ensure that your SSH keys are managed securely to maintain a robust security posture.

Verifying SSH Connection

After adding SSH to your Git remote, it’s essential to verify that the connection is working correctly. You can do this by using the following command:

ssh -T git@github.com

Output:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

This command attempts to authenticate you with GitHub using your SSH key. If successful, it confirms that your SSH setup is working correctly. If you receive an error message, it may indicate an issue with your SSH key or configuration.

Verifying your SSH connection is a crucial step that ensures you can interact with your remote repository without issues. If you encounter problems, check your SSH key permissions and ensure that the public key is correctly added to your GitHub or GitLab account.

Conclusion

Adding SSH to your Git remote is a vital step toward securing your development workflow. Whether you are using the default SSH port or a custom one, understanding how to configure your Git repository can save you time and enhance your productivity. Following the steps outlined in this tutorial, you can easily set up SSH, verify your connection, and enjoy a more secure interaction with your remote repositories. Remember to keep your SSH keys secure and regularly check your configurations for any potential issues.

FAQ

  1. How do I generate an SSH key?
    You can generate an SSH key using the command ssh-keygen -t rsa -b 4096 -C "your_email@example.com" in your terminal.

  2. What should I do if I encounter an SSH permission denied error?
    Ensure that your public key is added to your GitHub or GitLab account and that your local SSH key has the correct permissions.

  3. Can I use SSH with other Git hosting services?
    Yes, most Git hosting services like GitLab, Bitbucket, and others support SSH for secure connections.

  4. How do I check if my SSH key is working?
    You can verify your SSH key by running ssh -T git@github.com and checking for a successful authentication message.

  5. Is it safe to use a custom SSH port?
    Yes, using a custom SSH port can enhance security by reducing the chances of automated attacks targeting the default port.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
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