How to Secure SSH Access on Linux (Ubuntu)

Every publicly reachable Linux server is under constant brute-force pressure. If you leave SSH open with password authentication and the root account enabled, it is not a question of if an attacker gets in, only of when. This guide walks you through four concrete steps that eliminate the most common attack surface:

  1. Create an SSH key pair on your local machine
  2. Create a dedicated sudo user on the server
  3. Deploy your public key to the server
  4. Harden sshd: disable password login and block root login

By the end you will log in exclusively with your private key, through a non-root account that can escalate privileges with sudo.

Before you start, take a snapshot. If your server runs on a cloud provider (Hetzner, DigitalOcean, AWS, etc.), create a snapshot or backup of the server right now. Most providers offer this in their web console with one click. A snapshot lets you restore the exact state of the machine in minutes if something goes wrong, for example if you accidentally lock yourself out before key-based login is verified. The cost is typically a few cents and the peace of mind is worth it.


Prerequisites

  • A Linux server reachable via SSH, in our case Ubuntu 22.04, 24.04 or 26.04. The steps are the same on all three; the one difference (since 24.04, ssh is started through systemd socket activation via ssh.socket) is pointed out where it matters. Ubuntu 20.04 has left standard support, so upgrade it first.
  • Your current ability to log in as root or a user with sudo privileges (you need this to apply the config changes)
  • OpenSSH client on your local machine. It ships with every modern Linux, macOS, and Windows 10+ installation

Warning: Do not close your existing SSH session until you have verified that key-based login works. If you lock yourself out of a cloud VM, you will need the provider’s recovery console.


Step 1: Create an SSH Key Pair on Your Local Machine

An SSH key pair consists of two mathematically linked files:

  • Private key (id_ed25519): stays on your machine and never leaves it
  • Public key (id_ed25519.pub): placed on every server you want to access

The current recommendation is Ed25519, an elliptic-curve algorithm defined in RFC 8032. It produces shorter keys than RSA while offering equivalent or better security, and it is supported by all modern OpenSSH versions (≥ 6.5).

Linux / macOS

ssh-keygen -t ed25519 -C "your-email@example.com"

Windows (PowerShell)

ssh-keygen -t ed25519 -C "your-email@example.com"

ssh-keygen is part of the OpenSSH client that ships with Windows 10 (version 1809) and later. If you are on an older version, install it via Optional Features or use PuTTYgen.

The tool will ask two things:

Enter file in which to save the key (/home/you/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
  • File location: press Enter to accept the default (~/.ssh/id_ed25519 on Linux/macOS, %USERPROFILE%\.ssh\id_ed25519 on Windows).
  • Passphrase: enter a strong passphrase. This encrypts your private key on disk; if your laptop is stolen, the attacker still cannot use the key without the passphrase. An SSH agent (see below) means you only have to type it once per session.

After completion you will have two files:

~/.ssh/id_ed25519      # private key — permissions must be 600
~/.ssh/id_ed25519.pub  # public key  — safe to share

A note on Post-Quantum Cryptography (PQC)

Ed25519 is the right choice today, but it is worth understanding the longer-term picture.

Both RSA and elliptic-curve algorithms (including Ed25519) rely on mathematical problems (integer factorization and the discrete logarithm problem) that a sufficiently powerful quantum computer could solve in polynomial time using Shor’s algorithm. No such computer exists yet, but the threat is real enough that NIST finalized three post-quantum cryptography standards in August 2024:

  • FIPS 203: ML-KEM (formerly CRYSTALS-Kyber): a key-encapsulation mechanism for key exchange
  • FIPS 204: ML-DSA (formerly CRYSTALS-Dilithium): a lattice-based digital signature scheme
  • FIPS 205: SLH-DSA (formerly SPHINCS+): a hash-based digital signature scheme

What OpenSSH already does. OpenSSH protects the key exchange (KEX) with hybrid algorithms that combine a post-quantum KEM with classical X25519, so the session stays secure as long as either half holds:

  • OpenSSH 9.0 (April 2022) made sntrup761x25519-sha512@openssh.com the default: Streamlined NTRU Prime 761 + X25519. It is now standardized as sntrup761x25519-sha512 in RFC 9941.
  • OpenSSH 9.9 (September 2024) added mlkem768x25519-sha256: ML-KEM-768 from FIPS 203 + X25519. OpenSSH 10.0 (April 2025) made it the default. It is specified in RFC 10042 (formerly draft-ietf-sshm-mlkem-hybrid-kex).

Which one you get depends on the OpenSSH versions on both ends: client and server pick the first algorithm they both support. Either way, this protects the session encryption against the so-called “harvest now, decrypt later” attack: an adversary recording your encrypted traffic today cannot decrypt it retroactively once a quantum computer becomes available.

You can verify which KEX your server negotiates:

Linux / macOS

ssh -v deploy@your-server-ip exit 2>&1 | grep "kex: algorithm"

Windows (PowerShell)

ssh -v deploy@your-server-ip exit 2>&1 | Select-String "kex: algorithm"

What is still in progress. The hybrid KEX protects session confidentiality, but authentication keys (your id_ed25519) are not post-quantum in OpenSSH yet, and there is no standardized ML-DSA or SLH-DSA key type for SSH. Work on SSH protocol extensions happens in the IETF SSHM working group. This is less urgent than the KEX: a recorded login cannot be forged after the fact, so authentication is only at risk once a quantum computer actually exists. Until post-quantum key types land in a stable OpenSSH release and distribution packages, Ed25519 remains the correct choice.


Using ssh-agent to avoid re-typing the passphrase

ssh-agent holds your decrypted key in memory for the duration of your session.

Linux / macOS

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Windows (PowerShell)

Run once to enable the service:

Administrator rights required. The first two commands modify a Windows service and must run in an elevated PowerShell session. Right-click the Start menu → Terminal (Admin) or Windows PowerShell (Admin), or search for “PowerShell”, right-click the result, and select Run as administrator. The third command (ssh-add) can then be run in any regular terminal.

Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent
ssh-add "$env:USERPROFILE\.ssh\id_ed25519"

Step 2: Create a Dedicated Sudo User on the Server

Never work as root day-to-day. Create a regular user and grant it sudo access. This limits blast radius: a compromised session still needs the user’s password (or a privilege escalation exploit) to become root.

Connect to your server as root (your last root login for a while):

Linux / macOS

ssh root@your-server-ip

Windows (PowerShell)

ssh root@your-server-ip

Create the user

Replace deploy with whatever username you prefer.

adduser deploy

adduser is Ubuntu’s interactive frontend to useradd. It will prompt for a password and some optional profile fields. Choose a strong password, because you will need it when running sudo commands.

Grant sudo privileges

usermod -aG sudo deploy

This adds deploy to the sudo group. Ubuntu’s default /etc/sudoers contains the rule %sudo ALL=(ALL:ALL) ALL, which grants all members of that group the ability to run any command as root by prefixing it with sudo.

Verify

su - deploy
sudo whoami

sudo whoami should print root. If it does, the user is correctly configured.


Step 3: Deploy Your Public Key to the Server

The server authenticates you by checking whether the public key stored in ~/.ssh/authorized_keys matches the private key you present during login. This mechanism is defined in RFC 4252 §7.

ssh-copy-id automates the process: it appends your public key to the remote authorized_keys file and sets the correct permissions.

ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@your-server-ip

Option B: Manual copy (works everywhere, including Windows)

Linux / macOS

cat ~/.ssh/id_ed25519.pub | ssh deploy@your-server-ip \
  "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Windows (PowerShell)

$pubKey = Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub"
ssh deploy@your-server-ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '$pubKey' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Verify key-based login before proceeding

Open a new terminal (keep the old session open) and test:

Linux / macOS

ssh -i ~/.ssh/id_ed25519 deploy@your-server-ip

Windows (PowerShell)

ssh -i "$env:USERPROFILE\.ssh\id_ed25519" deploy@your-server-ip

You should be logged in. If you set a passphrase, you will be prompted for it. If the login fails, do not proceed to Step 4. Troubleshoot first (check file permissions, key placement, and ~/.ssh/authorized_keys content on the server).


Step 4: Harden sshd Configuration

The main SSH daemon config is /etc/ssh/sshd_config, but on current Ubuntu releases you should not put your changes there. Two rules from the sshd_config man page explain why:

  1. The very first line of Ubuntu’s sshd_config is Include /etc/ssh/sshd_config.d/*.conf, so every drop-in file in that directory is read before the rest of the main file, in lexical order of the file names.
  2. For most keywords, “the first obtained value will be used”. Later lines for the same keyword are silently ignored.

On many cloud images (AWS, Azure, Hetzner, DigitalOcean …), cloud-init writes /etc/ssh/sshd_config.d/50-cloud-init.conf containing PasswordAuthentication yes. If you only edit PasswordAuthentication no into the main file, that drop-in wins and password login stays on, even though the config file looks correct. Check whether your server has such a file:

sudo grep -ri passwordauthentication /etc/ssh/sshd_config.d/

The reliable fix is a drop-in of your own whose name sorts first, so its values are read before everything else. Connect to the server as deploy and create it:

sudo nano /etc/ssh/sshd_config.d/00-hardening.conf

We will put three directives into this file.

4.1 Disable password authentication

PasswordAuthentication no

This forces every client to authenticate with a key. Brute-force attacks against passwords become impossible because the server will not accept them at all.

4.2 Disable root login

PermitRootLogin no

Even if an attacker obtains valid credentials, they cannot log in as root directly. All privileged operations must go through sudo from a named account, which creates an audit trail.

Add a line that whitelists only your new user:

AllowUsers deploy

Any other account, including one an attacker might create, is denied SSH access regardless of whether it has a valid key.

The complete 00-hardening.conf now reads:

PasswordAuthentication no
PermitRootLogin no
AllowUsers deploy

Apply the changes

Save the file and let sshd check the whole configuration for syntax errors before you reload. A broken config is the classic way to lock yourself out:

sudo sshd -t

No output means the configuration is valid. Then reload ssh without dropping existing connections:

sudo systemctl reload ssh

reload sends SIGHUP to the running daemon, which re-reads the config file without terminating active sessions. Use sudo systemctl restart ssh only if reload does not work. On Ubuntu the unit is called ssh, not sshd.

Ubuntu 24.04 and later: SSH is socket-activated: ssh.socket listens on port 22 and starts ssh.service on demand. For the directives in this post, reload ssh is all you need. Only if you change Port or ListenAddress do you also have to run sudo systemctl daemon-reload && sudo systemctl restart ssh.socket, because the socket unit is generated from those values. See Ubuntu’s announcement of socket-based activation.

Now ask sshd which values it actually uses. sshd -T prints the effective configuration after all Include files have been merged. That is the only reliable way to rule out an overriding drop-in:

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

Expected output:

permitrootlogin no
passwordauthentication no
allowusers deploy

Verify

From a new terminal on your local machine, confirm that key-based login still works:

Linux / macOS

ssh deploy@your-server-ip

Windows (PowerShell)

ssh deploy@your-server-ip

Then confirm that password authentication is rejected:

Linux / macOS

ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no deploy@your-server-ip

Windows (PowerShell)

ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no deploy@your-server-ip

You should see Permission denied (publickey), which is the correct response.

Also confirm that root login is blocked:

Linux / macOS

ssh root@your-server-ip

Windows (PowerShell)

ssh root@your-server-ip

Expected result: Permission denied (publickey). Root login is gone.


Summary of Changes

WhatBeforeAfter
Authentication methodPassword or keyKey only
Root loginAllowedBlocked
Default working userrootdeploy (sudo-enabled)
Privilege escalationDirect root sessionsudo with audit trail

Troubleshooting

“Permission denied (publickey)” when I expect to be let in

  1. Check that the public key in ~/.ssh/authorized_keys on the server matches ~/.ssh/id_ed25519.pub on your machine exactly (one line, no line breaks).

  2. Check permissions:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    
  3. Run ssh with verbose output to see exactly where authentication fails:

    ssh -vvv deploy@your-server-ip
    

“Could not load host key” in sshd logs

Run sudo ssh-keygen -A on the server to regenerate missing host keys, then sudo systemctl restart ssh.

I locked myself out

If you are on a cloud provider (AWS, Hetzner, DigitalOcean, etc.), use the provider’s web console or recovery/rescue mode to mount the server’s disk and fix or remove /etc/ssh/sshd_config.d/00-hardening.conf directly. This is why you must verify key-based login before disabling password auth.


What’s Next?

With SSH hardened, consider these complementary measures:

  • Fail2ban: bans IPs that produce too many failed login attempts, reducing log noise even after password auth is off
  • UFW (Uncomplicated Firewall): restrict incoming traffic to only the ports you actually need (sudo ufw allow OpenSSH && sudo ufw enable)
  • Two-factor authentication for sudo: pair sudo with a TOTP token via libpam-google-authenticator as a second factor
  • Regular unattended upgrades: sudo apt install unattended-upgrades keeps security patches applied automatically
  • Post-quantum authentication keys: once a post-quantum SSH key type is standardized (watch the IETF SSHM working group) and OpenSSH ships stable support, generate a second key pair using an ML-DSA or SLH-DSA algorithm (FIPS 204 / FIPS 205) and add it alongside your Ed25519 key to authorized_keys for a hybrid authentication setup

UFW, Fail2ban, and unattended upgrades are covered step by step in Ubuntu Server Hardening — Part 1: The Essentials. Each of these topics deserves its own post, so stay tuned.