How to secure SSH access to a LWS VPS: keys only and fail2ban

Procédure

To secure SSH access to your LWS VPS, it is recommended to completely disable password authentication in favor of an SSH key, then install fail2ban. This setup protects your server against brute-force attacks and eliminates the risks associated with intercepted or guessed passwords. In this tutorial, you will copy your public SSH key, adjust the SSH configuration to override cloud-init default settings, then install and verify fail2ban's active protection.

Install your SSH key

  1. Connect once via SSH, then disconnect

    Open a terminal on your computer and connect to the server using the root password received in your delivery email. During the first connection, accept the server key fingerprint by typing yes. Once connected, immediately end the session by running exit to return to your local machine.

    ⌨ On your computer
    ssh root@203.0.113.10
    exit
  2. Copy your public SSH key to the VPS

    From your computer, use the ssh-copy-id command to copy your public key to the server. Enter your root password one last time when the terminal prompts you. If you had already added your SSH key during the order or reinstallation from the KVM panel, this step is not necessary.

    ⌨ On your computer
    ssh-copy-id root@203.0.113.10
    Expected result
    Number of key(s) added: 1
    
    Now try logging into the machine, with: "ssh 'root@203.0.113.10'"
    and check to make sure that only the key(s) you wanted were added.
  3. Verify SSH key-based login

    Reconnect to your VPS via SSH from your computer. The connection should be established instantly without asking for the server root password, confirming that your public key has been installed correctly.

    ⌨ On your computer
    ssh root@203.0.113.10

Disable password login

  1. Spot the cloud-init file trap

    List the files present in the /etc/ssh/sshd_config.d/ directory and display the contents of 50-cloud-init.conf. This file enables password authentication. Since the SSH server keeps the first directive encountered when reading files in alphabetical order, your own configuration must necessarily be read before it.

    ⌨ On the server
    ls /etc/ssh/sshd_config.d/
    cat /etc/ssh/sshd_config.d/50-cloud-init.conf
    Expected result
    PasswordAuthentication yes
  2. Create the priority SSH configuration file

    Open the nano editor to create the file 00-key-only.conf. The 00 prefix ensures that this file will be processed before 50-cloud-init.conf. Add the directives disabling passwords and limiting root access to keys, then save with Ctrl+X, confirm with O (or Y depending on the language), and press Enter.

    ⌨ On the server
    nano /etc/ssh/sshd_config.d/00-cles-uniquement.conf
    📄 /etc/ssh/sshd_config.d/00-cles-uniquement.conf
    # SSH login by key only
    PasswordAuthentication no
    KbdInteractiveAuthentication no
    PermitRootLogin prohibit-password
  3. Test the syntax and apply the configuration

    Run sshd -t to check for syntax errors, then verify the active values with sshd -T. Note that permitrootlogin displays without-password, which corresponds to prohibit-password. If everything is correct, reload the SSH service to apply the new settings without interrupting your session.

    ⌨ On the server
    sshd -t
    sshd -T | grep -E "^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin)"
    systemctl reload ssh
    Expected result
    permitrootlogin without-password
    passwordauthentication no
    kbdinteractiveauthentication no

Test SSH access

  1. Confirm password rejection and key-based access

    Without closing your active session, open a second terminal window on your computer. First try a connection by forcing key rejection with the PubkeyAuthentication=no option: the server must refuse access. Then start a normal connection: key-based authentication should succeed.

    ⌨ On your computer
    ssh -o PubkeyAuthentication=no root@203.0.113.10
    ssh root@203.0.113.10
    Expected result
    root@203.0.113.10: Permission denied (publickey).

Install and verify fail2ban

  1. Install fail2ban

    From your session connected to the VPS, update the Debian repositories list with apt update, then install the fail2ban package. On recent Debian versions, fail2ban automatically enables protection for the SSH service as soon as installation is complete.

    ⌨ On the server
    apt update
    apt install -y fail2ban
  2. Check fail2ban status and close the session

    Make sure the fail2ban service is active with systemctl, then query the sshd jail to confirm that filtering works and monitors connections. You can then disconnect with complete peace of mind.

    ⌨ On the server
    systemctl is-active fail2ban
    fail2ban-client status sshd
    exit
    Expected result
    active
    Status for the jail: sshd
    |- Filter
    |  |- Currently failed: 1
    |  |- Total failed:     3
    |  `- Journal matches:  _SYSTEMD_UNIT=ssh.service + _COMM=sshd
    `- Actions
       |- Currently banned: 0
       |- Total banned:     0
       `- Banned IP list:

Glossary

SSH (Secure Shell)
A secure communication protocol that allows you to connect to and administer a remote server from the command line.
Public key
A key file placed on the remote server to verify the user's identity without transmitting a password.
Private key
A strictly confidential key file kept on your local computer and used to prove your identity to the server.
Cloud-init
An automation tool used during the initial deployment of virtual machines to initialize their network and access settings.
fail2ban
Security software that monitors system logs and temporarily blocks suspicious IP addresses at the firewall level.
Jail
In fail2ban, a set combining a detection filter for abusive attempts and a ban rule for a given service.

Frequently asked questions

Why name the file 00-key-only.conf instead of modifying sshd_config?

The OpenSSH daemon reads the files in the sshd_config.d folder in alphabetical order and applies the first value it encounters. By using the 00 prefix, your directives are processed before those in 50-cloud-init.conf, which guarantees that password blocking is taken into account.

What should I do if I get locked out of my VPS?

If you lose SSH access, log in to your LWS customer area and open the KVM / VNC console available for your VPS. This console simulates a physical screen and keyboard connected to the server and lets you correct your configuration files without using SSH.

What is the difference between prohibit-password and without-password?

It is exactly the same setting in OpenSSH. prohibit-password is the recommended modern syntax, but the sshd -T command displays its historical equivalent without-password in the list of active directives.

Do you need to configure fail2ban after installing it on Debian?

On recent distributions such as Debian 13, fail2ban automatically configures and starts the jail for the SSH service as soon as the package is installed. You can customize ban durations later in a jail.local file if you wish.

help.lws.net/a/7926
Was this article helpful?

Yes

No

Read 1 times

Thank you! Feel free to ask questions about our documentation if you would like more information, and help us improve it.

Similar articles

A question about this article?

Ask the LWS team and the community. Answers are published after moderation.

Ask the LWS team and its community a question

GDPR: Controller: LWS-Ligne Web Services. Purpose: moderation and public publication of your question, possible notification of an answer. Legal basis: consent (Art. 6(1)(a) GDPR). Email retention: 90 days after notification, 12 months maximum without an answer. To exercise your rights, see the GDPR section of our Terms and Conditions.