Hardening OpenSSH on a remote Linux host is straightforward until two modern distro changes trip you up: cloud images shipping a 50-cloud-init.conf snippet that re-enables passwords behind your back, and Ubuntu switching openssh-server to systemd socket activation (ssh.socket). On servers I manage, nearly every unexplained SSH lockout or ignored port change comes down to skipping sshd -t or restarting the wrong systemd unit.
This guide covers your local workstation plus Ubuntu 24.04/26.04 LTS, Debian 12/13, Fedora, RHEL/Alma/Rocky, and Arch Linux targets.
Diagram: Original SVG schematic by Ajay Kumar Chaturvedi.
Prerequisites and Safety Checks
Confirm these items before modifying SSH server settings:
- Existing shell access to the remote Linux server (
user@host) withsudorights. - Console or serial rescue access available in your VPS or hypervisor dashboard if something goes wrong.
- Open firewall allowance for SSH (if you plan to change port 22, open the new port in your firewall before switching
sshd).
Backup and Lockout Warning: Editingsshdconfiguration on a remote server without console access can lock you out permanently if syntax fails or password login is disabled before your key works. Back up/etc/ssh/sshd_configfirst, validate every edit withsudo sshd -t, and keep one working SSH session open while testing in a second terminal.
Step-by-Step Procedure
Step 1: Generate an Ed25519 Key Pair on Your Workstation
Target OS: Any Linux or macOS workstation running OpenSSH.
Per Ubuntu's OpenSSH documentation and the `ssh-keygen(1)` manual, Ed25519 is the preferred algorithm (and default when -t is omitted in modern OpenSSH) because it produces short keys with strong security and fast handshakes:
ssh-keygen -t ed25519 -C "workstation-2026"Accept the default path (~/.ssh/id_ed25519) or enter a custom filename if you keep separate keys per environment. Set a real passphrase when prompted so a stolen laptop backup does not hand away root access. Use ssh-agent locally so you only type that passphrase once per login session.
Step 2: Copy Your Public Key Safely and Verify Permissions
Target OS: Local workstation pushing to Ubuntu, Debian, Fedora, RHEL, or Arch.
First run ssh-copy-id with -n (dry-run mode, documented in the `ssh-copy-id(1)` manual) to confirm which exact key will be uploaded rather than pushing every identity sitting in your agent:
ssh-copy-id -n -i ~/.ssh/id_ed25519.pub user@server-ipOnce the dry-run prints only your intended .pub key, install it on the server:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ipNow prove key authentication works with passwords explicitly blocked on the client before touching any server daemon config:
ssh -i ~/.ssh/id_ed25519 -o PubkeyAuthentication=yes -o PasswordAuthentication=no user@server-ipIf that login drops you into a shell without asking for your account password, verify permissions on the remote user's directory and authorized keys file so OpenSSH StrictModes never rejects them:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keysStep 3: Back Up sshd_config and Inspect Drop-In Precedence
Target OS: Ubuntu, Debian, Fedora, RHEL, Arch.
On the remote server, create a read-only backup copy of the stock configuration as recommended in Ubuntu's OpenSSH docs:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original
sudo chmod a-w /etc/ssh/sshd_config.originalCheck what drop-ins already exist in /etc/ssh/sshd_config.d/:
ls -la /etc/ssh/sshd_config.d/According to the `sshd_config(5)` manual, OpenSSH applies the first obtained value for each directive. Modern Ubuntu and Debian ship Include /etc/ssh/sshd_config.d/*.conf at the very top of /etc/ssh/sshd_config. If your cloud provider dropped 50-cloud-init.conf containing PasswordAuthentication yes, naming your file 99-hardening.conf loses to 50-cloud-init.conf! Putting your rules in 00-hardening.conf ensures your file is parsed first and wins.
Step 4: Write the Hardening Drop-In and Validate with sshd -t
Target OS: Ubuntu, Debian, Fedora, RHEL, Arch.
Create /etc/ssh/sshd_config.d/00-hardening.conf restricting logins to public keys, disabling root SSH logins, and disabling keyboard-interactive fallbacks:
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitEmptyPasswords no
MaxAuthTries 3
X11Forwarding no
EOFI would skip changing the default port 22 just for obscurity unless you are trying to cut automated scanner noise in logs, because moving ports forces extra firewall, SELinux, and client config bookkeeping for zero cryptographic gain. If you do use a custom port (say 2222) on Fedora or RHEL with SELinux enforcing, label that port first per the `semanage-port(8)` manual before adding Port 2222:
# Fedora / RHEL / Alma / Rocky only when using a non-22 port
sudo semanage port -a -t ssh_port_t -p tcp 2222Before restarting anything, test syntax with the `sshd(8)` test flag and inspect the effective parsed values:
sudo sshd -t
sudo sshd -T | grep -E '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication)'If sudo sshd -t prints any warning or exits non-zero, fix the file immediately before proceeding.
Step 5: Reload the Correct Systemd Unit for Your Distro
Target OS: Ubuntu 24.04/26.04 LTS (ssh.socket) vs Debian/Fedora/RHEL/Arch (ssh.service / sshd.service).
Keep your current terminal session open! On Ubuntu 24.04 and 26.04 LTS (and Ubuntu 22.10+ using socket activation per Ubuntu's socket-activation note), ssh.socket owns port binding via sshd-socket-generator:
# Ubuntu 24.04 / 26.04 LTS (use daemon-reload + restart ssh.socket if Port or ListenAddress changed)
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
sudo systemctl restart ssh.serviceOn Debian (non-socket unit), restart ssh.service, and on Fedora, RHEL, Alma, Rocky, or Arch, restart sshd.service:
# Debian
sudo systemctl restart ssh.service
# Fedora / RHEL / Arch Linux
sudo systemctl restart sshd.serviceVerification and Expected Result
With your first terminal still open as a lifeline, open a brand-new terminal on your workstation and test both key login and password rejection:
# Must succeed using your key
ssh -i ~/.ssh/id_ed25519 user@server-ip
# Must fail with Permission denied (publickey)
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password user@server-ipVerify listening sockets on the server with ss -tlnp | grep -E ':22\b|sshd'. Expected result: sshd -T prints passwordauthentication no and permitrootlogin no, key-based login connects cleanly, and password-only attempts are rejected immediately without falling back to KbdInteractiveAuthentication.
Troubleshooting
- Password login still prompts even after editing `00-hardening.conf`: Check
/etc/ssh/sshd_configto confirmInclude /etc/ssh/sshd_config.d/*.confsits near the top of the file (above any defaultPasswordAuthenticationline), and runsudo grep -rn PasswordAuthentication /etc/ssh/to catch overrides. - Ubuntu ignores a new `Port` in `sshd_config.d/`:
ssh.socketcaches the generatedListenStreamunit untilsudo systemctl daemon-reloadruns, followed bysudo systemctl restart ssh.socket. - Key ignored on login (`Permission denied (publickey)`): Tail
sudo journalctl -fu ssh.service(Ubuntu/Debian) orsudo journalctl -fu sshd.service(Fedora/Arch) while connecting; the most common culprit is group/world write permissions on/home/user(chmod 755 ~),~/.ssh(700), orauthorized_keys(600), or missingrestorecon -Rv ~/.sshon SELinux hosts.
