Fix SSH sign_and_send_pubkey: signing failed: agent refused operation

Beginner 20 min Apr 17, 2026 54 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Resolve SSH authentication errors when the SSH agent refuses to sign public keys. This tutorial covers checking agent status, fixing key permissions, restarting the agent, and troubleshooting SSH client configuration.

Prerequisites

  • Basic command line knowledge
  • SSH client installed
  • Existing SSH key pair

What this solves

The "sign_and_send_pubkey: signing failed: agent refused operation" error occurs when your SSH client cannot authenticate using public key authentication. This typically happens when the SSH agent is not running, cannot access your private keys, or when key permissions are incorrect. This tutorial shows you how to diagnose and fix these SSH authentication issues.

Understanding the SSH agent refused operation error

This error appears in several scenarios. Your SSH agent might not be running, your private keys might not be loaded into the agent, or file permissions on your SSH keys could be too permissive. The SSH agent is a background process that holds your private keys in memory and handles authentication requests from SSH clients.

When you see this error, SSH falls back to other authentication methods like password authentication. If that's also disabled, you'll get locked out of your server. The key is identifying whether the problem is with the agent, the keys, or the SSH client configuration.

Step-by-step troubleshooting

Check SSH agent status

First, verify that the SSH agent is running and accessible. The SSH_AUTH_SOCK environment variable should point to the agent's socket file.

echo $SSH_AUTH_SOCK
ps aux | grep ssh-agent

If SSH_AUTH_SOCK is empty or the agent process isn't running, you need to start the SSH agent.

Start SSH agent if not running

If no agent is running, start it and set the environment variables. This command starts the agent and outputs the variables you need to set.

eval $(ssh-agent -s)
echo "SSH agent started with PID: $SSH_AGENT_PID"

The eval command sets SSH_AUTH_SOCK and SSH_AGENT_PID in your current shell session.

Check loaded SSH keys

List the keys currently loaded in the SSH agent. If no keys are loaded, the agent cannot authenticate you.

ssh-add -l

If you see "The agent has no identities", your keys aren't loaded. If you see "Could not open a connection to your authentication agent", the agent isn't properly configured.

Load SSH keys into agent

Add your private keys to the SSH agent. By default, ssh-add looks for common key names in ~/.ssh/.

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_ed25519
ssh-add -l

Replace the key paths with your actual key files. The last command confirms your keys are now loaded.

Fix SSH key permissions

SSH is strict about key file permissions. Private keys must be readable only by you, and the ~/.ssh directory must have restricted access.

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/id_ed25519.pub
Never use chmod 777. SSH will refuse to use keys that are readable by other users. The correct permissions are 600 for private keys and 644 for public keys.

Check SSH key ownership

Ensure you own all SSH key files. If keys were created by another user or copied incorrectly, SSH will reject them.

ls -la ~/.ssh/
whoami

If the owner isn't your username, fix it:

sudo chown -R $USER:$USER ~/.ssh/

Restart SSH agent and reload keys

Sometimes the agent gets into a bad state. Kill existing agents and start fresh.

ssh-agent -k
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_ed25519

This ensures you have a clean agent with properly loaded keys.

Test SSH connection

Try connecting to your server with verbose output to see the authentication process.

ssh -v user@your-server.example.com

Look for lines mentioning "Offering public key" and "Server accepts key". If you still see the agent error, check the SSH client configuration.

Troubleshoot SSH client configuration

Check SSH config file

Your SSH client configuration might be interfering with agent communication. Check for conflicting settings.

cat ~/.ssh/config

Look for these potentially problematic settings:

# These can cause agent issues
IdentitiesOnly yes
PubkeyAuthentication no
ForwardAgent no

Fix SSH config conflicts

If you have a restrictive SSH config, ensure it allows agent forwarding and public key authentication for your server.

Host your-server.example.com
    HostName your-server.example.com
    User your-username
    PubkeyAuthentication yes
    IdentitiesOnly no
    ForwardAgent yes

Save the file and test the connection again.

Clear SSH known_hosts if needed

Sometimes SSH key conflicts in known_hosts can cause authentication issues. Remove the server's entry if you've reinstalled it.

ssh-keygen -R your-server.example.com

This removes any old host key entries that might conflict with the current server.

Configure automatic SSH agent startup

Add agent to shell profile

To avoid manually starting the agent each time, add it to your shell's startup file. This ensures the agent runs automatically when you log in.

# Start SSH agent if not already running
if [ -z "$SSH_AUTH_SOCK" ]; then
    eval $(ssh-agent -s)
    ssh-add ~/.ssh/id_rsa ~/.ssh/id_ed25519 2>/dev/null
fi

Source your shell profile to apply changes:

source ~/.bashrc

Use keychain for persistent agent

Install keychain to manage SSH agent across sessions more reliably.

sudo apt update && sudo apt install -y keychain
sudo dnf install -y keychain

Add keychain to your shell profile:

eval $(keychain --eval --agents ssh id_rsa id_ed25519)

Verify your setup

Test that your SSH agent setup works correctly:

# Check agent is running
echo $SSH_AUTH_SOCK
ssh-add -l

Test connection

ssh -v user@your-server.example.com

Check key permissions

ls -la ~/.ssh/

You should see your keys listed by ssh-add -l, and the SSH connection should succeed without the agent error.

Common issues

SymptomCauseFix
"Could not open a connection"SSH agent not runningeval $(ssh-agent -s)
"The agent has no identities"No keys loadedssh-add ~/.ssh/id_rsa
"Permissions 0644 too open"Private key too permissivechmod 600 ~/.ssh/id_rsa
Agent starts but keys won't loadWrong key ownershipchown $USER ~/.ssh/id_rsa
Works in terminal, fails in scriptsMissing environment variablesSource agent variables in script
Keys load but authentication failsWrong public key on serverCheck ~/.ssh/authorized_keys on server

Prevention and best practices

Key management tips: Use ssh-keygen -t ed25519 for new keys as they're more secure than RSA. Set passphrases on private keys for additional security. Use different keys for different purposes (work, personal, specific servers).

Always verify key permissions after copying keys between systems. Use ssh-copy-id to install public keys on remote servers instead of manually copying them. This command ensures correct permissions and placement.

Consider using SSH certificates for large environments instead of managing individual public keys. For more advanced SSH security, you might want to explore SSH tunneling and port forwarding or advanced SSH key authentication.

Monitor SSH authentication logs in /var/log/auth.log (Ubuntu/Debian) or /var/log/secure (RHEL-based systems) to catch authentication issues early.

Next steps

Running this in production?

Want this handled for you? This works for a single server. When you run multiple environments or need this available 24/7, keeping it healthy is a different job. See how we run infrastructure like this for European teams.

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle managed cloud infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.