Secure SSH Access in Linux: 10-Step Hardening Checklist

Secure SSH in Linux with a 10-step checklist: Ed25519 and FIDO2 keys, sshd_config drop-ins, AllowGroups, Fail2ban, SSH certificates and log monitoring.

Aug 2, 2025 - 18:59
Nov 9, 2026 - 01:06
 0  81
TOPICDevOps BasicsPractical guide
Secure SSH Access in Linux: 10-Step Hardening Checklist

Secure SSH access in Linux comes down to a short list: use Ed25519 or hardware-backed keys, disable password and root logins, restrict who can log in, keep OpenSSH patched, limit forwarding, and monitor authentication logs. Put settings in a drop-in file, test with sshd -t, and keep a second session open before you reload.

This guide is a hardening checklist for administrators who manage several Linux servers, whether on-prem or in the cloud. Each step explains the risk it reduces, the exact configuration, and how to confirm it worked. For RHEL-specific key setup and crypto policies, see configuring SSH keys and security in RHEL 10. For how key pairs authenticate under the hood, read how SSH key pairs authenticate logins. The wider Linux context sits in our RHEL 10 setup guide for beginners.

Key takeaways

  • Key-based authentication with Ed25519 (or FIDO2 -sk keys) is the baseline; turn off password login once keys work.
  • Set PermitRootLogin no and use named accounts with sudo for traceability.
  • Restrict access with AllowGroups or AllowUsers, and limit network exposure with a firewall or bastion.
  • Use drop-in files under /etc/ssh/sshd_config.d/; for most keywords sshd uses the first value it reads.
  • Always validate with sshd -t and check effective settings with sshd -T before reloading.
  • For many servers, SSH certificates signed by an internal CA scale better than copying public keys everywhere.

Why SSH hardening matters

SSH is the main administrative door on most Linux systems. Internet-facing SSH ports receive constant automated login attempts, and weak or reused passwords are the usual way in. Defence here is about reducing what an attacker can try, raising the cost of each attempt, and noticing quickly when something is wrong. Our post on the security implications of open network ports covers the broader exposure problem.

Before you change anything

  1. Keep one working root or sudo session open the whole time.
  2. Confirm you have console access (cloud serial console or out-of-band management) in case you lock yourself out.
  3. Back up the current configuration: sudo cp -a /etc/ssh /etc/ssh.bak.$(date +%F).
  4. Check your OpenSSH version with ssh -V; some options depend on it.

Step 1: create strong keys

Ed25519 keys are short, fast and widely supported. Hardware-backed FIDO2 keys keep the private key on a security token, so a stolen laptop file is not enough.

# Software key protected by a passphrase
ssh-keygen -t ed25519 -C "priya@laptop-2026"

# Hardware-backed key on a FIDO2 token
ssh-keygen -t ed25519-sk -C "priya@yubikey"

# Install the public key on a server
ssh-copy-id -i ~/.ssh/id_ed25519.pub priya@server01

Use a passphrase and an agent (ssh-agent) so the key is not usable if copied. Check permissions on the server: ~/.ssh should be 700 and authorized_keys 600, or sshd will refuse the key when StrictModes is on (the default).

Step 2: use a drop-in hardening file

Modern distributions (RHEL 9 and 10, Ubuntu 22.04 and later, Debian 12) include Include /etc/ssh/sshd_config.d/*.conf near the top of sshd_config. Files are read in lexical order, and for most keywords the first value obtained wins. Name your file with a low number so it takes effect, for example /etc/ssh/sshd_config.d/10-hardening.conf, and check that no earlier file sets a conflicting value.

# /etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
AuthenticationMethods publickey

AllowGroups sshusers

MaxAuthTries 3
LoginGraceTime 30
MaxStartups 10:30:60

X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
PermitTunnel no

ClientAliveInterval 300
ClientAliveCountMax 2

LogLevel VERBOSE

The sshd_config manual lists ChallengeResponseAuthentication as a deprecated alias for KbdInteractiveAuthentication, so use the newer name. For reference, the defaults are MaxAuthTries 6, LoginGraceTime 120 and MaxStartups 10:30:100; the values above are stricter. PermitRootLogin defaults to prohibit-password, which still allows root with a key, so set it to no explicitly. LogLevel VERBOSE records the fingerprint of the key used for each login, which helps audits.

Step 3: test and reload safely

# Syntax check: no output means OK
sudo sshd -t

# Show the effective value of key settings
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|allowgroups|x11forwarding'

# Reload (service name is sshd on RHEL, ssh on Ubuntu and Debian)
sudo systemctl reload sshd

Now open a new terminal and log in. Only close your original session once the new login works.

Step 4: control who can log in

Create a group for SSH users and add only the people who need shell access:

sudo groupadd sshusers
sudo usermod -aG sshusers priya

Service accounts used by automation should have their own keys, restricted in authorized_keys with options such as from="10.0.5.0/24", command="..." or the restrict keyword, so a leaked automation key cannot open an interactive shell from anywhere.

Step 5: reduce network exposure

ControlWhat it doesExample
Host firewallAllows SSH only from trusted rangesfirewall-cmd rich rules or nftables
Cloud security groupBlocks port 22 from the internetAllow 22 only from a bastion or VPN range
Bastion / jump hostSingle hardened entry pointssh -J bastion.example.com app01
Provider session toolsShell access without open inbound portsAWS Systems Manager Session Manager, Azure Bastion, GCP IAP TCP forwarding
Listen addressBinds sshd to a management interface onlyListenAddress 10.0.0.12

Moving SSH to a non-standard port reduces log noise from bots but is not a security control on its own. If you do it on SELinux systems, label the new port with semanage port -a -t ssh_port_t -p tcp 2222 and open it in the firewall.

Step 6: slow down guessing with Fail2ban or sshd limits

With passwords disabled, brute force cannot succeed, but repeated attempts still fill logs. MaxStartups and MaxAuthTries limit what each connection can do. Fail2ban (available from EPEL on RHEL) reads authentication logs and temporarily blocks addresses that fail repeatedly. A minimal jail looks like this:

# /etc/fail2ban/jail.d/sshd.local
[sshd]
enabled  = true
maxretry = 5
findtime = 10m
bantime  = 1h

OpenSSH 9.8 added built-in penalties for clients that repeatedly fail authentication or crash the pre-authentication process. The PerSourcePenalties option controls it and is on by default. Distribution packages may be older, so check ssh -V before relying on it.

Step 7: add multi-factor where it matters

Options, from simplest to most involved:

  • FIDO2 keys (ed25519-sk): something you have, plus a touch, without extra server software.
  • Key plus TOTP : AuthenticationMethods publickey,keyboard-interactive with a PAM module such as Google Authenticator. This requires KbdInteractiveAuthentication yes, so adjust the drop-in file accordingly and test carefully.
  • Identity-aware access : short-lived certificates issued after single sign-on, through tools built around an SSH CA.

Step 8: scale with SSH certificates

Copying public keys to hundreds of servers is hard to audit and hard to revoke. An SSH certificate authority signs user keys with an expiry and allowed principals. Servers trust the CA instead of individual keys.

# On the CA host: sign a user key valid for 8 hours as principal "priya"
ssh-keygen -s /etc/ssh-ca/user_ca -I priya-2026-11-07 -n priya -V +8h id_ed25519.pub

# On each server, trust the CA public key
echo "TrustedUserCAKeys /etc/ssh/user_ca.pub" | sudo tee /etc/ssh/sshd_config.d/20-ca.conf

Short validity periods make revocation mostly automatic: a certificate simply stops working. Protect the CA private key as carefully as any root credential.

Do not forget host keys

User authentication gets most of the attention, but the client also has to trust the server. The first time you connect, SSH shows the server's host key fingerprint and asks you to accept it. Accepting blindly is how man-in-the-middle attacks succeed.

  • Publish host key fingerprints through a trusted channel (an inventory system or the cloud console output) and compare them on first connect. ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub prints the fingerprint on the server.
  • Treat a "REMOTE HOST IDENTIFICATION HAS CHANGED" warning as an incident until you know why. Rebuilt servers are the usual reason, but confirm it.
  • At scale, sign host keys with a host CA. Set HostCertificate on each server and add a @cert-authority line to clients' known_hosts. Clients then trust any server whose key the CA signed, without per-host prompts.
# Sign a server's host key with the host CA (on the CA machine)
ssh-keygen -s /etc/ssh-ca/host_ca -I app01 -h -n app01.example.com -V +52w ssh_host_ed25519_key.pub

# Client side: trust the host CA for a domain
echo "@cert-authority *.example.com $(cat host_ca.pub)" >> ~/.ssh/known_hosts

Step 9: keep crypto and software current

  • Patch OpenSSH through your distribution's updates; serious bugs (for example CVE-2024-6387, "regreSSHion", a race condition that affected portable OpenSSH 8.5p1 to 9.7p1 and was addressed in 9.8, with distribution backports) are fixed this way. See automating Linux package updates.
  • On RHEL, prefer system-wide crypto policies (update-crypto-policies --show) over hand-editing cipher lists.
  • Remove old RSA keys under 3072 bits and any DSA keys. OpenSSH 10.0 (April 2025) removed DSA support completely.

Step 10: monitor and audit

# Recent SSH authentication events (systemd journal)
sudo journalctl -u sshd --since "1 hour ago"

# On Ubuntu and Debian the unit is ssh
sudo journalctl -u ssh --since today | grep -E "Accepted|Failed"

# Who is logged in now, and recent logins
who
last -n 20

Forward these logs to a central system and alert on logins from unexpected addresses, logins by service accounts outside their usual hours, and any successful root login (which should be impossible after Step 2).

Hardening checklist summary

AreaSetting or actionVerify with
KeysEd25519 or ed25519-sk with passphrasessh-keygen -l -f key.pub
RootPermitRootLogin nosshd -T | grep permitrootlogin
PasswordsPasswordAuthentication no, KbdInteractiveAuthentication nossh -o PubkeyAuthentication=no user@host should fail
WhoAllowGroups sshusersLogin as a non-member should fail
ForwardingX11, agent and TCP forwarding off unless neededsshd -T
NetworkPort 22 limited to bastion or VPNSecurity group and firewall rules
UpdatesRegular OpenSSH patchesssh -V, package manager
LogsCentral collection and alertsSIEM or log platform queries

Related reading

SSH also matters for Git: see SSH vs HTTPS for cloning Git repositories. For why SSH replaced Telnet in the first place, read why HTTPS, FTPS and SSH are considered secure.

Build the skill

SSH configuration, key management, firewalld and SELinux port labelling are part of the RHCSA objectives. Our RHCSA training and Linux administration course practise them on real systems, in Kharadi, Pune and live online.

Frequently asked questions

What is the most important SSH hardening step?

Switch to key-based authentication and disable password login. That removes password guessing, the most common SSH attack.

Should I use RSA or Ed25519 keys?

Ed25519 is the usual choice for new keys. Use RSA of at least 3072 bits only where older systems cannot handle Ed25519.

Where should I put SSH hardening settings?

In a drop-in file under /etc/ssh/sshd_config.d/ with a low number. sshd uses the first value it reads for most keywords.

How do I test sshd_config without locking myself out?

Run sshd -t for syntax, sshd -T for effective values, keep your current session open, reload, and test a new login before closing anything.

Does changing the SSH port improve security?

It reduces automated noise in logs but does not stop a targeted attacker. Combine it with keys, firewall rules and monitoring, or skip it.

What is an SSH certificate authority?

A trusted key that signs user or host keys with expiry dates and principals. Servers trust the CA, so you do not copy individual keys everywhere.

How do I add two-factor authentication to SSH?

Use FIDO2 security keys with ed25519-sk, or combine public key with a TOTP PAM module through AuthenticationMethods.

How do I see failed SSH logins?

Use journalctl for the sshd or ssh unit and look for "Failed" and "Invalid user" lines, or check /var/log/secure on RHEL and /var/log/auth.log on Debian-based systems.

References

Interested in learning more?Get a callback from our training advisors.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0
Mridul I am a passionate technology enthusiast with a strong focus on DevOps, Cloud Computing, and Cybersecurity. Through my blogs at DevOps Training Institute, I aim to simplify complex concepts and share practical insights for learners and professionals. My goal is to empower readers with knowledge, hands-on tips, and industry best practices to stay ahead in the ever-evolving world of DevOps.