> ## Documentation Index
> Fetch the complete documentation index at: https://blog.onetsolutions.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Harden SSH on a Linux VPS without locking yourself out

> Harden SSH on your Linux VPS without locking yourself out: Ed25519 keys only, password login off, fail2ban and the console firewall. An OnetSolutions guide.

*Published on November 30, 2026*

Hardening SSH on a Linux VPS takes a few lines of configuration. What holds people back is not security: it is the fear of locking themselves out when `sshd` reloads.

That risk dropped a lot in November. The OnetSolutions console can now [change a VPS password in one click](https://blog.onetsolutions.net/en/reset-vps-root-password.md) and [restart it in rescue mode](https://blog.onetsolutions.net/en/vps-rescue-mode-repair-server-that-wont-boot.md). Together with the [KVM console](https://help.onetsolutions.net/vps/kvm-console), which goes through neither SSH nor the network, you always have a way back in.

This guide hardens SSH in an order that keeps access at every step, on Debian, Ubuntu, AlmaLinux and Rocky Linux: Ed25519 keys only, no more passwords, root by key or not at all, fail2ban, then the console firewall.

## What you change, and how to check it

Summary as of November 30, 2026:

| Setting | Effect | Check |
| - | - | - |
| `PasswordAuthentication no` | No more SSH password logins | `ssh -o PubkeyAuthentication=no ...` returns `Permission denied (publickey)` |
| `KbdInteractiveAuthentication no` | Closes the second password path, through PAM | Same test, with `keyboard-interactive` |
| `AuthenticationMethods publickey` | Only a key opens a session | `sshd -T` shows `authenticationmethods publickey` |
| `PubkeyAcceptedAlgorithms ssh-ed25519,...` | Only Ed25519 keys are accepted | `sshd -T` shows the list |
| `PermitRootLogin prohibit-password` or `no` | Root by key only, or never over SSH | `sshd -T` shows `permitrootlogin` |
| fail2ban `sshd` jail | Bans addresses that pile up failures | `fail2ban-client status sshd` |
| SSH rule in the console firewall | Port 22 only answers your addresses | Connecting from another address: no answer |

## Before you start: two sessions and a way back in

Three rules keep you from losing access:

1. **Keep one SSH session open** from start to finish. It stays your workbench if the new configuration refuses logins.
2. **Test every change in a second session**, never in the one you keep open.
3. **Validate before reloading**: `sshd -t` checks the configuration without applying anything.

The service is called `ssh` on Debian and Ubuntu, `sshd` on AlmaLinux and Rocky Linux. The commands below give both.

> [!TIP]
>
> Find the KVM console on your VPS page in the [console](https://console.onetsolutions.net) now. It opens a local session on the server, independent of SSH and of the firewall.

## Install an Ed25519 SSH key

Generate the key on your workstation. Ed25519 is the type our [SSH keys documentation](https://help.onetsolutions.net/vps/ssh-keys) recommends:

```bash theme={null}
# On your workstation: an Ed25519 key protected by a passphrase
ssh-keygen -t ed25519 -C "you@example.com"
```

Copy the public key to the VPS, then test a login that accepts the key only:

```bash theme={null}
# Replace with your VPS IP address
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@203.0.113.10

# Should open a session without asking for the server password
ssh -o PreferredAuthentications=publickey root@203.0.113.10
```

On the VPS, list the authorized keys and their type. The type is shown in parentheses at the end of each line:

```bash theme={null}
ssh-keygen -lf ~/.ssh/authorized_keys
```

**Good result:** every key you need shows `(ED25519)`. An `(RSA)` or `(ECDSA)` key will be refused at the next step: replace it with an Ed25519 key first.

> [!NOTE]
>
> A key saved in the console is installed when a VPS is created or reinstalled. For a server already running, `ssh-copy-id` or a line added to `~/.ssh/authorized_keys` does the job.

## Create a sudo user so you can do without root

This step is optional if you keep root by key. You need it if you want to forbid root over SSH.

On Debian and Ubuntu:

```bash theme={null}
sudo adduser alice
sudo usermod -aG sudo alice
```

On AlmaLinux and Rocky Linux:

```bash theme={null}
sudo useradd -m -G wheel alice
sudo passwd alice
```

The password is for `sudo`, not for SSH. Then give the user root's keys:

```bash theme={null}
sudo install -d -m 700 -o alice -g alice /home/alice/.ssh
sudo install -m 600 -o alice -g alice /root/.ssh/authorized_keys /home/alice/.ssh/authorized_keys

# AlmaLinux and Rocky Linux: restore the SELinux context of the copied files
sudo restorecon -Rv /home/alice/.ssh
```

In a second session, check the login and the privileges:

```bash theme={null}
ssh -o PreferredAuthentications=publickey alice@203.0.113.10
sudo -v && echo "sudo works"
```

## Where to put your SSH configuration: a file in sshd\_config.d

The OpenSSH manual is explicit: for each keyword, **the first value read wins**. Included files are read in the lexical order of their names.

Debian, Ubuntu, AlmaLinux and Rocky Linux include `/etc/ssh/sshd_config.d/*.conf` at the top of `/etc/ssh/sshd_config`. Check it, and look at what the directory already holds:

```bash theme={null}
grep -n '^Include' /etc/ssh/sshd_config
ls -l /etc/ssh/sshd_config.d/
```

**Good result:** the line `Include /etc/ssh/sshd_config.d/*.conf` appears before any other active directive.

You will often find files put there by the system:

| File | Origin | What to watch |
| - | - | - |
| `50-cloud-init.conf` | cloud-init, when it sets password authentication | `PasswordAuthentication yes` or `no` |
| `50-redhat.conf` | AlmaLinux and Rocky Linux OpenSSH package | Distribution settings and crypto policy |
| `01-permitrootlogin.conf` or `25-permitrootlogin.conf` | Installer or upgrade on AlmaLinux and Rocky Linux | `PermitRootLogin yes` |

A file named `10-...` is read after `01-` but before `25-` and `50-`. To make yours win over all of them, start its name with `00-`. It stays in charge even if cloud-init rewrites its own file on a later boot.

Record the effective configuration before you touch anything:

```bash theme={null}
sudo sshd -T | grep -E '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|authenticationmethods|pubkeyacceptedalgorithms) '
```

## Disable SSH password authentication

Create the hardening file. Pick the `PermitRootLogin` line that fits you:

```bash theme={null}
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
# No more passwords over SSH, neither directly nor through PAM
PasswordAuthentication no
KbdInteractiveAuthentication no
# An SSH session opens with a key only
AuthenticationMethods publickey
# Ed25519 keys only, FIDO security keys included
PubkeyAcceptedAlgorithms ssh-ed25519,sk-ssh-ed25519@openssh.com
# Root by key only. Change to "no" if you go through a sudo user
PermitRootLogin prohibit-password
EOF
```

`KbdInteractiveAuthentication` matters as much as `PasswordAuthentication`. The cloud-init documentation points it out: with PAM, keyboard-interactive authentication can still accept a password. Debian and Ubuntu already turn it off in their `sshd_config`; declaring it here costs nothing.

Validate, then reload:

```bash theme={null}
# No output: the configuration is valid
sudo sshd -t

# Debian, Ubuntu
sudo systemctl reload ssh
# AlmaLinux, Rocky Linux
sudo systemctl reload sshd
```

If `sshd -t` prints an error, fix the file and run the test again. Do not reload until it is silent.

## Check that passwords are really refused

Read the effective configuration again:

```bash theme={null}
sudo sshd -T | grep -E '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|authenticationmethods|pubkeyacceptedalgorithms) '
```

**Good result:**

```text theme={null}
permitrootlogin prohibit-password
pubkeyacceptedalgorithms ssh-ed25519,sk-ssh-ed25519@openssh.com
passwordauthentication no
kbdinteractiveauthentication no
authenticationmethods publickey
```

Depending on the OpenSSH version, `prohibit-password` shows under its older name, `without-password`: it is the same setting.

Then, from your workstation and in a second session, the three tests that count:

```bash theme={null}
# 1. The key still works
ssh -o PreferredAuthentications=publickey alice@203.0.113.10

# 2. Passwords are refused, on both paths
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive alice@203.0.113.10

# 3. Same test for root
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive root@203.0.113.10
```

**Good result:** test 1 opens a session. Tests 2 and 3 stop at `Permission denied (publickey).` without ever asking for a password.

With `PermitRootLogin no`, add a fourth test: `ssh -o PreferredAuthentications=publickey root@203.0.113.10` must be refused too. Close the first session only once everything checks out.

## Set up fail2ban on the sshd jail

Without passwords, brute force leads nowhere. fail2ban still earns its place: it bans the bots that cycle through usernames, and trims the log accordingly.

What each distribution's package does, checked on November 30, 2026:

| Distribution | fail2ban version | `sshd` jail after install |
| - | - | - |
| Debian 13 (Trixie) | 1.1.0-8 | Enabled, reads the systemd journal |
| Debian 12 (Bookworm) | 1.0.2-2 | Enabled, but looks for `/var/log/auth.log`: needs a fix |
| Ubuntu 24.04 LTS | 1.0.2-3ubuntu0.1 | Enabled, reads the systemd journal |
| Ubuntu 26.04 LTS | 1.1.0-9 | Enabled, reads the systemd journal |
| AlmaLinux, Rocky Linux (EPEL) | 1.1.0 | To enable |

Debian 12 no longer installs `rsyslog` by default: without it there is no `/var/log/auth.log`, and the default jail does not start. The fix landed in package 1.0.2-3, which Debian 12 did not get. The `backend = systemd` setting below takes its place.

### Debian and Ubuntu

```bash theme={null}
sudo apt install fail2ban python3-systemd
```

Put your settings in a `.local` file, read after the package configuration. List in `ignoreip` the addresses you manage the server from:

```bash theme={null}
sudo tee /etc/fail2ban/jail.d/sshd.local >/dev/null <<'EOF'
[sshd]
enabled = true
backend = systemd
# Your addresses are never banned
ignoreip = 127.0.0.1/8 ::1 198.51.100.7
maxretry = 5
findtime = 10m
bantime = 1h
EOF

sudo systemctl restart fail2ban
```

### AlmaLinux and Rocky Linux

fail2ban comes from the EPEL repository. The `fail2ban-systemd` package sets it up to read the systemd journal:

```bash theme={null}
sudo dnf install epel-release
sudo dnf install fail2ban-server fail2ban-systemd
```

```bash theme={null}
sudo tee /etc/fail2ban/jail.d/sshd.local >/dev/null <<'EOF'
[DEFAULT]
# Ban through nftables
banaction = nftables
banaction_allports = nftables[type=allports]

[sshd]
enabled = true
ignoreip = 127.0.0.1/8 ::1 198.51.100.7
maxretry = 5
findtime = 10m
bantime = 1h
EOF

sudo systemctl enable --now fail2ban
```

### Check fail2ban

```bash theme={null}
# Jail status: current failures, banned addresses
sudo fail2ban-client status sshd

# Does the filter recognise sshd's journal entries?
sudo fail2ban-regex systemd-journal sshd
```

**Good result:** `fail2ban-client status sshd` answers with the `Currently failed` and `Total banned` counters and the `Banned IP list`. `fail2ban-regex` counts matched lines as soon as bots have tried their luck.

To lift a ban:

```bash theme={null}
sudo fail2ban-client set sshd unbanip 198.51.100.7
```

## Restrict SSH to your IP addresses with the console firewall

The console firewall is set up without installing anything on the VPS. Reserved for your addresses, port 22 stops answering bots.

First note the address you are connected from:

```bash theme={null}
# On the VPS: the first value is your workstation's address
echo "$SSH_CLIENT"
```

Then, on your VPS **Firewall** page in the [console](https://console.onetsolutions.net):

| Field | Value |
| - | - |
| Priority | 10 |
| Direction | INPUT |
| Action | ACCEPT |
| Protocol | TCP |
| Source IP | Your address, or that of your VPN or bastion |
| Destination port | 22 |

Add one rule per admin address.

> [!WARNING]
>
> From the first rule on, the firewall only accepts the traffic you allow. If the VPS serves a website, add the port 80 and 443 rules right away, as described in the [firewall documentation](https://help.onetsolutions.net/vps/firewall).

**Check:** your SSH session still opens from your workstation. From another connection, a mobile hotspot for example, `ssh` gets no answer until it times out.

## Locked out: get back in from the console

Each situation has its way out:

| Situation | Action |
| - | - |
| A session is still open | Fix `/etc/ssh/sshd_config.d/00-hardening.conf`, then `sshd -t` and reload |
| No session left, but you know the password | Open the [KVM console](https://help.onetsolutions.net/vps/kvm-console) and log in locally: `PermitRootLogin` and `PasswordAuthentication` only apply to SSH |
| No session left and the password is unknown | [Change the password](https://help.onetsolutions.net/vps/reset-password) from the console (**Connect (SSH)**, then **Change password**): the VPS reboots, then log in through the KVM console |
| Password change is turned off or cloud-init is missing | Start [rescue mode](https://help.onetsolutions.net/vps/rescue-mode) and repair from the rescue system (below) |
| Your IP address changed and the firewall blocks you | Edit the SSH rule on the console **Firewall** page |
| fail2ban banned your address | Through the KVM console: `fail2ban-client set sshd unbanip <your-ip>`, then add the address to `ignoreip` |

In rescue mode, the console opens a root session on the rescue system. Mount your system partition, then remove the hardening file or add your key:

```bash theme={null}
# Find the system partition, then mount it
lsblk -f
mount /dev/sda1 /mnt

# Either remove the hardening file...
rm /mnt/etc/ssh/sshd_config.d/00-hardening.conf
# ...or add a new public key for root
cat >> /mnt/root/.ssh/authorized_keys <<'EOF'
ssh-ed25519 AAAA... you@example.com
EOF

umount /mnt
```

Replace `/dev/sda1` with the partition `lsblk -f` points to, and the `ssh-ed25519 AAAA...` line with your public key. Then leave rescue mode from the **Rescue mode** tab: the VPS restarts on its own system.

## Common pitfalls

**A `PasswordAuthentication yes` read before yours.** The first value wins. A `01-` file or a setting placed above the `Include` line takes precedence: only `sshd -T` tells the truth.

**Forgetting `KbdInteractiveAuthentication`.** On a system where PAM enables it, passwords are still accepted that way. The `keyboard-interactive` test reveals it.

**An RSA key still in use.** With `PubkeyAcceptedAlgorithms` limited to Ed25519, an old RSA key, such as a deployment tool's, is refused. Take stock with `ssh-keygen -lf` before reloading.

**Reloading without `sshd -t`.** One typo in the file and the service refuses new logins. The test takes a second.

**Banning your own address.** A few key failures from a misconfigured workstation are enough. Put your fixed addresses in `ignoreip`.

**Updating the firewall after your address changes.** If your address changes, add the new rule before removing the old one.

## Going further

* [SSH keys](https://help.onetsolutions.net/vps/ssh-keys): generate a key and save it in the console for your next VPS
* [Firewall](https://help.onetsolutions.net/vps/firewall): create and order your VPS rules
* [KVM console](https://help.onetsolutions.net/vps/kvm-console), [password change](https://help.onetsolutions.net/vps/reset-password) and [rescue mode](https://help.onetsolutions.net/vps/rescue-mode): the console's three ways back in
* [Lost your VPS root password](https://blog.onetsolutions.net/en/reset-vps-root-password.md) and [VPS that won't boot: rescue mode](https://blog.onetsolutions.net/en/vps-rescue-mode-repair-server-that-wont-boot.md), step by step
* [Automatic security updates](https://blog.onetsolutions.net/en/automatic-security-updates-linux-vps.md): so OpenSSH gets its fixes without delay
* [regreSSHion (CVE-2024-6387)](https://blog.onetsolutions.net/en/regresshion-cve-2024-6387.md): why an up-to-date sshd matters as much as its configuration

Have a question about a server hosted with us? [Our team is available](https://help.onetsolutions.net/console/support) from your client area.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.