Sub topic 3
Choosing a Key Type
This chapter assumes you can log in to a Linux server with ssh and a password, and that you know what a public key and a private key are at a high level. You do not need to have generated a key yet. The next chapter covers the ssh-keygen workflow. Here you decide which algorithm that key should use, and why.
What the key type decides
A key type is the mathematical algorithm behind your key pair. It determines four things you will notice in practice:
- How long the public key is, which affects how readable your
authorized_keysfile is. - How fast signing and verification are.
- Which SSH servers and clients can use the key.
- How much security margin the key gives against known attacks.
The key type does not change the login workflow. The client signs a challenge with the private key, and the server checks the signature against the public key. All the types below work through that same exchange.
The types OpenSSH supports
OpenSSH accepts several user key types. In practice, three matter today.
| Type | `ssh-keygen -t` value | Typical size | Notes |
|---|---|---|---|
| Ed25519 | `ed25519` | Fixed, 256 bits | Short keys, fast, modern default. Needs OpenSSH 6.5 (2014) or newer. |
| RSA | `rsa` | 3072 or 4096 bits recommended | Works almost everywhere, including very old servers and some appliances. |
| ECDSA | `ecdsa` | 256, 384 or 521 bits | Older elliptic curve option. Rarely a good first choice. |
A fourth type, DSA, is obsolete. OpenSSH disabled it by default in version 7.0 (2015), so do not create new DSA keys.
Hardware-backed keys use the types ed25519-sk and ecdsa-sk. They need a FIDO2 security key and OpenSSH 8.2 or newer. They are outside the scope of this chapter, but they follow the same decision rules described below.
Ed25519
Ed25519 is an elliptic curve signature scheme. Its public key is only 68 characters long when encoded in the authorized_keys format, so it fits on one line without wrapping. It was designed to avoid several implementation pitfalls of older schemes. For example, signing does not depend on a good random number for each signature, which removes a class of failures that has leaked keys in other systems.
For these reasons, Ed25519 is the right default whenever both ends support it. The server you log in to must run OpenSSH 6.5 or newer, and most distributions have shipped that for years.
RSA
RSA is the oldest of the three and the most widely supported. Its security depends on key length, so you must choose a size:
- Do not use fewer than 2048 bits. Keys below that are no longer considered safe.
- Use 3072 bits as a reasonable minimum for new keys. Use 4096 bits if you want extra margin and can accept slightly slower operations.
- Longer RSA keys make public keys and signatures bigger. A 4096-bit public key is about 725 characters.
RSA also has a signature-algorithm detail that causes real trouble. Older RSA keys were signed with SHA-1, using the algorithm name ssh-rsa. OpenSSH 8.8 (2021) disabled that SHA-1 signature by default. The key itself is still fine. The client and server must instead agree on rsa-sha2-256 or rsa-sha2-512 signatures. A modern client talking to a modern server negotiates this automatically. You only run into the problem when one side is very old, as the failure mode section later in this chapter shows.
ECDSA
ECDSA also uses elliptic curves, but it is older than Ed25519 and harder to implement safely. Like all DSA-family schemes, it needs a fresh random value for each signature. Many people also distrust the NIST curves it uses, though no practical attack on them is known. ECDSA is supported by servers that predate Ed25519 support in some cases, but RSA is usually the better compatibility choice. Pick ECDSA only if a policy or a device requires it.
Comparing the options
The decision rests on compatibility first. Security differences between a good Ed25519 key and a 3072-bit or longer RSA key are small for most users. Operational differences are larger: Ed25519 keys are easier to copy, paste, and compare by eye.
Checking what your server supports
You rarely need to guess. Check the server version from the client:
ssh -V
ssh -v [email protected] 2>&1 | grep -i "remote software version"The first command prints the version of your local client. The second prints a line such as Remote protocol version 2.0, remote software version OpenSSH_9.6. If the number is 6.5 or higher, Ed25519 works.
You can also ask your own client which key types it knows about:
ssh -Q keyThis lists every public key algorithm the local client supports. If ssh-ed25519 appears, your client can use it. The server must support it as well, but that is almost always the case on current systems.
To see what keys you already have, list the ~/.ssh directory and look at the fingerprints:
ls ~/.ssh/id_*.pub
ssh-keygen -l -f ~/.ssh/id_ed25519.pubThe second command prints the key size, the fingerprint, the comment, and the type in parentheses, for example 256 SHA256:... alice@laptop (ED25519).
Common failure modes
Most key-type problems show up as a login that falls back to a password prompt or ends with Permission denied (publickey).
- Server too old for Ed25519. On OpenSSH older than 6.5, the server ignores the Ed25519 key. Generate an RSA key for that host instead.
- Modern server rejects an old RSA key. If the key is accepted by an old client but refused by a new server, the client is probably offering the SHA-1
ssh-rsasignature. Upgrade the client. As a temporary workaround only, you can addPubkeyAcceptedAlgorithms +ssh-rsato the server or-o PubkeyAcceptedAlgorithms=+ssh-rsato the client command. Older OpenSSH versions call that optionPubkeyAcceptedKeyTypes. - FIPS or compliance modes. Some systems restrict allowed algorithms. If an Ed25519 key is refused on such a server, check the policy before blaming the key. Ask the administrator which type is permitted.
- DSA keys. An old
id_dsakey will not work on any current OpenSSH release without special configuration. Replace it.
Using more than one key type
Nothing prevents you from owning an Ed25519 key and an RSA key at the same time. This is common when you administer both new servers and legacy equipment. Each key has its own file pair, for example id_ed25519 and id_rsa. The client tries the keys it knows about in turn, which can cause trouble: a server limits the number of failed authentication attempts, often to six by default through MaxAuthTries. If you hold many keys, the client may use up those attempts before it offers the right one. A later chapter shows how to bind a specific key to a specific host in ~/.ssh/config, which avoids this.
A short selection guide
- Choose Ed25519 unless you have a reason not to.
- Choose RSA at 3072 or 4096 bits when a target server is older than OpenSSH 6.5, or when a device or tool only accepts RSA.
- Choose ECDSA only when something requires it.
- Never create DSA keys, and never create RSA keys smaller than 2048 bits.
- If your organization has a written policy, it overrides these defaults.
Exercise
You must log in to two servers. Server A reports OpenSSH_9.6. Server B is an old appliance that reports OpenSSH_5.3. Which key type or types do you generate, and what RSA size would you use if needed?
Show answer
Generate an Ed25519 key for server A, since it runs OpenSSH 6.5 or newer. Server B is older than 6.5 and cannot use Ed25519, so generate an RSA key for it, at 3072 or 4096 bits. Keeping two separate key files lets you revoke either one without affecting the other.
Exercise
A colleague's RSA key works on an old server but gives Permission denied (publickey) on a server running OpenSSH 9.x. The key is 4096 bits. What is the most likely cause, and what is the preferred fix?
Show answer
The old client likely offers a SHA-1 ssh-rsa signature, which OpenSSH 8.8 and newer disable by default. The key size is not the problem. The preferred fix is to upgrade the client so it negotiates rsa-sha2-256 or rsa-sha2-512, or to generate an Ed25519 key. Re-enabling ssh-rsa on the server is only a temporary workaround.