OpenSSH connections fail in Git on Windows primarily due to path configuration errors, missing SSH keys, or permission issues within the user profile. By verifying that the SSH agent is running, ensuring correct key permissions, and checking the global Git settings for the remote URL, you can resolve most connectivity failures. This guide walks through the essential diagnostic steps to restore secure communication between your local repository and remote servers.
Verifying SSH Agent Configuration
The SSH agent must be active to manage your private keys during authentication. If the agent is not running, Git cannot pass your credentials to the OpenSSH client, resulting in immediate connection refusals or password prompts that never complete. You should start the agent manually if it is not configured to launch at startup.
Open PowerShell or Command Prompt and execute the following command to check the status:
Get-Service ssh-agent
If the state is stopped, start it using:
Start-Service ssh-agent
Next, verify that your keys are loaded by listing them with
ssh-add -l. If this returns an error stating the agent is not running, ensure the service was started successfully. For persistent access, configure the agent to start automatically via the Services management console or by adding a startup script in your user profile.
Checking Key Permissions and Existence
Windows enforces strict file permissions on the
.ssh directory. If the permissions are too open, OpenSSH will reject the key for security reasons, even if the key itself is valid. The private key file must have restrictive access rights that only allow your user account to read it.
Navigate to your user directory and locate the
.ssh folder. Ensure that the
id_rsa or
id_ed25519 file has no permissions for "Users" or "Everyone". You can adjust these permissions via the Properties dialog under the Security tab. Remove all groups except your specific user account and grant Read & Execute permissions.
Additionally, confirm that the corresponding public key (e.g.,
id_rsa.pub) is added to the remote server's authorized keys list. If you recently regenerated your keys, you must update the server configuration accordingly. Failure to sync the public key with the remote host is a common cause of authentication failures.
Validating Git Remote URLs
Incorrect remote URLs often mimic SSH failures. If the URL uses HTTPS but attempts to connect via SSH, or vice versa, the connection will fail. Verify that your remote origin is correctly configured for SSH usage.
Run the following command to view your current remote settings:
git remote -v
Look for the fetch and push URLs. They should begin with
[email protected]: or similar SSH syntax, not
https://. If they use HTTPS, change them to the SSH format using:
git remote set-url origin [email protected]:username/repository.git
This ensures Git routes the traffic through the SSH protocol rather than attempting an HTTP connection. Always double-check the username and repository name for typos, as these are frequent sources of silent failures.
Testing Connectivity Manually
Before relying on Git commands, test the underlying SSH connection directly. This isolates whether the issue lies with Git configuration or the network/SSH stack itself. Use the verbose flag to see detailed handshake information.
Execute a test connection to your remote host:
ssh -T [email protected]
Replace
github.com with your actual server hostname. A successful connection will display a welcome message and confirm authentication. If it fails, the output will specify whether the host is unreachable, the key is rejected, or the port is blocked.
For deeper diagnostics, add the
-v flag multiple times (up to
-vvv) to increase verbosity. This reveals exactly where the handshake breaks down, such as DNS resolution issues or firewall blocks on port 22.
Reinstalling Git and OpenSSH Components
In cases where configuration checks fail to resolve the issue, corrupted installations may be the culprit. Reinstalling Git ensures that the bundled OpenSSH client is properly registered with the system. This process also refreshes environment variables and PATH entries that might be misconfigured.
Download the latest installer from the official Git website and run it as an administrator. During installation, ensure that "Git Bash Here" and "Use Git and optional Unix tools" are selected. After installation, restart your terminal sessions to load the new environment variables.
If you suspect a conflict with system-wide OpenSSH features, consider resetting the Windows OpenSSH components. You can do this by uninstalling the feature from "Apps & Features" under Optional Features and reinstalling it. This provides a clean slate for the SSH daemon and client utilities.
For more details on managing OpenSSH services on Windows, see
Installing OpenSSH on Windows: A Tech Support Guide. Additionally, understanding the differences between shell environments can help troubleshoot path-related issues, as discussed in
What's the difference between git-bash.exe and bash.exe and how they interact with Windows Terminal?.
Frequently Asked Questions
Why does Git ask for a password instead of using my key?
Git asks for a password when the SSH agent is not running or the key is not loaded into it. Ensure the ssh-agent service is active and use ssh-add to load your private key into memory for automatic authentication.
Can I use PuTTY with Git on Windows?
Yes, you can configure Git to use PuTTY’s plink tool by setting the GIT_SSH environment variable to point to plink.exe. However, native OpenSSH is preferred for better compatibility and easier key management without extra configuration steps.
How do I fix "Permission denied (publickey)" errors?
This error means the server rejected your key, usually because the public key is not uploaded to the remote repository or the private key has incorrect file permissions. Verify the key exists in the remote server's settings and ensure the local private key is readable only by your user account.
What should I do if SSH connections time out?
Timeouts often indicate firewall blocking or incorrect port configurations. Check if your network allows outbound traffic on port 22, or try connecting via port 443 if your organization restricts standard SSH ports.
Does reinstallation delete my existing repositories?
No, reinstalling Git does not delete your local repositories or configurations stored in your home directory. It only updates the executable files and system registrations, leaving your code and .gitconfig files intact.