Skip to content

SecurityFor beginnersDuration around 10 min.

How to create an SSH key and add it to your server

An SSH key allows you to sign in without a password, and no one can guess your password. This tutorial generates a key on your machine, adds it to the server, and finally disables password authentication.

Tested on with OpenSSH 9.6 on Ubuntu 24.04 LTS and OpenSSH 10.2 on Ubuntu 26.04 LTS

At a glance

  • A key pair consists of a private file that never leaves your machine and a public one that is placed on the server.
  • ssh-copy-id appends the public key to the authorized_keys file on the server and sets the permissions.
  • Only when signing in with the key works should you disable password authentication — in a dedicated file under sshd_config.d.

Prerequisites

  • A Linux server you can sign in to via SSH with a password — such as a VPS or a Cloud Server running Ubuntu 24.04 or 26.04.
  • The OpenSSH client on your machine. It is pre-installed on Linux and macOS; on Windows 10 and 11, ssh and ssh-keygen are available in PowerShell.

In the examples, the user on the server is user and the server is 203.0.113.10. You perform steps 1 to 4 on your machine, and step 5 on the server.

1. Generate a key

Ed25519 is short, fast, and considered secure. The comment after -C helps you identify the key in a list later. ssh-keygen asks for the save location — press Enter to accept the default — and for a passphrase, which provides additional protection for the private key.

Generate a key pair
ssh-keygen -t ed25519 -C "anna@laptop"

The ~/.ssh folder will then contain two files. You only share the one ending in .pub; the other must only be readable by your user.

The two files
-rw------- 1 anna anna 399 id_ed25519
-rw-r--r-- 1 anna anna  93 id_ed25519.pub

2. Copy the public key to the server

ssh-copy-id signs in with your password one last time and appends the public key to ~/.ssh/authorized_keys for the user on the server.

Add key
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@203.0.113.10

3. Test signing in with the key

Sign in now. If SSH no longer asks for the server password — at most for your passphrase — the key is active.

Sign in
ssh user@203.0.113.10

4. Without ssh-copy-id, e.g. on Windows

ssh-copy-id is not available on Windows. The same step can be achieved with a single line that appends the contents of the .pub file and sets the permissions. On Windows, you read the file in PowerShell using type instead of cat.

Append the key manually
cat ~/.ssh/id_ed25519.pub | ssh user@203.0.113.10 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Permissions are important: if ~/.ssh is writable by others, the SSH service ignores the file, and you will continue to be asked for a password.

5. Disable password authentication

Leave your current session open until you have tested the final step. Create a dedicated file under /etc/ssh/sshd_config.d on the server. Its name starts with 00, because the SSH service applies the first setting it finds — many cloud images include a file that re-enables passwords.

Create file
sudo nano /etc/ssh/sshd_config.d/00-nur-schluessel.conf
/etc/ssh/sshd_config.d/00-nur-schluessel.conf
PasswordAuthentication no
KbdInteractiveAuthentication no

Check the settings and restart the service:

Check and restart
sudo sshd -t
sudo sshd -T | grep -E 'passwordauthentication|kbdinteractiveauthentication'
sudo systemctl restart ssh
This is what the check should look like
passwordauthentication no
kbdinteractiveauthentication no

Then open a second terminal and sign in with the key. An attempt without a key will now end with this message:

Sign in without a key
user@203.0.113.10: Permission denied (publickey).

6. Short name in the SSH configuration

Instead of entering the address, user and key every time, add the server to ~/.ssh/config on your computer. After that, ssh web is enough, and scp and rsync will also understand the short name.

~/.ssh/config
Host web
    HostName 203.0.113.10
    User user
    IdentityFile ~/.ssh/id_ed25519

One key per machine

Generate a separate key for each machine you sign in from. If a laptop is lost, you only delete its line from authorized_keys.

Typical errors

  • UNPROTECTED PRIVATE KEY FILE: The private key is readable by others, so SSH rejects it. Reset the permissions:
Permissions of the private key
chmod 600 ~/.ssh/id_ed25519
The message
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
Permissions 0644 for '/home/anna/.ssh/id_ed25519' are too open.
  • Still asking for a password: Usually, the permissions of ~/.ssh (700) or authorized_keys (600) on the server are incorrect, or the key belongs to the wrong user.
  • Permission denied (publickey) after step 5: The server no longer accepts passwords and does not know your key. Sign in via the still open session — for a VPS also via the KVM console in the customer area — and add the key.
  • Old server without Ed25519: Very old remote hosts do not know the type. In this case, generate an RSA key with 4096 bits; the commands below also show how to set a passphrase later and display the fingerprint.
RSA key, passphrase, fingerprint
ssh-keygen -t rsa -b 4096 -C "anna@laptop"
ssh-keygen -p -f ~/.ssh/id_ed25519
ssh-keygen -lf ~/.ssh/id_ed25519.pub

We tested all commands in this guide on 2 December 2026 — with OpenSSH 9.6 on Ubuntu 24.04 LTS and OpenSSH 10.2 on Ubuntu 26.04 LTS. If a step changes at the manufacturer, we update it here along with the date.

Official documentation

Frequently asked questions about SSH keys

Which file goes on the server?

Only the public one with the .pub extension. The private key without an extension stays on your computer and is never copied or sent.

Do I need a passphrase?

It is not mandatory, but it protects the key in case someone copies the file. So you do not have to enter it at every sign-in, the ssh-agent remembers it for the duration of your session.

Ed25519 or RSA?

Ed25519 is the first choice: short, fast and secure. You only need RSA with 4096 bits for very old remote hosts that do not know Ed25519 yet.

Can I use multiple keys for one server?

Yes. Each line in authorized_keys is an allowed key. It is best if every computer and every person has their own, so a single access can be removed specifically.

Is root allowed to sign in with a password?

Not on Ubuntu: the default of the SSH service only allows root to sign in with a key. For daily work, a separate user with sudo privileges is still better.

What do I do if the private key is lost?

Generate a new key and add it via another access, for a VPS such as the KVM console in the customer area. Delete the old public key from authorized_keys.

Further reading

More tutorials

  • How to set up fail2ban: protect SSH on Ubuntu against password guessing

    SecurityFor beginnersabout 10 min

    fail2ban monitors the login attempts on your server and bans addresses that try an incorrect password too often. Following this guide, it protects SSH access with custom values for ban duration and failed attempts, and you will know how to check and lift a ban.

  • How to install Docker on Ubuntu: Docker Engine and Compose

    ContainerFor beginnersabout 10 min

    By the end of this tutorial, Docker Engine from the official Docker repository will be running on your Ubuntu server, along with the Compose plugin and a first service. You need a server with Ubuntu 24.04 LTS or 26.04 LTS and a user with sudo privileges.

  • How to set up Nginx as a reverse proxy with HTTPS (Let’s Encrypt)

    Web serverFor beginnersabout 20 min

    Following this tutorial, Nginx will accept requests to your domain, pass them to an application on the same server, and serve everything over HTTPS with a Let’s Encrypt certificate. This is suitable for Node.js, Python, Go, or a container listening on a local port.

A server for your key

Every VPS and Cloud Server is accessible via SSH — you can add your key as described in this guide.