SSH Hangs When Using sshpass and Private Keys
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
sshpass and SSH private keys solve two different authentication problems, and mixing them often creates confusing behavior. When an SSH command appears to hang, the usual cause is not a frozen network session but an interactive prompt that your automation is not equipped to answer.
Why sshpass and Private Keys Clash
sshpass is designed to feed a password to a program that expects password-based authentication. Private-key authentication works differently. SSH first tries the key, may ask for a key passphrase, may ask for host-key confirmation, and may fall back to password authentication depending on server policy and client options.
That means a command like this is structurally suspicious:
There are several possible outcomes:
- The key authenticates successfully, so
sshpasswas unnecessary - The private key is encrypted, and SSH prompts for the key passphrase instead of the login password
- The host key is unknown, and SSH waits for a trust confirmation
- The server rejects the key, and SSH falls through to another auth method
From the outside, all of those can look like "it hangs."
Use One Authentication Strategy at a Time
If you want key-based login, use SSH directly and make it fail fast in automation:
BatchMode=yes tells SSH not to prompt interactively. If the key cannot be used, the command exits with an error instead of waiting for input.
If the private key itself is passphrase-protected, the usual fix is not sshpass. The fix is ssh-agent:
You enter the key passphrase once when loading the key into the agent. Later SSH commands can reuse the unlocked key non-interactively.
If you truly need password authentication, disable key auth explicitly so SSH does not waste time attempting the wrong mechanism:
This makes your intent clear to the client.
Other Prompts That Look Like Hangs
Authentication is not the only issue. SSH can pause because it is waiting for input unrelated to your password.
A common example is the first connection to a host:
In a non-interactive environment, that prompt blocks progress. If you already manage host keys safely, pre-populate known_hosts or use an appropriate StrictHostKeyChecking policy for your environment.
Another cause is a remote command that expects a terminal. In that case the SSH session is established, but the remote side is waiting on interactive input. That is different from an authentication stall.
Debugging the Real Failure
Run SSH in verbose mode before changing too many flags. The debug logs usually show exactly where the process stops.
Look for lines that reveal the phase:
- Host-key verification
- Public key offered
- Public key rejected
- Password authentication attempted
- Keyboard-interactive challenge
If you must inspect sshpass behavior, keep the SSH debug flags:
That combination usually makes the source of the pause obvious.
Safer Automation Patterns
For unattended jobs, key-based authentication with an agent or dedicated deployment key is normally the right answer. It is more predictable and avoids embedding server passwords in scripts, process lists, or logs.
If you are inside CI, prefer one of these patterns:
- Inject a private key as a secret, write it with restrictive permissions, and use
ssh-agent - Use an ephemeral runner image with the host key already trusted
- Set
BatchMode=yesso jobs fail clearly instead of hanging forever
Also consider timeouts. A network issue can look similar to an auth prompt when the script provides no logging. Wrapping SSH with a timeout makes failures easier to diagnose.
Common Pitfalls
The most common mistake is expecting sshpass to unlock an encrypted private key. That is not what it is built for. It supplies login passwords, not key passphrases in a general, reliable way.
Another mistake is leaving multiple authentication methods enabled. SSH will try methods in order, and the fallback behavior can make the session appear inconsistent from one host to another.
Developers also forget about host-key prompts. The SSH command is not actually frozen; it is blocked waiting for trust confirmation that your non-interactive script cannot provide.
Finally, avoid using password automation when key-based access is available. Even when it works, it is usually less secure and harder to maintain.
Summary
- '
sshpassis for password authentication, not for general private-key handling.' - If you use a private key, prefer
ssh-agentplusBatchMode=yes. - Disable public-key auth explicitly when you truly want password-based login.
- Use
ssh -vvvto see whether the pause is caused by keys, passwords, or host-key verification. - For automation, choose one auth mechanism and make it fail fast instead of waiting for prompts.
Related reading
- SSH Key - Still asking for password and passphrase
- SSH Key - Still asking for password and passphrase
- ssh remote host identification has changed
- SSL Certificate added but shows Kubernetes Ingress controller fake certificate
- SSL CERTIFICATE_VERIFY_FAILED in aws cli
- SSL InsecurePlatform error when using Requests package
- ssl module in Python is not available when installing package with pip3
- StackOverflowError in Math.Random in a randomly recursive method
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.