Skip to content

SSH hardening — port choice, ciphers, MACs, KEX

Beyond disabling passwords, modern SSH hardening means tightening the cryptographic primitives and (sometimes) moving off port 22. Here is what actually moves the needle and what is theatre.

Beyond disabling password auth, modern SSH hardening is mostly about three things: pruning weak cryptographic primitives (ciphers, MACs, key exchange algorithms), tightening session-level controls (login grace, max-auth-tries), and a handful of feature toggles (no agent forwarding, no X11). Changing the port from 22 is sometimes useful, sometimes theatre — we cover when it is which. This article is the reference config we apply on every production host.

How to verify

Inspect what the current sshd accepts.

sudo sshd -T | grep -iE '^(ciphers|macs|kexalgorithms|hostkeyalgorithms|pubkeyacceptedalgorithms|loginGraceTime|maxauthtries|maxsessions|maxstartups)'
ssh -Q cipher
ssh -Q mac
ssh -Q kex
nmap --script ssh2-enum-algos -p 22 server.example.net 2>/dev/null | head -40

ssh -Q <category> lists what the local OpenSSH was built with. sshd -T lists what the running daemon will accept. The remote nmap script reports what an external client sees.

What’s happening

Every SSH connection negotiates four cryptographic suites:

  • Key exchange (KEX). How the shared session secret is derived. Modern: curve25519-sha256, sntrup761x25519-sha512 (post-quantum hybrid).
  • Host key algorithm. How the server’s identity is signed for the client. Modern: ssh-ed25519, rsa-sha2-512.
  • Symmetric cipher. Encrypts the session traffic. Modern: chacha20-poly1305@openssh.com, aes256-gcm@openssh.com.
  • MAC. Message authentication (when not using AEAD ciphers). Modern: hmac-sha2-512-etm@openssh.com. With AEAD ciphers (Chacha20-Poly1305, AES-GCM) the MAC is implicit.

Default OpenSSH on Ubuntu 22.04+ / Debian 12 already excludes the worst algorithms (no SHA-1, no CBC-mode AES, no Diffie-Hellman group1). The remaining tightening is pruning the merely adequate algorithms in favour of the current best. The trade-off: tightening too aggressively means older clients (your CI runner on an LTS, a vendor’s appliance) cannot connect. The hardening below leaves modern OpenSSH and modern PuTTY working while removing the things scanners flag.

The port question. Moving SSH from 22 to (say) 2202 cuts the brute-force noise on the host’s logs but does not meaningfully improve security against a targeted attacker — anyone scanning your /24 finds the SSH port in seconds. Where the port change does help: it reduces journal volume, it reduces fail2ban-bantime churn, and it makes pattern-matching log-aggregation rules cleaner. We do it on hosts that take constant probing; we do not bother on hosts behind a bastion.

The procedure

  1. Pick the algorithm posture. The “strict modern” posture below works for any client built against OpenSSH 8.5+ (Ubuntu 22.04+, Debian 12+, macOS Ventura+, PuTTY 0.78+, modern WinSCP, modern Filezilla).

    # /etc/ssh/sshd_config.d/05-crypto.conf
    KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,sntrup761x25519-sha512@openssh.com
    HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256
    PubkeyAcceptedAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256
    Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
    MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com

    Validate and reload:

    sudo sshd -t
    sudo systemctl reload ssh
  2. Tighten session-level controls.

    # /etc/ssh/sshd_config.d/06-session.conf
    LoginGraceTime 30
    MaxAuthTries 3
    MaxSessions 3
    MaxStartups 10:30:60
    ClientAliveInterval 60
    ClientAliveCountMax 3
    PermitRootLogin no
    PermitEmptyPasswords no
    PasswordAuthentication no
    KbdInteractiveAuthentication no

    MaxStartups 10:30:60 means: up to 10 unauthenticated connections at once; from 10 to 60, drop 30% of new connection attempts; over 60, drop all. This is the natural rate-limit for SSH.

  3. Disable unused subsystems.

    # /etc/ssh/sshd_config.d/07-features.conf
    AllowAgentForwarding no
    AllowTcpForwarding no           # set to yes on bastions, see ssh-proxyjump-bastion
    X11Forwarding no
    PermitTunnel no
    PermitUserEnvironment no
    PrintMotd no

    On bastions, AllowTcpForwarding yes is required for ProxyJump — set it deliberately, don’t blanket-disable.

  4. Move the port if it helps (optional). Choose a port above 1024 to avoid root-bind contention; below 32768 (the ephemeral-port range start) is conventional.

    # /etc/ssh/sshd_config.d/08-port.conf
    Port 2202

    Update the firewall first:

    sudo ufw allow 2202/tcp comment 'SSH-alt'
    sudo sshd -t
    sudo systemctl reload ssh
    # test from a fresh session before removing the old rule
    sudo ufw delete allow OpenSSH

    On SELinux systems, you also need:

    sudo semanage port -a -t ssh_port_t -p tcp 2202
  5. Regenerate host keys to drop legacy ones. Old hosts may have DSA or ECDSA host keys that no longer make sense. Regenerate cleanly:

    sudo ssh-keygen -A             # generates missing host keys
    sudo rm /etc/ssh/ssh_host_dsa_key*        # remove DSA if present
    sudo rm /etc/ssh/ssh_host_ecdsa_key*      # remove ECDSA if you intend ed25519-only
    sudo systemctl restart ssh

    This changes the host key fingerprint as visible to clients. The client-side message (“WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!”) is what they will see; communicate the new fingerprints out-of-band.

  6. For a fleet, distribute via configuration management. Static text files are fine to drop in via Ansible’s copy module.

    - name: SSH hardening drop-in
      ansible.builtin.copy:
        src: sshd_config.d/05-crypto.conf
        dest: /etc/ssh/sshd_config.d/05-crypto.conf
        owner: root
        group: root
        mode: '0644'
      notify: Reload sshd
  7. Verify externally.

    ssh-audit server.example.net               # ssh-audit, a community tool, scores the config
    nmap --script ssh2-enum-algos -p 2202 server.example.net

    ssh-audit (https://github.com/jtesta/ssh-audit) is the cleanest external verifier; the “fail” / “warn” lines tell you exactly what to tighten.

  8. Test old clients before declaring done. A locked-down host that refuses your CI runner’s SSH client is worse than slightly-loose. Run ssh -vv <host> from the actual client to see which KEX/cipher fails.

Operational notes

  • Don’t put the Match block at the end of the monolithic sshd_config after your drop-ins — drop-ins under /etc/ssh/sshd_config.d/ are loaded before the rest of sshd_config, but config inside a Match block remains scoped. Test the resolved config with sshd -T -C 'user=ops,host=any,addr=any'.
  • Some appliances and managed services (older AWS Direct Connect endpoints, certain HSMs) still require SHA-1 KEX. If you have a fleet that must talk to those, the global hardening should add a per-host exception via Match host=<appliance>.
  • crypto-policies on RHEL/Fedora is a system-wide knob that affects sshd defaults. update-crypto-policies --set FUTURE may already be sufficient on those distros without per-file tuning.
  • Be careful about MaxStartups in a CI-heavy environment — if a deploy fans out 50 concurrent SSH connections, the default 10:30:60 will drop most of them. Bump it or use connection multiplexing.
  • The “moving SSH off 22” decision should be made per host class, not as a fleet diktat. Bastions stay on 22 (they are the front door); internal application hosts behind a bastion stay on 22 (no public exposure); a public-internet edge that must accept direct SSH benefits from the move.

In the engagements we run, the SSH configuration is identical across the fleet — same algorithms, same session controls, same disabled features — and any per-host exception is documented in the inventory. The result is that an ssh-audit scan returns the same green report for every production host, and the few deviations (a vendor appliance, a customer endpoint) are explicit, not accidental. That uniform hardening is part of the Managed Operations baseline.