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:
- Use
ssh -T git@github.comto test SSH connection - Execute
git pullorgit pushoperations to verify functionality - 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:
- Checking VPN connection status and testing after disabling VPN
- Changing network environment (e.g., switching WiFi networks)
- Verifying SSH key configuration and proxy settings
- Contacting network administrator to confirm firewall policies
Applicable Scenario Analysis
Solution 1 (SSH over HTTPS) is suitable for:
- Environments wishing to maintain SSH authentication workflow
- Scenarios requiring preservation of existing SSH key configurations
- Environments with high security requirements
Solution 2 (Switching to HTTP) is suitable for:
- Environments where SSH is completely blocked
- Temporary solutions or testing environments
- Scenarios with less strict authentication requirements
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.