How SSH Key Pairs Authenticate Linux Logins in the Cloud
How SSH key pair login works step by step: key exchange, host keys, RFC 4252 signatures, how EC2 installs your public key, and OpenSSH 10 changes.
An SSH key pair authenticates a cloud Linux login by proof of possession. The server holds your public key in ~/.ssh/authorized_keys. When you connect, your client signs data tied to that specific session with the private key, and the server checks the signature against the stored public key. The private key never leaves your machine.
This post explains the mechanism step by step: what happens during the connection, how cloud providers such as AWS get your public key onto a new instance, and what changed in recent OpenSSH releases. If you are looking for a hardening checklist instead, see our guide to configuring secure SSH access in Linux.
Key takeaways
- There are two separate authentications in every SSH connection: the server proves its identity with a host key, then the user proves theirs with a user key.
- Under RFC 4252, the client signs data that includes the session identifier, so a captured signature cannot be replayed on another connection.
- On Amazon EC2, the public key you choose at launch is written to
~/.ssh/authorized_keyson first boot. AWS does not keep a copy of the private key. ssh-keygengenerates an Ed25519 key by default. OpenSSH 10.0 removed DSA support and made the hybrid post-quantummlkem768x25519-sha256key exchange the default.- OpenSSH 10.1 added a warning when a connection falls back to a key exchange that is not post-quantum, controlled by the
WarnWeakCryptooption. - At scale, SSH certificates or keyless access such as AWS Systems Manager Session Manager are easier to manage than copying public keys to every server.
Why key pairs replaced passwords for cloud logins
A password is a shared secret. You type it, it travels to the server inside the encrypted channel, and the server compares it with a stored hash. That works, but the secret leaves your machine on every login, it can be guessed, and the same password is often reused elsewhere. Internet-facing cloud instances receive a steady stream of automated login attempts, so password authentication over the public internet is a risk most teams no longer accept.
Public key authentication removes the shared secret. The server only ever stores the public half of your key, which is safe to publish. The private half stays on your laptop, in a hardware token or in an agent. Logging in proves you hold the private key without revealing it. Even if an attacker copies every file from the server, they get nothing that lets them log in as you somewhere else.
That property is why cloud providers build their default Linux login flow around key pairs, and why many cloud images ship with password authentication disabled in the SSH daemon configuration.
The two halves of a key pair
A key pair is generated together by a tool such as ssh-keygen. The two files are mathematically linked: data signed with the private key can be verified with the public key, but the private key cannot be worked out from the public one.
- The private key (for example
~/.ssh/id_ed25519) stays with you. It should be protected by a passphrase and readable only by your user. - The public key (for example
~/.ssh/id_ed25519.pub) is a single line containing the key type, the key itself and a comment. You copy it to every server you want to log in to.
Generating a key on a current OpenSSH release looks like this. The ssh-keygen manual says Ed25519 is the default type when you run it without arguments, and -a sets the number of key derivation rounds used to protect the private key file with your passphrase (the default is 32; more rounds make offline guessing slower).
ssh-keygen -t ed25519 -a 100 -C "you@laptop-2026"
# Show the fingerprint so you can compare it later
ssh-keygen -l -f ~/.ssh/id_ed25519.pub
Supported types in the current manual include ed25519, ecdsa, rsa, the FIDO hardware-backed ed25519-sk and ecdsa-sk, and a new hybrid mldsa44-ed25519 type. For most people, Ed25519 is the sensible choice. If you have a FIDO security key, ed25519-sk keeps the secret on the hardware, and the authenticator must be attached every time the key is used.
Step by step: what happens when you run ssh
When you type ssh [email protected], several things happen before you see a prompt. It helps to separate them, because many login problems are caused by confusing one step with another.
1. Transport setup and key exchange
The client and server agree on algorithms and run a key exchange to create shared session keys. Everything after this point is encrypted. The key exchange also produces a value called the session identifier, which is unique to this connection.
OpenSSH 10.0 made the hybrid post-quantum algorithm mlkem768x25519-sha256 the default for key agreement, according to the OpenSSH release notes. It combines ML-KEM with the classical X25519 exchange. OpenSSH 10.1 added a warning when a connection negotiates a key exchange that is not post-quantum; the new WarnWeakCrypto client option controls it and is on by default.
2. The server proves who it is
The server signs part of the key exchange with its host key. Your client checks that host key against ~/.ssh/known_hosts. The first time you connect, there is no entry, so the client shows you the fingerprint and asks whether to trust it. If the key later changes unexpectedly, the client refuses to continue and prints a warning, because that may mean you are talking to a different machine.
Cloud instances generate new host keys on first boot, so a recycled IP address often triggers this warning legitimately. Confirm the reason before you delete the old entry with ssh-keygen -R.
3. The user proves who they are
This is the public key method defined in RFC 4252, section 7. The client may first ask the server whether a given public key would be acceptable for this user, which avoids asking for a passphrase on keys the server will reject. If the server says yes, the client builds a request that includes the session identifier, the user name and the public key, and signs it with the private key.
The server then checks two things. First, that the public key is authorised for that user, which for OpenSSH usually means it appears in the user's ~/.ssh/authorized_keys. Second, that the signature is valid for that public key. If both pass, the user is authenticated. The server may still require another method, such as a second factor, if it is configured to.
Because the signed data includes the session identifier, a signature captured from one connection is useless on another. That is the main reason key authentication resists replay.
4. Session starts
Once authenticated, the server starts your shell or command under your user account, applying any restrictions written in front of the key in authorized_keys, such as from= or command=.
How cloud providers deliver your public key
A new cloud instance has no user accounts you have configured yourself, so the provider needs a way to place your public key on it before your first login.
On Amazon EC2 you choose or import a key pair when launching an instance. The EC2 documentation explains that for Linux instances, the public key is placed in an entry within ~/.ssh/authorized_keys when the instance boots for the first time. Amazon EC2 stores the public key and you store the private key. AWS does not keep a copy of the private key, so if you lose it you cannot download it again; you have to use a recovery procedure or a different access method.
You can create the key yourself and import only the public half, which means the private key is never generated or seen by AWS:
aws ec2 import-key-pair \
--key-name laptop-ed25519 \
--public-key-material fileb://~/.ssh/id_ed25519.pub
Other providers use similar ideas with different plumbing, usually a metadata service read by cloud-init or a guest agent during boot. The authentication step itself is still the standard SSH public key method described above.
Common login failures and what they mean
| Error or symptom | Which step failed | Usual cause |
|---|---|---|
| Connection timed out | Before SSH starts | Security group, NACL or firewall blocks port 22, or wrong IP |
| Host key verification failed | Server authentication | Server's host key changed, often a reused IP |
| Permission denied (publickey) | User authentication | Wrong user name, wrong key, or key not in authorized_keys |
| Unprotected private key file | Client side | Private key readable by other users; fix with chmod 600 |
| Authentication refused: bad ownership or modes | Server side | Home directory or .ssh writable by others |
The default user name depends on the image. If you get "Permission denied (publickey)" on a fresh instance, check the image's documented user name before anything else. Run ssh -v to see which keys the client offered and which the server rejected.
Managing keys when you have many servers
Copying one public key to one server is easy. Managing keys for twenty engineers across two hundred instances is not. Keys pile up in authorized_keys files, people leave the company and their keys stay behind, and nobody is sure which key belongs to whom. A few approaches help.
Use the agent instead of copying private keys
Never copy your private key to a jump host. Use ssh-agent on your own machine and connect through a bastion with ProxyJump, so the bastion only forwards the connection and never sees the key.
Host bastion
HostName 203.0.113.10
User ec2-user
IdentityFile ~/.ssh/id_ed25519
Host app-*
User ec2-user
ProxyJump bastion
IdentityFile ~/.ssh/id_ed25519
Use SSH certificates
OpenSSH supports certificates signed by your own certificate authority. Servers trust the CA's public key once, through TrustedUserCAKeys in sshd_config, and accept any user certificate the CA has signed and that has not expired. You stop editing authorized_keys on every machine, and short-lived certificates expire on their own when someone leaves.
# On the CA host: sign a user's public key for one week, for login "ec2-user"
ssh-keygen -s ca_key -I alice-laptop -n ec2-user -V +1w id_ed25519.pub
Use keyless access where the provider supports it
The EC2 documentation points to AWS Systems Manager Session Manager as an alternative to key pairs. It gives an interactive browser-based shell or an AWS CLI session to the instance, so you can reach it without distributing SSH private keys. Many teams combine it with SSH for tasks that need file copy or port forwarding.
Good habits that keep key-based login secure
- Protect every private key with a passphrase and load it into an agent, so you type the passphrase once per session.
- Use one key per device, not one key copied everywhere. Losing a laptop then means revoking one key.
- Disable password authentication in
sshd_configonce key login works, and keep a second session open while you test. - Restrict port 22 to known address ranges in your security group instead of allowing the whole internet.
- Review
authorized_keysregularly, or move to certificates so old access expires automatically. - Keep OpenSSH updated so you get current defaults such as the post-quantum key exchange.
For more on the network side, see why AWS treats security groups as the main control over NACLs. For a team-wide list, our 12 SSH security practices for DevOps covers auditing and rotation, and configuring SSH keys in RHEL 10 walks through a Red Hat setup. If you are deciding between SSH and HTTPS for Git, see SSH vs HTTPS for cloning Git repositories.
How key authentication compares with other login methods
SSH supports several user authentication methods, and servers can require more than one. Password authentication sends a secret through the encrypted channel on every login. Keyboard-interactive authentication is often used for one-time codes. Public key authentication proves possession of a private key without sending it. Certificate authentication is a form of public key authentication where the server trusts a signing authority instead of individual keys.
In cloud environments the practical choice is usually between plain key pairs for small setups, certificates for larger fleets, and provider-managed access such as Session Manager when you want to avoid managing SSH credentials at all. Many teams use more than one: certificates for engineers, and a provider-managed path for emergency access when the SSH configuration itself is broken. Whatever you choose, write it down, because the worst outcome is an undocumented mix where nobody knows which access path is still live.
Practise it properly
The best way to understand SSH authentication is to break it on purpose: remove your key from authorized_keys, change permissions on .ssh, rotate a host key, and read ssh -v output each time. Our Linux administration training and RHCSA training cover SSH configuration as part of day-to-day administration, in the Kharadi classroom and live online.
FAQs
Does the private key ever get sent to the server?
No. The client uses the private key to sign data that includes the session identifier, and only the signature and public key are sent. The server verifies the signature with the public key it already has.
Where does the server store my public key?
By default OpenSSH reads it from the user's ~/.ssh/authorized_keys file. On Amazon EC2, the public key you choose at launch is written there when the instance first boots.
Which SSH key type should I use in 2026?
Ed25519 is the default in current ssh-keygen and a good choice for most users. If you have a FIDO security key, ed25519-sk keeps the private key on hardware. DSA keys were removed in OpenSSH 10.0.
What happens if I lose my EC2 private key?
AWS does not keep a copy, so it cannot be recovered. You need a recovery procedure from the EC2 documentation, or another access method such as Session Manager, to add a new public key.
What is the difference between a host key and a user key?
The host key proves the server's identity to your client and is checked against known_hosts. The user key proves your identity to the server and is checked against authorized_keys.
Is SSH ready for post-quantum threats?
OpenSSH 10.0 made the hybrid mlkem768x25519-sha256 key exchange the default, and OpenSSH 10.1 warns when a connection uses a key exchange that is not post-quantum. Both ends need a recent enough version to use it.
Are SSH certificates better than authorized_keys?
For many servers and many users, usually yes. Servers trust one CA key, certificates can expire after hours or days, and you no longer need to edit authorized_keys on every machine.
References
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0