Troubleshooting SSH Connectivity Issues: Using SSH Over the HTTPS Port

Introduction

Secure Shell (SSH) is a widely used protocol for secure data communication over an insecure network. It is primarily used for remote login and command execution, as well as for managing repositories on platforms like GitHub. However, sometimes, you may encounter connectivity issues when using SSH to access your remote repository, especially when using a proxy server.

One common error message encountered is:

key_exchange_identification: Connection closed by remote host. 
Connection closed by IP port 22. 
fatal: Could not read from remote repository.

In this article, we’ll explore how to use SSH over the HTTPS port to resolve this issue, following the guidelines provided by GitHub (link).


Understanding the Issue

This error message usually indicates that there is a problem with the SSH connection, often due to firewall restrictions, a misconfigured proxy server, or network issues. When this happens, the connection to the remote repository is closed, making it impossible to perform Git operations.


Using SSH Over the HTTPS Port

To bypass these connectivity issues, you can use SSH over the HTTPS port (port 443) instead of the default SSH port (port 22). Port 443 is generally open in most network environments, as it is the standard port used for HTTPS traffic.

Here’s how to configure your Git client to use SSH over the HTTPS port:

  1. Verify SSH Connectivity
    Before making any changes, verify if you can successfully connect to your Git repository using SSH. Run the following command in your terminal:
    ssh -T git@github.com
    

    If you see a “Permission denied” or “Connection closed” error, proceed to the next step.

  2. Create an SSH Config File
    Create or modify the SSH configuration file in your home directory. The file is usually located at ~/.ssh/config (on Unix-based systems) or %USERPROFILE%\.ssh\config (on Windows). If the file does not exist, create one using a text editor.

  3. Add a Host Entry
    In the SSH config file, add the following lines:
    Host github.com
      Hostname ssh.github.com
      Port 443
      User git
    

    These lines instruct your Git client to use the alternative hostname (ssh.github.com) and port 443 for SSH connections to GitHub.

  4. Test the New Configuration
    To test your new configuration, run the SSH connectivity test again:
    ssh -T git@github.com
    

    If you see a successful authentication message, your Git client is now configured to use SSH over the HTTPS port, and you should no longer experience the “key_exchange_identification” error.


Conclusion

Using SSH over the HTTPS port is an effective workaround for resolving SSH connectivity issues when accessing remote Git repositories, particularly in environments with restrictive firewalls or proxy servers. By following the steps outlined in this article, you can ensure a stable and secure connection to your remote repositories and continue using Git without interruption.