How to Harden SSH Beyond the Basics (Ciphers, MACs & Algorithms)

Beyond changing the port and disabling root login (see SSH Hardening: Change the Port, Disable Root Login & Use SSH Keys), SSH's cryptographic configuration itself can be tightened to remove legacy, weaker algorithms.

Prerequisites

  • SSH already configured with key-based authentication
  • Root or sudo access
  • An active SSH session (keep it open throughout)

Checking Currently Supported Algorithms

ssh -Q cipher
ssh -Q mac
ssh -Q kex

Restricting to Strong Ciphers Only

sudo nano /etc/ssh/sshd_config
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr

Restricting to Strong MACs (Message Authentication Codes)

MACs [email protected],[email protected],[email protected]

Restricting to Strong Key Exchange Algorithms

KexAlgorithms curve25519-sha256,[email protected],diffie-hellman-group16-sha512

Disabling Weak Host Key Types

HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256

Regenerating Host Keys with Stronger Parameters (If Needed)

If your SSH host keys were generated long ago with weaker defaults:

sudo rm /etc/ssh/ssh_host_*
sudo ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
sudo ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""

Additional Hardening Directives

Protocol 2
X11Forwarding no
AllowTcpForwarding no
MaxAuthTries 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2

MaxAuthTries 3 limits authentication attempts per connection; ClientAliveInterval/ClientAliveCountMax disconnect idle sessions after a defined period.

Validating the Configuration

sudo sshd -t

Restarting SSH

sudo systemctl restart ssh

Testing in a New Session (Critical)

Without closing your current session, verify connectivity in a new terminal window before assuming success.

Verifying Your Configuration Externally

Use an external SSH configuration scanner (search "SSH audit tool") to check your server's configuration against current best practices from outside, confirming the changes took effect as intended.

Common Errors

Older SSH clients can't connect after restricting algorithms — some legacy clients don't support modern ciphers/KEX algorithms; balance security against your actual client compatibility needs, and update client software where possible rather than weakening the server.

Locked out after regenerating host keys — existing clients will show a host key mismatch warning; this is expected after intentionally regenerating keys, but verify the change was actually intentional before proceeding past the warning on the client side.

Best Practices

  • Always test configuration changes in a new session before closing the current one
  • Regenerate weak host keys if the server has been running for years without renewal
  • Re-audit periodically as cryptographic best practices evolve over time

Continue Reading

Browse more articles in Advanced Security & Compliance.

  • ssh ciphers, advanced ssh hardening, ssh algorithms, cryptography hardening
  • 0 Kasutajad peavad seda kasulikuks
Kas see vastus oli kasulik?

Seotud artiklid

How to Install and Configure auditd for System Auditing

auditd is the Linux kernel's auditing framework, recording detailed logs of security-relevant...

GDPR Compliance Basics for a Self-Hosted VPS

If you handle personal data of EU residents, GDPR applies regardless of where your server is...

How to Prepare Your VPS Infrastructure for a SOC 2 Audit

SOC 2 evaluates an organization's controls around security, availability, and confidentiality of...

How to Set Up AppArmor for Application Sandboxing

AppArmor confines individual applications to a defined set of permitted file, network, and...

How to Scan for Malware with ClamAV

ClamAV is a widely-used, open-source antivirus engine capable of detecting a broad range of...