推送代码时SSH连接github.com遭拒绝,求可行解决方案
Let's work through this issue step by step—since both port 22 and 443 are getting connection refused, this is likely tied to network restrictions or local configuration glitches. Here are the fixes to try:
1. Check for Network Restrictions (Most Common Cause)
Many corporate, campus, or public networks block SSH traffic even on port 443 (they only allow standard HTTPS web traffic). If you're on such a network:
- Switch to using HTTPS instead of SSH for your repo URL. You can update it with this command:
git remote set-url origin https://github.com/your-username/your-repo.git - Alternatively, try connecting via a personal hotspot to rule out network-level blocks.
2. Verify Local Firewall/Security Software
Your local firewall or antivirus might be blocking outgoing SSH connections:
- On Windows: Go to Windows Defender Firewall > Advanced Settings > Outbound Rules, and ensure there's no rule blocking
ssh.exeor connections tossh.github.com. - On macOS: Open System Settings > Network > Firewall > Options, and make sure SSH is allowed.
- Disable any third-party security software temporarily to test if it's the culprit.
3. Test Network Connectivity
Run these commands to confirm if you can reach GitHub's servers on port 443:
# Using telnet telnet ssh.github.com 443 # Using netcat (if installed) nc -zv ssh.github.com 443
If these commands fail (no connection), your network is definitely blocking the traffic—stick with the HTTPS workaround above.
4. Fix SSH Configuration Issues
Even if you created the config file, there might be permission or setup problems:
- Ensure your
~/.ssh/configfile has strict permissions (SSH ignores files with loose permissions):chmod 600 ~/.ssh/config - Double-check that your SSH public key (
~/.ssh/id_rsa.pub) is added to your GitHub account: Go to GitHub Settings > SSH and GPG keys, and confirm the key exists. - Test the connection with verbose logging to see exactly where it fails:
Look for lines like "Connection refused" or "Permission denied" to pinpoint the issue.ssh -vT git@github.com
5. Try GitHub CLI as an Alternative
If SSH keeps failing, GitHub CLI (gh) uses HTTPS by default and handles authentication seamlessly. Install it, then run:
gh auth login
Follow the prompts to authenticate, and you'll be able to push/pull without dealing with SSH issues.
内容的提问来源于stack exchange,提问作者zhaokang66




