Skip to content
HOSTIGAN
Linux

What is SSH, and how do you secure your SSH connection?

Learn how SSH works and harden it on a Linux server: ed25519 keys, sshd_config.d drop-ins, no root or password logins, Fail2ban, firewall rules and optional 2FA.

Intermediate 5 min read Updated November 24, 2026

SSH (Secure Shell) is how you manage a Linux server: every command you run remotely goes through it. That also makes it the most attacked service on almost any server. Within minutes of a new VPS going online, bots start trying to log in as root with common passwords, and they never stop.

This guide explains briefly how SSH works and then hardens it step by step: keys instead of passwords, a clean configuration in a drop-in file, Fail2ban, firewall rules and, if you need it, two-factor authentication. If you have never connected before, read how to connect to a Linux VPS with SSH first.

How SSH works

SSH is a client-server protocol. On the server, the sshd daemon listens on TCP port 22. On your computer, an SSH client (OpenSSH is built into Linux, macOS and Windows 10/11) connects to it. A login goes through roughly these stages:

  1. Connection to port 22.
  2. Host key check. The server proves its identity with a host key. Your client stores its fingerprint in ~/.ssh/known_hosts on the first connection.
  3. Key exchange. Both sides agree on session keys, and everything after this point is encrypted.
  4. Authentication with a password or, much better, a key pair.
  5. Session. You get a shell.

Warning: If your client suddenly reports that the host key has changed, stop. It can mean the server was reinstalled, but it can also mean someone is intercepting the connection. Confirm the reason before you remove the old entry with ssh-keygen -R YOUR_SERVER_IP.

Step 1: Log in with an ed25519 key

A key pair consists of a private key that never leaves your computer and a public key that you place on the server. Create one on your own machine:

ssh-keygen -t ed25519 -C "you@laptop"

Choose a passphrase when asked. It protects the key if your laptop is ever stolen, and an SSH agent means you only type it once per session.

Copy the public key to the server and test it:

ssh-copy-id deploy@YOUR_SERVER_IP
ssh deploy@YOUR_SERVER_IP

On Windows, where ssh-copy-id is missing, paste the contents of id_ed25519.pub into ~/.ssh/authorized_keys on the server by hand. In both cases the permissions must be strict, or sshd ignores the key:

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

Do not move on until a key login works for a normal user with sudo rights.

Step 2: Harden sshd with a drop-in file

Ubuntu 24.04 ships OpenSSH 9.x, whose main /etc/ssh/sshd_config starts with:

Include /etc/ssh/sshd_config.d/*.conf

Put your changes in a file in that directory instead of editing the main file. Package updates then never overwrite them. Two rules matter: files are read in alphabetical order, and for most settings the first value sshd reads wins. Some cloud images ship a 50-cloud-init.conf that turns password login on, so give your file a name that sorts first:

sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowUsers deploy

AllowUsers is a whitelist: only the listed accounts can log in over SSH. Add every admin account, separated by spaces.

Step 3: Test before you reload

Check the syntax and look at the settings sshd will actually use:

sudo sshd -t
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|allowusers'

If sshd -T still shows passwordauthentication yes, another file in sshd_config.d is winning. Then reload the service (it is called ssh on Ubuntu and Debian, sshd on AlmaLinux and Rocky):

sudo systemctl reload ssh

Tip: Keep your current session open. Open a second terminal, log in again and run sudo -v. Only close the first session once the new one works.

Step 4: Slow down brute force with Fail2ban

Fail2ban reads the logs, spots repeated failed logins and blocks the source address in the firewall for a while. With password login switched off, attackers cannot succeed anyway, but Fail2ban keeps logs quieter and blocks noisy scanners.

sudo apt install fail2ban
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled  = true
backend  = systemd
maxretry = 5
findtime = 10m
bantime  = 1h
ignoreip = 127.0.0.1/8 ::1 203.0.113.10

Replace 203.0.113.10 with your own fixed IP so you can never ban yourself. Then:

sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip 198.51.100.7   # lift a ban

backend = systemd reads from the journal, which works on systems that have no /var/log/auth.log.

Step 5: Limit who can reach port 22

The safest SSH port is one the internet cannot reach. If you have a fixed IP, allow SSH only from there:

sudo ufw allow from 203.0.113.10 to any port 22 proto tcp

Without a fixed IP, rate-limit new connections instead with sudo ufw limit OpenSSH, or put SSH behind a VPN such as WireGuard. The firewall guide covers UFW and firewalld in detail.

Should you change the SSH port?

Moving SSH to, say, port 2222 cuts log noise from simple bots, but it is not real protection: a port scan finds it in seconds. It also means one more thing to remember in every client and script. If you do it, open the new port in the firewall first. On Ubuntu 24.04, sshd is started through socket activation, so after setting Port 2222 in your drop-in file you also need:

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

Optional: two-factor authentication

For servers with sensitive data, you can require both a key and a time-based code from an authenticator app. On Ubuntu that uses the libpam-google-authenticator PAM module:

  1. Install it with sudo apt install libpam-google-authenticator and run google-authenticator as the user to create the secret.
  2. Add auth required pam_google_authenticator.so to /etc/pam.d/sshd.
  3. In your drop-in file, set KbdInteractiveAuthentication yes and AuthenticationMethods publickey,keyboard-interactive.
  4. Reload and test in a second session before closing the first.

Hardware security keys (FIDO2, ssh-keygen -t ed25519-sk) are an even stronger option if your team has them.

If you lock yourself out

Even careful administrators get it wrong now and then: a typo in AllowUsers, a firewall rule on the wrong port, a key that was never copied. Do not panic and do not reinstall. Log in through out-of-band access instead, usually a web-based console in your provider's client area that works like a keyboard and screen attached to the server, independent of SSH. From there:

sudo sshd -T | grep -Ei 'allowusers|passwordauthentication|port'
sudo ufw status numbered
journalctl -u ssh -n 50

The journal usually says exactly why logins were refused, for example User deploy from 203.0.113.10 not allowed because not listed in AllowUsers. Fix the drop-in file or the firewall rule, reload and test from your own machine again. If there is no console available, contact your provider's support.

Keep it maintained

  • Install OpenSSH updates with the rest of the system; see how to update Ubuntu Server safely.
  • Review logins now and then with last and journalctl -u ssh --since today.
  • Remove keys from authorized_keys when someone leaves or a laptop is replaced.

In short

Use ed25519 keys with a passphrase, switch off password and root login in an early-sorting drop-in file, test with sshd -t and a second session before you close the first, add Fail2ban and restrict port 22 in the firewall. A non-standard port only reduces noise; 2FA adds real protection where it matters.

Secure SSH on a Hostigan server

A Hostigan VPS gives you full root access, so you configure sshd, Fail2ban and the firewall exactly as described here on Ubuntu, Debian, AlmaLinux or Rocky Linux. The same steps apply to a dedicated server.

Frequently asked questions

You can no longer log in with that key. Keep a second key on another device or in a password manager, and if all keys are lost, use your provider's web console to log in and add a new public key.

Next in “A secure server” · 5 of 7How to configure a firewall on your Linux VPS