How-To Guides5 min read

How to Set Up Ed25519 SSH Keys and Harden sshd Without Locking Yourself Out

Generate Ed25519 SSH keys, deploy them safely with ssh-copy-id, and harden OpenSSH via drop-in configs across Ubuntu 24.04/26.04 (ssh.socket), Debian, Fedora, and Arch Linux.

Generate an Ed25519 key with ssh-keygen -t ed25519, install it via ssh-copy-id, and confirm key-only login before editing server settings. Place hardening directives in /etc/ssh/sshd_config.d/00-hardening.conf, validate with sudo sshd -t, and reload ssh.socket (after daemon-reload for port changes on Ubuntu 24.04/26.04) or sshd.service on Fedora/RHEL/Arch while keeping a fallback SSH session open.

Key takeaways

  • Never disable PasswordAuthentication until you prove key-based login succeeds with PasswordAuthentication=no from your workstation.
  • Use low-numbered drop-in files like 00-hardening.conf so cloud-init snippets such as 50-cloud-init.conf cannot silently override password rules.
  • Distinguish Ubuntu ssh.socket activation from traditional sshd.service so port and listen-address changes actually bind.

Quick facts

  • OpenSSH ssh-keygen defaults to Ed25519, offering compact keys and fast verification without legacy RSA-1024/2048 weaknesses.
  • sshd reads Include /etc/ssh/sshd_config.d/*.conf at the top of sshd_config and applies the first obtained value per directive.
  • On Ubuntu 22.10 through 26.04 LTS, ssh.socket controls listening ports via sshd-socket-generator, requiring daemon-reload after Port changes.
  • On Fedora and RHEL derivatives with SELinux enforcing, custom SSH ports must be labeled ssh_port_t with semanage before restarting sshd.
  • Always test a new key-authenticated session in a second terminal before closing your existing root or sudo session.

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.

SSH Ed25519 Key Setup and Distro sshd Hardening Flow
SSH Ed25519 Key Setup and Distro sshd Hardening Flow

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) with sudo rights.
  • 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: Editing sshd configuration 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_config first, validate every edit with sudo 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:

BASH
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:

BASH
ssh-copy-id -n -i ~/.ssh/id_ed25519.pub user@server-ip

Once the dry-run prints only your intended .pub key, install it on the server:

BASH
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip

Now prove key authentication works with passwords explicitly blocked on the client before touching any server daemon config:

BASH
ssh -i ~/.ssh/id_ed25519 -o PubkeyAuthentication=yes -o PasswordAuthentication=no user@server-ip

If 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:

BASH
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Step 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:

BASH
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original
sudo chmod a-w /etc/ssh/sshd_config.original

Check what drop-ins already exist in /etc/ssh/sshd_config.d/:

BASH
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:

BASH
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
EOF

I 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:

BASH
# Fedora / RHEL / Alma / Rocky only when using a non-22 port
sudo semanage port -a -t ssh_port_t -p tcp 2222

Before restarting anything, test syntax with the `sshd(8)` test flag and inspect the effective parsed values:

BASH
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:

BASH
# 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.service

On Debian (non-socket unit), restart ssh.service, and on Fedora, RHEL, Alma, Rocky, or Arch, restart sshd.service:

BASH
# Debian
sudo systemctl restart ssh.service

# Fedora / RHEL / Arch Linux
sudo systemctl restart sshd.service

Verification 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:

BASH
# 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-ip

Verify 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_config to confirm Include /etc/ssh/sshd_config.d/*.conf sits near the top of the file (above any default PasswordAuthentication line), and run sudo grep -rn PasswordAuthentication /etc/ssh/ to catch overrides.
  • Ubuntu ignores a new `Port` in `sshd_config.d/`: ssh.socket caches the generated ListenStream unit until sudo systemctl daemon-reload runs, followed by sudo systemctl restart ssh.socket.
  • Key ignored on login (`Permission denied (publickey)`): Tail sudo journalctl -fu ssh.service (Ubuntu/Debian) or sudo journalctl -fu sshd.service (Fedora/Arch) while connecting; the most common culprit is group/world write permissions on /home/user (chmod 755 ~), ~/.ssh (700), or authorized_keys (600), or missing restorecon -Rv ~/.ssh on SELinux hosts.

Sources