Resolving Git SSH Connection Timeout: Strategies for Switching from Port 22 to HTTPS Port

Nov 03, 2025 · Programming · 23 views · 7.8

Keywords: Git | SSH Connection Timeout | HTTPS Port

Abstract: This article provides an in-depth analysis of Git SSH connection timeout errors, focusing on solutions that utilize HTTPS port 443 as an alternative to SSH port 22 in firewall or proxy environments. Through configuration of ~/.ssh/config files and modification of remote repository URLs using git config commands, two effective resolution methods are presented with detailed verification steps and applicable scenarios. The article combines Q&A data and reference materials to offer comprehensive operational guidance and troubleshooting recommendations.

Problem Background and Error Analysis

SSH connection timeout is a common network issue when using Git for version control. When executing commands such as git push, git pull, or git clone, users may encounter the "ssh: connect to host github.com port 22: Connection timed out" error. This error typically occurs in restricted network environments, such as corporate firewalls, proxy servers, or ISP limitations.

Root Cause Investigation

The SSH protocol defaults to using port 22 for communication, but many network environments restrict or block access to this port. Firewall policies, proxy server configurations, or ISP port blocking can all lead to connection timeouts. Particularly in enterprise networks or public WiFi environments, administrators may disable non-standard ports for security reasons.

Solution 1: Modify SSH Configuration to Use HTTPS Port

GitHub supports SSH connections through HTTPS port 443, which is typically not blocked by firewalls. The specific configuration steps are as follows:

# Edit SSH configuration file
nano ~/.ssh/config

# Add the following configuration
Host github.com
    Hostname ssh.github.com
    Port 443

After configuration, test the connection using the following command:

ssh -T git@github.com

If configured successfully, authentication success information will be displayed, indicating that the SSH connection has been established through port 443.

Solution 2: Switch to HTTP Protocol

If SSH over HTTPS remains unavailable, consider switching completely to the HTTP protocol. This method does not require SSH key authentication but uses username and password or Personal Access Token instead.

First, edit the local Git repository configuration:

git config --local -e

Change the SSH format URL:

url = git@github.com:username/repo.git

To HTTPS format:

url = https://github.com/username/repo.git

Verification and Testing

After implementing either solution, verification testing is necessary:

  1. Use ssh -T git@github.com to test SSH connection
  2. Execute git pull or git push operations to verify functionality
  3. Check network connection status and proxy settings

Advanced Configuration Options

For more complex environments, consider the following advanced configurations:

# Global Git configuration alternative
git config --global "url.ssh://git@ssh.github.com:443/.insteadOf" git@github.com:

# Detailed SSH configuration
Host ssh.github.com
    HostName ssh.github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    Port 443

Troubleshooting Recommendations

If the above solutions are ineffective, consider:

Applicable Scenario Analysis

Solution 1 (SSH over HTTPS) is suitable for:

Solution 2 (Switching to HTTP) is suitable for:

Conclusion

Git SSH connection timeout issues typically stem from network environment restrictions. By properly configuring SSH to use HTTPS ports or switching to HTTP protocol, this problem can be effectively resolved. It is recommended to first try the SSH over HTTPS solution to maintain SSH security advantages while bypassing port restrictions. In practical applications, appropriate solutions should be selected based on specific network environments and security requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.