Log inSign up

Tune OpenSSH beyond keys and passwords

Put each change in a drop-in under /etc/ssh/sshd_config.d/, check it with sudo sshd -t and the effective values with sudo sshd -T, and restart only while a timed undo is armed. From there, limit who may log in with AllowGroups, give special accounts their own Match rules, trust an SSH certificate authority instead of copying keys around, and reach inner servers through ProxyJump.

Topic: Security · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Where does this guide start?

It assumes you already log in with keys and have turned off password logins, as described in SSH keys and hardening, and that root is restricted as in disable root login. The settings below control who can connect, what each account may do, which algorithms the server accepts, and how trust is managed once you run more than a handful of servers. Versions differ: Ubuntu 24.04 ships OpenSSH 9.6, Ubuntu 22.04 has 8.9, Debian 12 has 9.2, Debian 13 has 10.0, and Rocky Linux 9 and AlmaLinux 9 have 8.7 with distribution patches. Check yours with ssh -V.

How do I change sshd settings without risk?

Use the same loop for every change in this guide. sshd keeps the first value it reads for most keywords, and drop-in files are read in name order near the top of the main file, so a low number such as 10- wins over packaged files like 50-cloud-init.conf or 50-redhat.conf:

sudo systemd-run --on-active=10min --unit=ssh-undo /bin/sh -c "mv /etc/ssh/sshd_config.d/10-custom.conf /root/; systemctl restart sshd || systemctl restart ssh"
sudo nano /etc/ssh/sshd_config.d/10-custom.conf
sudo sshd -t
sudo systemctl restart ssh    # Ubuntu and Debian
sudo systemctl restart sshd   # Rocky and AlmaLinux

Keep your session open, log in from a new terminal, and cancel with sudo systemctl stop ssh-undo.timer only after that login works. To preview what a given connection would get, ask sshd directly: sudo sshd -T -C user=deploy,host=office,addr=203.0.113.7 | grep -i allow.

How do I limit which accounts may use SSH?

A group based allow list is the easiest rule to maintain. Create the group, add your admin users, verify, and only then enforce it:

sudo groupadd -f sshusers
sudo usermod -aG sshusers deploy
id deploy
# /etc/ssh/sshd_config.d/10-custom.conf
AllowGroups sshusers
MaxAuthTries 3
LoginGraceTime 30
MaxStartups 10:30:60
ClientAliveInterval 300
ClientAliveCountMax 2

The id output must list sshusers before you restart, otherwise your own account is shut out. AllowUsers works the same way per user and accepts patterns like deploy@203.0.113.*. The other lines cut short slow or repeated attempts and drop dead connections after about ten minutes. OpenSSH 9.8 and newer, as on Debian 13, also apply PerSourcePenalties by default, which delays sources that keep failing.

What can Match blocks do?

A Match block overrides settings for connections that meet a condition: user, group, source address or local port. Place them at the very end of the main /etc/ssh/sshd_config, because every line after a Match keyword belongs to that block until the next one:

Match User backup
    AuthenticationMethods publickey
    AllowTcpForwarding no
    X11Forwarding no
    PermitTTY no

Match Group sftponly
    ChrootDirectory /srv/sftp/%u
    ForceCommand internal-sftp
    AllowTcpForwarding no

Match Address 10.8.0.0/24,203.0.113.7
    PasswordAuthentication no
    AllowAgentForwarding yes

The first block lets a backup account copy files but never open a shell; the second confines file transfer users, a setup the SFTP-only users guide explains in full. Test each block with sudo sshd -T -C user=backup,host=x,addr=198.51.100.9 and confirm the values you expect appear.

Should I restrict ciphers and key exchange?

Current defaults are already strong, so restrict only to drop older options a compliance check flags. List what your build supports, then see what is active:

ssh -Q kex
sudo sshd -T | grep -E "^(kexalgorithms|ciphers|macs)"

On Ubuntu and Debian a conservative modern set looks like this:

KexAlgorithms sntrup761x25519-sha512@openssh.com,curve25519-sha256,curve25519-sha256@libssh.org
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

Debian 13 also offers the post-quantum hybrid mlkem768x25519-sha256; if ssh -Q kex lists it, put it first. On Rocky Linux and AlmaLinux these lists come from the system crypto policy, so change the policy instead of sshd lines: sudo update-crypto-policies --show, then for example sudo update-crypto-policies --set DEFAULT:NO-SHA1 and restart sshd. Always test from your oldest client before closing the session.

How do SSH certificates replace authorized_keys?

With a certificate authority, servers trust one CA key, and you sign each user key with an expiry date. Adding a person or retiring a laptop no longer means editing files on every server. Create the CA on your own computer, not on a server:

ssh-keygen -t ed25519 -f ~/ssh-ca/user_ca -C "user CA"
ssh-keygen -s ~/ssh-ca/user_ca -I deploy-laptop-2026 -n deploy -V +26w ~/.ssh/id_ed25519.pub
ssh-keygen -L -f ~/.ssh/id_ed25519-cert.pub

The signed file sits next to your key, and the client sends it automatically. On each server, install the public CA key and trust it:

sudo install -m 644 user_ca.pub /etc/ssh/user_ca.pub
echo "TrustedUserCAKeys /etc/ssh/user_ca.pub" | sudo tee -a /etc/ssh/sshd_config.d/10-custom.conf

Host certificates work the other way round: sign /etc/ssh/ssh_host_ed25519_key.pub with a host CA using -h -n host.example.net, set HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub, and add @cert-authority *.example.net followed by the host CA public key to your known_hosts. Revoke a certificate before it expires with a key revocation list named in RevokedKeys. Keep a normal authorized key on each server until certificate logins are proven.

How do I reach servers through a jump host?

Use ProxyJump in your client config. One small server (the bastion) accepts SSH from anywhere, and the others accept SSH only from the bastion address in their firewall:

# ~/.ssh/config on your computer
Host bastion
    HostName 198.51.100.24
    User deploy
Host app1
    HostName 198.51.100.80
    User deploy
    ProxyJump bastion

Now ssh app1 connects through the bastion; the end to end session stays encrypted to app1, and your private key never leaves your computer, unlike agent forwarding. On the bastion, a dedicated jump account can be limited to forwarding only to known targets with PermitOpen 198.51.100.80:22, PermitTTY no and ForceCommand /usr/sbin/nologin inside a Match block. Firewall examples are in the UFW and firewalld guides.

Is moving SSH off port 22 worth it?

It reduces log noise from simple bots; it does not stop a targeted attacker, who finds the port with a scan in seconds. If you still want it, add the new port first and keep 22 until the new one is proven. In the drop-in write both lines:

Port 22
Port 2222

Then open 2222 in the firewall (sudo ufw allow 2222/tcp or sudo firewall-cmd --permanent --add-port=2222/tcp && sudo firewall-cmd --reload). On Rocky and AlmaLinux label the port for SELinux: sudo semanage port -a -t ssh_port_t -p tcp 2222. On Ubuntu 24.04, sshd is started by socket activation, so run sudo systemctl daemon-reload && sudo systemctl restart ssh.socket after editing ports. Test with ssh -p 2222 deploy@198.51.100.24, update fail2ban and any scripts, and remove Port 22 in a second round with the same undo timer.

Questions about Advanced OpenSSH settings

Why does my setting in sshd_config have no effect?

An earlier file set the same keyword first. Run sudo sshd -T to see the active value and sudo grep -ri keyword /etc/ssh/sshd_config.d/ to find which file set it.

Do I need to restart sshd for every change?

Yes, or reload it. Existing sessions keep running with the old settings, which is why they make a good safety line while testing.

Can certificates and authorized_keys work together?

Yes. sshd accepts either, so you can migrate users one at a time and remove old key files later.

Which key type should new users create?

Ed25519 is supported everywhere in this guide, short and fast. Hardware backed ed25519-sk keys add a physical token if your clients support FIDO2.

Does disabling agent forwarding matter?

Yes. A forwarded agent can be used by anyone with root on the remote server while you are connected. ProxyJump avoids that need, so AllowAgentForwarding no is a sensible default.