The Windows SSH Agent Your Scripts Use Isn’t the One Holding Your Key

Date:

You added your private key to the SSH agent, watched it confirm the key was loaded, and your scripts still stop dead to ask for the passphrase, or worse, just fail in automation where no one is there to type it. You re-add the key, it says loaded again, and the script breaks again. The key really is in an agent. It’s just not in the agent your script is talking to.

The short answer: there is no single Windows SSH agent to point at, because Windows runs two separate SSH stacks and they each have their own agent. The built-in OpenSSH client (ssh.exe under System32) talks to the Windows OpenSSH Authentication Agent, a background service that holds your key across reboots. Git Bash (and other MSYS or Cygwin shells) ship their own ssh, which talks to its own agent that starts empty in every shell. When a script calls a bare ssh from Git Bash, the shell resolves it to the Git Bash client first, so it asks the empty Git Bash agent, never sees the key the Windows service is holding, and falls back to prompting for the passphrase. The fix is to pick one stack, load the key into that stack’s agent, and pin your tools to it so a bare ssh can’t silently reach for the other one.

The reason this is so confusing is that both halves of the story are true at once. Your key is loaded, ssh-add -l proves it. And your script can’t use it, the passphrase prompt proves that too. Both commands are honest; they’re just reporting on two different agents that happen to share a name.

Two agents wearing the same name

Install Git for Windows alongside the OS’s own OpenSSH, which ships in Windows by default now, and you have two complete SSH clients on the machine, each with its own agent:

  • The Windows OpenSSH stack. The client is C:\Windows\System32\OpenSSH\ssh.exe. Its agent is a real Windows service, OpenSSH Authentication Agent (ssh-agent), that runs in the background and keeps your keys in the OS credential store, so a key you add once survives logouts and reboots. This is the stack PowerShell and Command Prompt use.
  • The Git Bash (MSYS) stack. Git for Windows bundles its own ssh at /usr/bin/ssh, a separate build with a separate, old-style agent. That agent isn’t a service; it’s a process you start per shell with eval "$(ssh-agent -s)", and it holds nothing until you add a key to it, and it dies when the shell closes.

Two clients, two agents, both reached through a command spelled ssh and ssh-add. Nothing on screen tells you which one you’re talking to, which is the whole trap.

Why a bare ssh picks the wrong one

Inside Git Bash, the PATH puts the MSYS directories first, so a bare ssh resolves to /usr/bin/ssh, the Git Bash client, not the Windows one, even though both are installed. It’s the same Git Bash PATH ordering that quietly redirects other tools to a build you didn’t mean to run. Ask the shell directly and it shows you:

# Which ssh will a bare call actually run, and is the Windows one even on PATH?
which -a ssh
# /usr/bin/ssh                              <- the Git Bash client wins
# /c/Windows/System32/OpenSSH/ssh          <- the Windows client, only if PATH includes it
Code language: Bash (bash)

So a script that calls ssh deploy@192.0.2.10 from a Git Bash shell, or anything that shells out to a bare ssh, runs the MSYS client and asks the MSYS agent. If your key only ever went into the Windows service, that agent is empty, and the client does the only thing it can: it asks you for the passphrase. This is the same shape as a success that works on your machine but fails on the server, or a scheduled task that runs with a thinner environment than your shell: the environment you tested in and the environment the job runs in differ in a way nothing advertises.

The tell: “loaded” in one shell, empty in another

The diagnosis takes two commands, one in each kind of shell. List the agent’s keys from PowerShell (the Windows service) and from Git Bash (the MSYS agent), and watch them disagree:

# In PowerShell / CMD - talks to the Windows service:
ssh-add -l
#   256 SHA256:... your-key (ED25519)        <- loaded here

# In Git Bash - talks to the MSYS per-shell agent:
ssh-add -l
#   The agent has no identities.             <- empty here
Code language: Bash (bash)

That split is the bug, fully visible. A green ssh-add -l in one terminal is a reassuring signal that only speaks for the thing it checked: it reports the agent that ssh-add reached, not the agent your script will reach. Read it in the shell your automation actually runs in, and the comforting “loaded” turns into “no identities.”

Where a bare ssh call from Git Bash actually goesThe key is loaded, just in the other stack's agent – so the script never finds it
1. You run 'ssh' in Git Bash

PATH resolves the bare command to the Git Bash client, /usr/bin/ssh – not Windows' System32 one.

2. It asks the Git Bash agent

A per-shell agent that started empty. Your key went to the separate Windows Authentication Agent service.

3. No key – so it prompts

Asks for the passphrase. Fine if you can type it; in a script with no terminal, the call hangs or just fails.

The key is loaded, just in the other stack's agent. Standardize on one ssh client and load the key into that client's agent.

Why adding a passphrase broke everything at once

Often this lies dormant for months and then every script breaks on the same day: the day you put a passphrase on the key. An unencrypted private key doesn’t need an agent at all, either client just reads the key file straight from ~/.ssh and connects, so both stacks worked and no one noticed there were two. The moment you encrypt the key, the key file alone is no longer enough; the client needs an agent that holds the key already decrypted. The Windows service has it, so PowerShell and your interactive sessions keep working. The MSYS agent never had it, so every bare-ssh script that runs through Git Bash now stops at a passphrase prompt. And in automation, with no terminal attached, there’s nothing to answer the prompt, so the job doesn’t error cleanly; it hangs, or fails with a bare “Permission denied,” which sends you debugging the server instead of the shell.

Fix: pick one stack and pin everything to it

The cure is consistency, not cleverness. Decide which client your automation uses, put the key in that client’s agent, and remove the ambiguity so a bare ssh can’t wander off to the other stack.

Standardize on the Windows service (the durable choice). It persists across reboots, so you load the key once. Make sure the service runs, add the key to it, then point your tools at the Windows client explicitly:

# 1) In an elevated PowerShell, make the agent service run automatically and start it:
#    Set-Service ssh-agent -StartupType Automatic ; Start-Service ssh-agent
# 2) Add the key to the service (uses the Windows ssh-add):
#    ssh-add C:\Users\you\.ssh\id_ed25519
# 3) Pin Git itself to the Windows client, so git-over-SSH never uses the MSYS one:
git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"
# 4) In shell scripts, call the Windows client by its full path instead of a bare ssh:
/c/Windows/System32/OpenSSH/ssh.exe deploy@192.0.2.10
Code language: Bash (bash)

Microsoft’s OpenSSH key-management guide documents the agent service and ssh-add, and Git’s core.sshCommand setting is the supported way to tell Git which SSH client to run.

Or standardize on the Git Bash agent, if your whole workflow lives there. Then you must start its agent and add the key in each session (or from your shell profile), because it doesn’t persist:

# Start the MSYS agent for this shell and add the key to it:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Code language: Bash (bash)

Either choice works; mixing them is what doesn’t. The failure only ever comes from loading the key into one stack’s agent and running ssh from the other.

The short version

Windows has two SSH clients, the OS OpenSSH one (with a persistent agent service) and the one Git Bash ships (with its own per-shell agent), both reached by typing ssh. A bare ssh from Git Bash runs the Git Bash client and asks its agent, which is empty if your key went into the Windows service, so it prompts for the passphrase or fails silently in automation. ssh-add -l says “loaded” in one shell and “no identities” in the other, and that split is the whole bug. Pick one stack, load the key into its agent, and pin your tools to that client, with core.sshCommand for Git and the full System32 path in scripts, so nothing reaches for the wrong agent.

Frequently Asked Questions

Why does ssh-add -l say my key is loaded in one terminal but not another?

Because the two terminals talk to two different agents. PowerShell and Command Prompt use the Windows OpenSSH Authentication Agent service; Git Bash uses its own MSYS agent. ssh-add -l reports whichever one the ssh-add you just ran is connected to, so a key added to the Windows service shows as loaded in PowerShell and absent in Git Bash. Nothing is broken about the key; you’re reading two separate agents that share a command name. Run ssh-add -l in the exact shell your script runs in to see the truth that matters.

Which Windows SSH agent should I standardize on?

For most people, the Windows OpenSSH Authentication Agent service, because it persists across reboots: you add the key once and it’s there for every future session, which is what automation needs. Make the service start automatically, add your key with the Windows ssh-add, set Git’s core.sshCommand to the System32 client, and call that full path in scripts. The Git Bash agent is a fine choice only if your entire workflow lives in Git Bash and you’re willing to re-add the key each session or from your profile, since it doesn’t survive the shell closing.

Does this affect git push over SSH too?

Yes, for the same reason. When Git talks to a remote over SSH it runs an ssh client, and by default that’s whichever one it finds, often the Git Bash one inside Git for Windows. If your key is in the Windows service, git push prompts for the passphrase or fails just like a shell script does. Setting git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe" pins Git to the Windows client so it uses the same agent your interactive sessions do, and the prompts stop.

Share post:

Subscribe

Popular

More like this
Related

Can AI CLI Conversation History Leak Answers Into a Blind Test?

You use an AI command-line tool to grade or answer hundreds of questions, one fresh run each. Then you find out it keeps every conversation on disk, answer keys included. Is run 200 quietly reading run 199?

The Code Snippets 403 That Comes From Your Host’s Firewall, Not WordPress

You change a few lines in a snippet, press Save, and get "Could not update snippet. Request failed with status code 403". Press Save on the unchanged snippet and it works. Your login is fine. Something between you and WordPress is reading the code itself.

Why a Flaky Test Passes Alone and Fails When the Machine Is Busy

Run the test by itself and it passes, every time. Run the whole suite in parallel and it fails, but only some days, and its failure looks exactly like a real bug. In our case the code was fine. The test was waiting on a clock.

Why Multisite Subsite Registration Sends Visitors to the Main Site

You put a "create an account" link on one site of a WordPress multisite. Visitors who click it land on the main site, which either says "Registration has been disabled" or signs them up there instead.