# SSH keys

In SSH, and so in SFTP, keys do two different jobs. A **host key** proves to a client that the server is the real one. A **user key** proves to the server who the user is. This page is about how the two fit together; the settings are on the pages it points to.

## The two kinds

| | Host key | User key |
|---|---|---|
| whose | the server's | a person's or a program's |
| private half kept by | the server, in its folder | the user, on their own machine |
| public half known to | every client, after the first connection | the server, in the user's account |
| set in | [SSH host keys, ciphers and key exchange](Ports-SFTP-Keys-and-Ciphers.md) | [SSH public keys of a user](Users-Restrictions-SSH-Keys.md) |
| if it is lost | every client warns: restore it from a backup | make a new pair and give the server the new public half |
| if it is stolen | somebody can pose as the server: replace it and tell every partner | somebody can log in as the user: take the public half out of the account |

In both cases only the **public** half ever travels. A private key is never mailed, never uploaded, never given to an administrator.

## Why keys for users

- Nothing that could be guessed: a key is far beyond any password.
- Nothing secret reaches the server, so nothing can be stolen from it.
- A program can use one without a person typing.
- Each device can have its own, and one can be withdrawn without touching the others.

## What a login can demand

A user with keys in their account can be held to more than the key. The server decides at each login which steps are still owed:

| The user's settings | The login needs |
|---|---|
| keys listed, nothing else | the key, **or** the password |
| **Require public key and password** | the key, then the password |
| **Require public key and keyboard interactive** | the key, then an answer at a prompt |
| a second factor set up, and nothing else | the password or the key, and then the one-time code |
| key and password required, and a second factor | all three: key, password, code |

The listener can add to every row: **Require Public Key Authentication** and **Require Password Authentication** on an [SFTP port](Ports-SFTP.md) hold for everybody who comes in there.

A key alone never completes the login of an account that owes a one-time code. And an account that must show a key first gets a plain refusal for any password until the key was seen: it gives nothing away, and sends no code.

To allow **keys only** for one account, list its keys and give it a long random password nobody knows.

## One-time codes over SFTP

SFTP has no screen for a code. The server asks for it with "keyboard interactive", a prompt most SFTP programs show. On a port that requires the password method, where there is no such prompt, the code is typed with the password, in the form the server expects there.

Programs without a person cannot answer either. Give them an account of their own with a key and an address restriction, without the second factor.

## Kinds of key

| Kind | For user keys | For host keys |
|---|---|---|
| ED25519 | the first choice: short, fast, strong | on, on a new server |
| ECDSA | fine | on |
| RSA, 3072 or 4096 bits | fine, and what very old clients know | on |
| RSA with SHA-1 signatures (`ssh-rsa`) | refused unless **RSA Sha1 Signatures Supported** is on for the port | |
| DSA | do not use | off |

## Good practice

- A key per person and per device. No shared keys.
- A passphrase on every key a person uses.
- Keys of programs: readable only by the program's account, on an account that can do only what the program needs, from an address you name.
- Review the keys of each account once a year, and when somebody leaves.
- Back up the server's host keys with its settings, and keep them when you move the server.
