Keywords: SSH authentication | ssh-agent | key management | file permissions | troubleshooting
Abstract: This technical article provides an in-depth analysis of the common SSH error 'sign_and_send_pubkey: signing failed: agent refused operation', identifying the root cause as improperly loaded SSH keys in the agent. Through detailed examination of ssh-agent mechanisms, it offers comprehensive solutions including client-side key addition, permission verification, and server-side configuration checks, supported by practical case studies for effective troubleshooting.
Problem Phenomenon and Background Analysis
When configuring SSH key authentication, users frequently encounter the error message sign_and_send_pubkey: signing failed: agent refused operation. This typically occurs after successfully copying public keys using ssh-copy-id, but actual connections still require password authentication instead of using the keys. Technically, this indicates that while the SSH client detects available keys, the SSH agent (ssh-agent) refuses to perform the signing operation.
SSH Agent Mechanism Explained
The SSH agent is a critical component in modern SSH implementations, responsible for managing user private keys and performing cryptographic signing operations. When a user initiates an SSH connection, the client requests the agent to sign challenge data to prove ownership of the corresponding private key. If the agent hasn't loaded the correct keys, or if key permissions are misconfigured, signing failures occur.
The command ssh-add -l allows inspection of currently loaded keys in the agent. When the output shows The agent has no identities, it confirms that no usable keys are available in the agent, which is the direct cause of the signing failure.
Core Solution: Client-Side Key Management
According to best practices, the primary step to resolve this issue is to properly load SSH keys into the agent on the client side. Execute the following command sequence:
# Start SSH agent (if not already running)
eval "$(ssh-agent -s)"
# Add private keys from default locations to the agent
ssh-add
# Verify successful key loading
ssh-add -l
In the above code, the ssh-add command by default loads private key files from standard locations like ~/.ssh/id_rsa and ~/.ssh/id_dsa. If key files are in non-standard paths, use ssh-add /path/to/private/key to specify the exact file.
File Permission Configuration Essentials
Beyond key loading issues, improper filesystem permissions are another common cause. SSH imposes strict security requirements on key file permissions:
# Set correct directory permissions
chmod 700 ~/.ssh
# Set private key file permissions to user read-write only
chmod 600 ~/.ssh/id_rsa
# Set public key file permissions to user read-write, others read-only
chmod 644 ~/.ssh/id_rsa.pub
Permission checks can be performed using the stat --format '%a' <file> command to verify current permission settings. Overly permissive permissions cause SSH to reject using these keys for security reasons.
Server-Side Configuration Verification
While client configuration is the main focus, server-side configuration also requires verification:
# Check if authorized_keys file exists and contains correct public keys
cat ~/.ssh/authorized_keys
# Verify authorized_keys file permissions
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
The server-side authorized_keys file must contain the client's public key content with correct file permissions; otherwise, authentication will fail even with proper client configuration.
Comprehensive Troubleshooting Process
When encountering SSH key authentication issues, follow this systematic troubleshooting process:
- Check SSH agent status: Execute
ssh-add -lto confirm if keys are loaded - Verify file permissions: Use
statcommand to check~/.sshdirectory and key file permissions - Test key loading: Manually run
ssh-addand observe for any error messages - Server-side verification: Confirm
authorized_keysfile content and permissions - Detailed debugging: Use
ssh -v user@hostto obtain detailed connection logs
Practical Case Analysis and Resolution
In Digital Ocean cloud server configuration scenarios, users successfully run ssh-copy-id but still encounter authentication issues, resolved by executing ssh-add to load keys into the agent. Similarly, after system upgrades (e.g., from Ubuntu 17.10 to 18.04), file permissions might be reset, requiring reconfiguration of correct permissions.
Certain third-party tool integration scenarios, such as KeePassXC's SSH agent functionality, might also experience compatibility issues. In such cases, verifying that the tool-generated key formats and agent integration mechanisms are compatible with standard SSH implementations is crucial.
Preventive Measures and Best Practices
To prevent recurrence of such issues, consider:
- Configure SSH agent auto-start and key loading in
~/.bashrcor~/.zshrc - Regularly check key file permissions, especially after system upgrades or file migrations
- Use
ssh-keygen -t ed25519to generate more secure Ed25519 key pairs - Use different key pairs for different servers or services to enhance security
Through systematic problem analysis and standardized configuration management, the sign_and_send_pubkey: signing failed: agent refused operation error can be effectively avoided, ensuring reliable and secure SSH key authentication.