1. Make a Key
Windows 10 and 11 include the OpenSSH client, so the same command works in PowerShell, macOS Terminal and a Linux shell:
ssh-keygen -t ed25519 -C "my laptop"
Press Enter to accept the file name, then type a passphrase. You get two files in the .ssh folder of your home directory (C:\Users\you\.ssh on Windows):
id_ed25519, the private key. It stays on this computer.id_ed25519.pub, the public key. This one goes on servers.
The comment after -C ends up at the end of the public key, so you can tell later which computer a key on a server belongs to.
2. Put It on the Server
On a new server, paste the public key into the SSH key field when you order a Linux server, and it is installed for root; see managing SSH keys.
On a running server, from macOS or Linux:
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
Windows has no ssh-copy-id; in PowerShell:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Both ask for the account's password one last time. Then ssh [email protected] should ask for the key's passphrase instead. Once it does, you can turn off password logins altogether.
3. Type the Passphrase Once
The SSH agent holds the unlocked key in memory, so you are not asked on every connection.
Windows: the agent is a service that is off by default. In PowerShell as administrator:
Get-Service ssh-agent | Set-Service -StartupType Automatic Start-Service ssh-agent
Then, as yourself:
ssh-add $env:USERPROFILE\.ssh\id_ed25519
macOS: store the passphrase in the Keychain once:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Then add these lines to ~/.ssh/config so SSH reads it from there:
Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519
Linux: most desktops start an agent for you, and ssh-add loads the key into it. Add AddKeysToAgent yes to ~/.ssh/config to load it the first time you use it.
4. Several Servers and Several Keys
~/.ssh/config gives each server a short name and says which key to use. On Windows the file is C:\Users\you\.ssh\config, with no extension:
Host web HostName 203.0.113.10 User alice IdentityFile ~/.ssh/id_ed25519 Host backup HostName 198.51.100.20 User root Port 2222 IdentityFile ~/.ssh/backup_ed25519 IdentitiesOnly yes
Now ssh web is the whole command, and scp, rsync and editors such as VS Code's Remote SSH read the same names. IdentitiesOnly yes stops SSH offering every key the agent holds, which otherwise can hit the server's limit on attempts and end in “Too many authentication failures”.
5. Using PuTTY or WinSCP
PuTTY and WinSCP use their own .ppk key format. Open PuTTYgen, choose Conversions → Import key, select id_ed25519, enter the passphrase, and Save private key as a .ppk file. In PuTTY, set it under Connection → SSH → Auth → Credentials. The public key on the server stays the same.
If the Key Is Refused
- Permissions: on the server,
~/.sshmust be700andauthorized_keys600, owned by that user; SSH ignores keys in files others can write. - Wrong user: a key added for
rootdoes not let you in asalice; each account has its ownauthorized_keys. - See what happens:
ssh -v webshows which keys are offered and which are refused. Fixing SSH errors covers the messages one by one. - Locked out: on a VPS, the browser console in the dashboard still accepts the password, so you can fix the file from there.
When a laptop is lost or someone leaves, delete their key's line from authorized_keys on every server; the comment at the end of each line tells you whose it is.