NewCode CLOUD — 20% off your first month on all cloud VPSClaim

How to Set Up SSH Key Authentication and Disable Password Login

A new Linux server that accepts password logins on port 22 is scanned within minutes of going online. Bots try thousands of common passwords per hour, and even a strong password can leak through a reused account or a phishing page. SSH keys replace the password with a cryptographic key pair: the private half never leaves your computer, and the server only stores the public half. Brute-forcing a modern key is not practical.

This guide covers generating a key on any operating system, installing it on the server, switching off password authentication without locking yourself out, and day-to-day key management.

How SSH keys work

An SSH key pair has two files:

File Where it lives Can you share it?
Private key (e.g. id_ed25519) Only on your own computer Never
Public key (e.g. id_ed25519.pub) In ~/.ssh/authorized_keys on each server Yes — it is safe to publish

When you connect, the server sends a challenge that can only be answered with the private key. Your key never crosses the network, so even a compromised server cannot steal it.

Step 1: Generate a key pair

Use Ed25519. It is fast, short and supported by every OpenSSH release from the last decade. Use RSA (4096-bit) only if you must connect to very old systems.

macOS, Linux and Windows 10/11 (OpenSSH)

Open Terminal (or PowerShell on Windows) and run:

ssh-keygen -t ed25519 -C "you@laptop-2026"
  • Press Enter to accept the default location (~/.ssh/id_ed25519).
  • Set a passphrase. It encrypts the private key on disk, so a stolen laptop does not mean a stolen server. An SSH agent remembers it, so you only type it once per session.

The comment after -C is just a label. Use something that tells you later which device the key belongs to.

Windows with PuTTY

If you use PuTTY, open PuTTYgen, choose EdDSA (Ed25519), click Generate, set a passphrase and save the private key as a .ppk file. Copy the text in the box labelled “Public key for pasting into OpenSSH authorized_keys file” — that is what goes on the server.

Step 2: Copy the public key to the server

Log in with your password one last time and install the key.

The easy way: ssh-copy-id

On macOS and Linux:

ssh-copy-id -i ~/.ssh/id_ed25519.pub root@YOUR_SERVER_IP

It appends the key to ~/.ssh/authorized_keys and fixes permissions.

The manual way (Windows or any system)

Print your public key and copy the whole line:

cat ~/.ssh/id_ed25519.pub
# PowerShell: Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub

Then on the server:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys     # paste the key on its own line, save
chmod 600 ~/.ssh/authorized_keys

Each key must be on a single line. A line break in the middle — common when copying from a terminal window — makes the key invalid.

Step 3: Test key login before changing anything

Open a new terminal window and connect:

ssh -i ~/.ssh/id_ed25519 root@YOUR_SERVER_IP

You should be asked for the key’s passphrase (if you set one), not the server password. Keep your existing session open until you have confirmed this works. That open session is your safety net if a later step goes wrong.

To see exactly which authentication method was used, add -v:

ssh -v root@YOUR_SERVER_IP 2>&1 | grep -i "authenticated"
# Authenticated to YOUR_SERVER_IP using "publickey".

Step 4: Disable password authentication

Only do this after key login works. Modern distributions read drop-in files from /etc/ssh/sshd_config.d/, which survive package upgrades better than editing the main file:

sudo tee /etc/ssh/sshd_config.d/10-keys-only.conf >/dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitRootLogin prohibit-password
EOF

PermitRootLogin prohibit-password still allows root to log in with a key, but never with a password. If you have created a separate sudo user (see creating a sudo user), you can set it to no instead.

Check the configuration and reload:

sudo sshd -t && sudo systemctl reload ssh     # Ubuntu / Debian
sudo sshd -t && sudo systemctl reload sshd    # AlmaLinux / Rocky / CentOS

sshd -t validates the syntax first. If it prints an error, fix it before reloading — a broken config can stop SSH from accepting new connections.

Watch out for cloud-init overrides

Some images ship a file such as /etc/ssh/sshd_config.d/50-cloud-init.conf containing PasswordAuthentication yes. OpenSSH uses the first value it reads, and files are read in alphabetical order — that is why the file above starts with 10-. Confirm the effective setting:

sudo sshd -T | grep -E "passwordauthentication|permitrootlogin"

Finally, from your computer, confirm that passwords are now refused:

ssh -o PubkeyAuthentication=no root@YOUR_SERVER_IP
# Permission denied (publickey).

Make daily use convenient

Use an SSH config file

Instead of typing the IP, user and key every time, create ~/.ssh/config on your computer:

Host hk1
    HostName 203.0.113.10
    User deploy
    IdentityFile ~/.ssh/id_ed25519
    ServerAliveInterval 30

Now ssh hk1 is enough. ServerAliveInterval sends a keep-alive every 30 seconds, which stops idle sessions being dropped by home routers and long international paths.

Use an SSH agent

The agent holds your decrypted key in memory:

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

macOS can store the passphrase in the Keychain with ssh-add --apple-use-keychain. On Windows, enable the OpenSSH Authentication Agent service and run ssh-add.

Managing keys for a team

  • One key per person per device. Never share a private key between people. When someone leaves, you remove their line from authorized_keys and nobody else is affected.
  • Label every key with the -C comment so authorized_keys stays readable.
  • Give each person their own Linux user rather than putting everyone’s key on root.
  • Restrict automation keys. A key used by a backup script can be limited to one command and one source IP by prefixing the line in authorized_keys:
from="198.51.100.7",command="/usr/local/bin/backup.sh",no-pty,no-port-forwarding ssh-ed25519 AAAA... backup@nas

Troubleshooting

Symptom Likely cause Fix
Still asked for the server password Key not offered, or wrong file ssh -v and look for “Offering public key”; check IdentityFile
Permission denied (publickey) Permissions too open, or key on two lines chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys; re-paste the key on one line
Works for root, not for a new user Key in the wrong home directory or wrong owner chown -R user:user /home/user/.ssh
Load key: invalid format (PuTTY users) Using a .ppk file with OpenSSH Export an OpenSSH key from PuTTYgen (Conversions menu)
Locked out after disabling passwords Key did not work and the test session was closed Use the VNC console in the control panel and revert the config file

The server log shows why a key was rejected:

sudo journalctl -u ssh -n 50      # Ubuntu / Debian (unit "sshd" on RHEL family)

If you lose your private key

There is no way to recover a lost private key. Log in with another authorised key, or open the VNC console from the control panel, temporarily re-enable password authentication, add a new public key and disable passwords again. This is why it is worth having two keys authorised — for example one on your laptop and one on a hardware token or a second machine.

Next steps

Key authentication is the single most effective SSH security measure, but it is one layer. Continue with: