SSH Key Only Auth Setup

Khimananda Oli 9 min read Security
SSH Key Only Auth Setup

By Khimananda Oli | Last reviewed: August 2026

Password-based remote access is the single most common entry point for server compromises in 2026. A proper SSH Key Only Auth Setup eliminates brute-force risk entirely by replacing guessable secrets with cryptographic proof of identity. This guide walks you through generating modern Ed25519 keys, configuring sshd_config safely, and validating that password login is permanently disabled before you lock yourself out.

Local Workstationssh-keygen -t ed25519~/.ssh/id_ed25519~/.ssh/id_ed25519.pubSecure Transferssh-copy-id OR manualAppend to authorized_keyschmod 600 enforcedProduction ServerPubkeyAuthentication yesPasswordAuthentication nosystemctl restart sshd
SSH Key Only Auth Setup end-to-end flow from local key generation through secure deployment to server-side enforcement

How do you generate secure SSH keys for SSH Key Only Auth Setup?

The foundation of any reliable Ubuntu security hardening strategy is strong cryptography at the transport layer. In 2026, Ed25519 is the default recommendation for new deployments. It offers fixed-size 256-bit keys, constant-time signing to prevent timing attacks, and broad compatibility across OpenSSH 6.5+, AWS EC2, Azure VMs, and GCP Compute Engine. RSA-4096 remains acceptable for legacy systems that lack Ed25519 support, but avoid DSA (deprecated) and ECDSA (implementation-dependent curve risks).

Generate an Ed25519 key pair with a strong passphrase

ssh-keygen -t ed25519 -C "khimananda@prod-bastion-2026" -f ~/.ssh/id_ed25519_prod
  • -t ed25519: Selects the Edwards-curve algorithm. Keys are always 256 bits regardless of output encoding.
  • -C: Adds a comment embedded in the public key. Use a descriptive label including hostname, purpose, and year so you can identify keys during audits or rotation.
  • -f: Specifies a non-default filename. Never reuse the same private key across environments. Separate keys per environment (dev, staging, prod) limits blast radius if one is compromised.

When prompted for a passphrase, use a minimum of 20 characters or a six-word diceware phrase. The passphrase encrypts the private key at rest using AES-256-CTR. If your laptop is stolen or your home directory is backed up insecurely, the passphrase is your last line of defense. Store passphrases in a dedicated secrets manager like Bitwarden or 1Password — never in plaintext files, shell history, or sticky notes.

Verify key fingerprint before distribution

ssh-keygen -lf ~/.ssh/id_ed25519_prod.pub
# Output example: 256 SHA256:AbCdEf...GhIjKl khimananda@prod-bastion-2026 (ED25519)

Record this fingerprint in your team’s access registry or infrastructure documentation. When onboarding new engineers or rotating keys, compare fingerprints to confirm the correct public key was deployed. This prevents man-in-the-middle substitution attacks where an attacker swaps their own key during transfer.

How do you configure sshd_config for SSH Key Only Auth Setup?

The server-side configuration is where most failures occur. A single typo in /etc/ssh/sshd_config can lock you out permanently or leave password auth silently enabled. Always edit this file with a backup session open, or use a console/IMPI/IPMI fallback if working on bare metal.

Essential directives for key-only enforcement

# /etc/ssh/sshd_config — Production hardened for 2026
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM yes
PermitRootLogin prohibit-password
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers deployer admin sre-team
LogLevel VERBOSE

Each directive serves a specific security function:

  • PubkeyAuthentication yes: Explicitly enables public key verification. While often the default, declaring it prevents accidental override by included config fragments.
  • PasswordAuthentication no: Disables interactive password prompts. This is the core of SSH Key Only Auth Setup. Without this line, attackers can still attempt brute force even when keys are configured.
  • ChallengeResponseAuthentication no: Prevents keyboard-interactive methods that may fall back to PAM password modules. Some distributions enable this by default for 2FA; disable it unless you have a verified TOTP/PAM setup.
  • PermitRootLogin prohibit-password: Allows root key login only when absolutely necessary (e.g., emergency recovery). Prefer disabling root entirely and using sudo via named accounts. Never set this to yes.
  • AllowUsers: Whitelists permitted usernames. Rejects connections before authentication begins. Update this list whenever personnel change. For larger teams, use AllowGroups ssh-users instead.
  • LogLevel VERBOSE: Logs key fingerprints on successful auth. Critical for forensic analysis and compliance audits (SOC 2, ISO 27001). Avoid DEBUG levels in production as they expose sensitive metadata.

Validate configuration syntax before restart

sudo sshd -t -f /etc/ssh/sshd_config
# Returns nothing on success; prints error location on failure

Never skip this step. A missing newline, incorrect indentation in a Match block, or unsupported option will cause sshd to fail on restart. The -t flag performs a full parse without starting the daemon. Fix all errors before proceeding.

Edit sshd_confignano/vim + backupsshd -t ValidateSyntax check onlyTest New SessionKeep old session opensystemctl restart sshdOnly after test succeedsValidation Failed?Fix config → re-run -tDO NOT restart sshdTest Failed?Revert config via old sessionDiagnose logs/auth.log
Safe sshd_config modification sequence with validation gates to prevent lockout during SSH Key Only Auth Setup

How do you safely deploy public keys without locking yourself out?

The transition period between enabling keys and disabling passwords is the highest-risk moment. Follow this exact sequence to maintain access throughout.

  1. Open two separate terminal sessions to the target server. Keep Session A active and untouched. Use Session B for all configuration changes. If something breaks, Session A remains authenticated under the old rules.
  2. Deploy the public key while password auth is still enabled. Use ssh-copy-id -i ~/.ssh/id_ed25519_prod.pub user@server or manually append to ~/.ssh/authorized_keys. Verify permissions: chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys. Incorrect permissions cause silent key rejection.
  3. Test key authentication in a third session. Run ssh -i ~/.ssh/id_ed25519_prod -o BatchMode=yes user@server 'echo KEY_AUTH_OK'. The BatchMode=yes flag ensures the command fails immediately if key auth doesn’t work, rather than prompting for a password. Confirm output shows KEY_AUTH_OK.
  4. Apply sshd_config changes and validate syntax as shown above. Only proceed if sshd -t returns clean.
  5. Restart sshd and test again. Run sudo systemctl restart sshd, then immediately test key auth in a new session. Do not close Session A until this succeeds.
  6. Disable password auth only after confirmed key access. Edit sshd_config to set PasswordAuthentication no, validate, restart, and test once more. Now attempt a password login from a different machine — it must be rejected instantly.

This disciplined approach mirrors the change management practices required for Ubuntu server security best practices and aligns with SOC 2 CC6.1 controls for logical access provisioning. Skipping the parallel-session safeguard is the #1 cause of self-inflicted outages during SSH hardening.

How does SSH Key Only Auth Setup compare to password and certificate auth?

Understanding the trade-offs helps you justify this control to stakeholders and choose the right model for your scale.

CriteriaPassword AuthSSH Key Only Auth SetupSSH Certificates (CA-signed)
Brute-force resistanceVulnerable (rate-limited only)Immune (no secret to guess)Immune + short-lived credentials
Key rotation effortN/A (password reset)Manual per-host authorized_keys updateCentralized CA re-signing; auto-expiry
Audit trail granularityUsername onlyKey fingerprint per loginFingerprint + principal + validity window
Scalability (100+ hosts)Poor (shared secrets leak)Moderate (Ansible/Terraform managed)Excellent (single trust anchor)
Setup complexityTrivialLow (this guide)High (requires CA infrastructure)
Compliance acceptanceFails PCI-DSS, SOC 2, ISO 27001Meets baseline requirementsExceeds requirements; preferred for high-assurance

For most teams managing fewer than 50 servers, SSH Key Only Auth Setup delivers the optimal balance of security and operational simplicity. Certificate-based auth becomes worthwhile when you manage hundreds of ephemeral instances or require automatic credential expiration. Password authentication should never appear in production environments after 2024.

Password Auth✗ Brute-force vulnerable✗ Shared credential risk✗ No per-key audit trail✗ Fails compliance auditsREJECT IN PRODSSH Key Only Auth Setup✓ Immune to brute force✓ Per-key fingerprint logging✓ Meets SOC2 / ISO27001△ Manual rotation at scaleRECOMMENDED DEFAULTSSH Certificates✓ Auto-expiring credentials✓ Centralized revocation✓ Scales to 1000+ hosts△ Requires CA infrastructureENTERPRISE SCALE
Security posture and operational trade-offs across password, SSH Key Only Auth Setup, and certificate-based authentication models

What ongoing maintenance does SSH Key Only Auth Setup require?

Deploying keys is not a set-and-forget task. Sustainable security requires operational discipline around rotation, revocation, and monitoring.

Key rotation and revocation procedures

Rotate individual keys annually or immediately upon personnel departure. Remove departed users’ keys from every host within 24 hours. Automate this with Ansible, Terraform, or a GitOps tool like ArgoCD. For guidance on integrating this into broader infrastructure automation, see automating server setup with Ansible playbooks. Maintain a centralized inventory mapping keys to owners and purposes — this is mandatory evidence for ISO 27001 A.9.2.6 reviews.

Monitoring and alerting on authentication events

Configure log aggregation to capture sshd events at VERBOSE level. Alert on:

  • Failed key authentications exceeding 5 attempts per minute (indicates misconfiguration or probing)
  • Successful logins from unexpected key fingerprints (potential key compromise)
  • Any password authentication attempt after disablement (config drift or rollback)

Integrate these signals into your existing observability stack. If you’re building out monitoring, the Prometheus and Grafana full monitoring stack provides native node_exporter metrics for SSH session counts and auth failure rates.

Defense in depth beyond sshd_config

SSH Key Only Auth Setup is necessary but insufficient alone. Layer additional controls:

  • Fail2Ban or CrowdSec: Block IPs after repeated failed key attempts. Even though keys can’t be brute-forced, excessive failures indicate reconnaissance or misconfigured clients.
  • Network segmentation: Restrict SSH port 22 to bastion hosts or VPN CIDRs via firewall rules. Never expose SSH directly to 0.0.0.0/0 on public clouds.
  • SSH agent forwarding caution: Disable ForwardAgent yes unless connecting through a trusted bastion. Agent forwarding exposes your private key to compromised intermediate hosts.
  • Regular config audits: Schedule quarterly reviews of sshd_config against CIS Benchmarks. Drift happens during incident response or hurried deployments.

Implement SSH Key Only Auth Setup Today

Every day password authentication remains enabled is another day your servers are exposed to automated credential stuffing and targeted brute-force campaigns. The steps outlined here take under 30 minutes per host and deliver immediate, measurable security improvement. Start with your most critical production systems, validate thoroughly using the parallel-session method, and expand outward. If your team needs hands-on assistance implementing this across a multi-cloud fleet or preparing for a compliance audit, reach out to discuss your infrastructure security posture.

Frequently Asked Questions

Edit /etc/ssh/sshd_config and set PasswordAuthentication to no. Restart the sshd service using systemctl restart sshd to apply changes immediately. Always verify key-based access works in a separate terminal session before closing your current connection to prevent accidental lockouts during this critical security hardening step.

The .ssh directory must be 700 and authorized_keys must be 600. Private keys require 600 permissions while public keys can be 644. Incorrect permissions cause silent authentication failures because OpenSSH refuses to trust files writable by group or others as a security precaution against unauthorized key injection attacks.

Yes, disabling root login is recommended best practice. Configure PermitRootLogin to no in sshd_config after adding your personal public key to a standard user account. Use sudo for privilege escalation instead. This limits attack surface while maintaining full administrative capability through proper identity separation and audit trails.

Ed25519 is preferred for its speed, small key size, and strong security. RSA 4096 remains acceptable for legacy compatibility but generates slower handshakes. Avoid DSA entirely as it is deprecated. Generate keys using ssh-keygen -t ed25519 -C "comment" for optimal balance of performance and cryptographic strength in modern deployments.

Keep an active session open while testing new configurations in a separate terminal. Use ssh -v to debug connection issues verbosely. Validate sshd_config syntax with sshd -t before restarting the daemon. Never close your original shell until confirmed working, ensuring recovery access remains available if misconfiguration occurs.

Yes, AWS EC2, GCP Compute Engine, and Azure VMs all support key-only authentication natively. Cloud-init or user-data scripts can inject public keys during provisioning. Disable password auth via cloud provider security policies or startup scripts to enforce key-only access consistently across ephemeral infrastructure without manual post-deployment configuration steps.

You lose access permanently unless backup keys or console access exists. Always maintain at least two authorized keys per server and store encrypted backups securely. Cloud providers offer VNC or serial console access for emergency recovery. Without these fallback mechanisms, lost keys require complete server rebuild or data migration procedures.

Add the new public key to authorized_keys first and verify connectivity. Only remove the old key after confirming the new one works reliably. Never replace keys atomically without testing. Maintain overlapping validity periods during rotation windows to prevent accidental lockouts when managing multiple servers or automated deployment pipelines.

Fail2ban still provides value by blocking IPs attempting invalid usernames or malformed packets even when passwords are disabled. Configure jails for sshd to catch enumeration attempts. While key-only auth eliminates password guessing, attackers still probe for misconfigurations, making rate limiting and IP blocking useful defense-in-depth layers.

Use Ansible, Puppet, or Chef to distribute keys declaratively rather than manual copying. Store public keys in version control but never private keys. Tools like ssh-copy-id help initially but lack idempotency. Centralized configuration management ensures consistency, enables auditing, and simplifies revocation when team members leave or keys are compromised.

Agent forwarding carries risk because compromised servers can use your forwarded credentials. Prefer ProxyJump (-J) for hopping between hosts instead. If forwarding is necessary, restrict it per-host in ssh_config using ForwardAgent yes only for trusted intermediaries. Always assume remote servers could be malicious when designing access architectures.

No, existing keys remain valid across OpenSSH upgrades. However, review supported algorithms periodically as older types get deprecated. Update sshd_config to disable weak ciphers and MACs. Regenerate keys only if using obsolete algorithms like DSA or RSA under 2048 bits. Version upgrades themselves do not invalidate cryptographic material.

Pipeline runners require dedicated service account keys with minimal permissions. Never share developer keys with automation systems. Store private keys in encrypted secrets managers like Vault or GitHub Secrets. Restrict authorized_keys entries with command= options to limit what automated processes can execute, preventing lateral movement if credentials leak.

Wrong file permissions, missing newline at end of authorized_keys, incorrect key format, or forgetting to restart sshd after config changes. SELinux contexts on RHEL systems also block access if misconfigured. Always validate with sshd -t and test in parallel sessions. Check /var/log/auth.log for specific rejection reasons when debugging failures.

Yes, key-only authentication satisfies SOC2 access control requirements better than passwords. It provides stronger identity assurance and eliminates credential stuffing risks. Document key management procedures, rotation schedules, and revocation processes for auditors. Combine with MFA via PAM modules for enhanced compliance where regulations demand multi-factor authentication beyond possession factors alone.