Create an SSH key and add it to a server

An SSH key replaces your login password with a key pair. Here is how to create it, install it and protect it.

How it works: a public key and a private key

An SSH key is a pair of mathematically linked keys. The public key goes on the servers you want to access, in your account's ~/.ssh/authorized_keys file. The private key stays on your computer.

When you connect, the server asks your SSH client to sign a message with the private key, and checks the signature with the public key it knows. The private key never leaves your computer during the exchange, and the signature cannot be used to rebuild it. Nobody can guess your key the way they might guess a password, and you can use the same public key on as many servers as you need.

There is nothing secret about the public key: it can be sent by email or posted in a ticket. All the security rests on the private key.

Ed25519 or RSA?

Two key types dominate today:

  • Ed25519: elliptic-curve signatures, short keys (68 characters for the encoded part of the public key), fast, with no parameters to tune. Supported by OpenSSH since version 6.5 (2014), it became the default type for ssh-keygen with OpenSSH 9.5. It is the right choice in almost every case.
  • RSA: the legacy type, useful for older devices or software that do not know Ed25519. In that case, choose 4096 bits (3072 at the very least).

An Ed25519 public key looks like this, the last field being a simple comment that helps you recognize the key:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI… alice@laptop

Create the key with ssh-keygen

On Linux and macOS, ssh-keygen comes with OpenSSH. Windows 10 and 11 also include the OpenSSH client: the same commands work in PowerShell.

# Ed25519 key (recommended)
ssh-keygen -t ed25519 -C "alice@laptop"

# 4096-bit RSA key, for older systems
ssh-keygen -t rsa -b 4096 -C "alice@laptop"

The command suggests a location (by default ~/.ssh/id_ed25519, or C:\Users\alice\.ssh\id_ed25519 on Windows), then asks for a passphrase. It creates two files: id_ed25519, the private key, and id_ed25519.pub, the public key. The -C comment is free text; naming the person and the device helps you sort keys out later.

Add it to the server

The simplest method is ssh-copy-id, which logs in one last time with your password and adds the public key in the right place, with the right permissions:

ssh-copy-id -i ~/.ssh/id_ed25519.pub alice@server.example.com

On Windows, where ssh-copy-id is not included, the same operation looks like this in PowerShell:

Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | ssh alice@server.example.com "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

You can also paste the public key line manually into ~/.ssh/authorized_keys, one key per line. The SSH server checks permissions: the ~/.ssh folder must be 700 and the file 600, otherwise the key is ignored. Then test it:

ssh -i ~/.ssh/id_ed25519 alice@server.example.com

To stop typing the full address, declare the server in ~/.ssh/config, then connect with ssh prod:

Host prod
    HostName server.example.com
    User alice
    IdentityFile ~/.ssh/id_ed25519

Once keys are in place for every user, you can disable password login with PasswordAuthentication no in /etc/ssh/sshd_config, then reload the service (ssh on Debian and Ubuntu, sshd on Red Hat and derivatives). Keep a session open while you test: if something goes wrong, you can fix it without locking yourself out.

Protect the private key

  • A passphrase encrypts the private key file: copied by mistake or stolen along with a computer, it remains unusable. You can add or change it at any time with ssh-keygen -p -f ~/.ssh/id_ed25519.
  • The SSH agent keeps the decrypted key for the length of your session: you type the passphrase once, with ssh-add ~/.ssh/id_ed25519, and the following connections are instant.
  • Strict permissions: chmod 600 ~/.ssh/id_ed25519. The SSH client refuses a private key that other users can read.
  • One key per device: if a computer is lost, you just remove its line from the authorized_keys files, and the other devices keep working.

Set up access for a team member

The ideal setup is for everyone to generate their own key on their own computer and send only the public key to the administrator: the private key has then never existed anywhere but with its owner.

Sometimes, though, an administrator sets up access from start to finish: a team member unfamiliar with the command line, a contractor who needs to be equipped quickly, emergency access. The kit's SSH key generator then creates the pair in the browser, in OpenSSH format; the administrator installs the public key on the server, then hands the private key to its recipient.

That is where “Send with onlyU” comes in: the key goes from the kit to onlyU tab to tab in your browser, without appearing in an address or going through the kit's server. onlyU encrypts it in your browser and creates a link the team member opens only once, valid for 24 hours, after which the link disappears. It is free, with no sign-up. The team member saves the key in ~/.ssh/, applies chmod 600, and adds their own passphrase with ssh-keygen -p; the administrator deletes their copy.

For full network access rather than access to a single server, see the guide on how to configure a WireGuard peer, which follows the same key-pair logic.