I use Linux, macOS and Windows every day, and I SSH into servers from all three. Typing passwords and long ssh -p 2222 someuser@some.long.hostname commands gets old fast. Setting up keys and a proper ~/.ssh/config file once on each machine fixes both problems, and it's more secure too.
This guide covers the whole thing on all three operating systems: creating a modern ed25519 key, getting the agent to remember it (including the macOS Keychain and the Windows ssh-agent service), copying it to a server, creating short aliases with custom ports, and then switching off password logins safely. There's a troubleshooting section at the end, because SSH is fussy about file permissions.
Prerequisites
- Linux: the OpenSSH client (
openssh-clienton Debian/Ubuntu,openssh-clientson Fedora/RHEL). Most distros include it. - macOS: nothing to install. OpenSSH ships with macOS. Open Terminal.
- Windows 10 (1809 or later) or Windows 11: the built-in OpenSSH Client. Use PowerShell.
- A server you can already log in to with a password, plus sudo access on it if you want to change its settings later.
In the examples, server.example.com is your server, deploy is your username on it, and 2222 is a custom SSH port. Swap in your own values.
Step 1: Check the client is installed
On Linux and macOS:
ssh -V
On Windows, open PowerShell and run ssh -V. If it isn't found, install the client from an administrator PowerShell, as Microsoft's Get started with OpenSSH for Windows page describes:
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*' Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
You can also add it under Settings > System > Optional features.
Step 2: Generate an ed25519 key
Ed25519 keys are short, fast and supported by any reasonably current OpenSSH server. The command is the same on all three systems:
ssh-keygen -t ed25519 -C "you@your-laptop"
- Press Enter to accept the default location:
~/.ssh/id_ed25519on Linux and macOS,C:\Users\<you>\.ssh\id_ed25519on Windows. - Set a passphrase. If someone copies your private key, the passphrase stops them using it. The agent (next step) means you won't have to type it all day.
- The
-Ccomment is just a label so you can tell keys apart later.
You now have two files. id_ed25519 is your private key: never share it or copy it to a server. id_ed25519.pub is the public key, and it's the one you hand out. Details of every option are in the ssh-keygen manual.
Step 3: Load the key into ssh-agent
The agent holds your unlocked key in memory, so you type the passphrase once per session rather than on every connection.
Linux
Most desktop environments start an agent for you. If ssh-add -l says it can't connect to an agent, start one in the current shell:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 ssh-add -l
macOS with the Keychain
macOS can store your passphrase in the Keychain so the key unlocks automatically after you log in:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
(Older guides use ssh-add -K. That flag was replaced by --apple-use-keychain in macOS 12 Monterey.) To make it stick across restarts, add UseKeychain yes and AddKeysToAgent yes to your config file in Step 5. Apple describes this behaviour in Technical Note TN2449.
Windows ssh-agent service
Windows runs the agent as a service, and it's disabled by default. From an administrator PowerShell:
Get-Service ssh-agent | Set-Service -StartupType Automatic Start-Service ssh-agent Get-Service ssh-agent
Then, in a normal PowerShell window:
ssh-add $env:USERPROFILE\.ssh\id_ed25519 ssh-add -l
The Windows agent stores keys securely for your user account and remembers them across reboots. Microsoft covers this in Key-based authentication in OpenSSH for Windows.
Step 4: Copy your public key to the server
Linux and macOS: ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 deploy@server.example.com
It logs in with your password one last time, appends the key to ~/.ssh/authorized_keys on the server, and sets sensible permissions. (On macOS, ssh-copy-id is included with recent versions. If yours doesn't have it, use the Windows method below, which works anywhere.)
Windows: the PowerShell alternative
Windows doesn't include ssh-copy-id. For a Linux server, this one-liner does the same job:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh -p 2222 deploy@server.example.com "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
If the server itself runs Windows, keys for administrator accounts go in the administrators_authorized_keys file inside C:\ProgramData\ssh\ instead, with specific permissions. Follow Microsoft's key management page for that case.
Test it. You should get in without being asked for your server password (you may be asked for your key passphrase if the agent isn't holding it):
ssh -p 2222 deploy@server.example.com
Step 5: Create shortcuts with ~/.ssh/config
This is the part that saves the most typing. Create (or edit) the config file:
- Linux and macOS:
~/.ssh/config - Windows:
C:\Users\<you>\.ssh\config(no file extension)
Here's an example:
Host web
HostName server.example.com
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host nas
HostName 192.0.2.10
User admin-user
Port 2200
Host *
AddKeysToAgent yes
ServerAliveInterval 60
Now ssh web does everything the long command did, and so do scp file.txt web:~/ and rsync -av site/ web:/var/www/site/. VS Code's Remote-SSH extension reads the same file too.
Things worth knowing:
- The first value wins. For each setting,
sshuses the first value it finds, so specificHostblocks go at the top andHost *defaults go at the bottom. This is documented in the ssh_config manual. IdentitiesOnly yesmakes ssh offer only the key you named, which avoids "Too many authentication failures" when your agent holds lots of keys.- On macOS, add
UseKeychain yesto theHost *block. If you share this file with Linux machines, putIgnoreUnknown UseKeychainat the very top so they don't complain about a Mac-only option. - See what ssh will actually use for an alias with
ssh -G web. - On Linux and macOS, lock the file down with
chmod 600 ~/.ssh/config.
Step 6: Turn off password logins on the server
Only do this once key login works, and keep your current session open while you test, so you can't lock yourself out.
Many current distributions (including Ubuntu and Debian) read drop-in files from /etc/ssh/sshd_config.d/ before the rest of the main config. Like the client, the server uses the first value it finds, and some cloud images ship a drop-in that turns passwords back on. So give your file a name that sorts first:
sudo tee /etc/ssh/sshd_config.d/00-keys-only.conf <<'CONF' PasswordAuthentication no KbdInteractiveAuthentication no PubkeyAuthentication yes PermitRootLogin no CONF
If your distro's sshd_config doesn't have an Include /etc/ssh/sshd_config.d/*.conf line near the top, put those settings directly in /etc/ssh/sshd_config instead. Then test the config, confirm the settings that will apply, and reload:
sudo sshd -t sudo sshd -T | grep -Ei 'passwordauthentication|kbdinteractive|permitrootlogin' sudo systemctl reload ssh # Debian/Ubuntu sudo systemctl reload sshd # Fedora/RHEL and most others
From a second terminal, run ssh web to confirm you can still get in. For deeper hardening (fail2ban, AllowUsers, 2FA), see my guide to hardening your SSH server beyond password authentication. Synology owners should read SSH key authentication on DSM 7, because DSM handles home folders differently.
Troubleshooting
Your best friend is verbose mode, which shows which keys are offered and why they're refused:
ssh -v web
On the server, check the log: sudo journalctl -u ssh -e (or -u sshd).
Permissions (the most common problem). OpenSSH refuses to use keys, and the server ignores authorized_keys, if the files are readable by other users. On Linux and macOS, on both client and server:
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_ed25519 ~/.ssh/config chmod 644 ~/.ssh/id_ed25519.pub chmod 600 ~/.ssh/authorized_keys
Your home directory on the server must not be writable by group or others either (chmod go-w ~). That's the server's StrictModes check.
Windows "UNPROTECTED PRIVATE KEY FILE" or "bad permissions": remove inherited permissions so only your account can read the key:
icacls $env:USERPROFILE\.ssh\id_ed25519 /inheritance:r /grant:r "$($env:USERNAME):F"
Other common errors:
Could not open a connection to your authentication agent(Linux): runeval "$(ssh-agent -s)"first.Error connecting to agent(Windows): the ssh-agent service isn't running. Re-run Step 3 as administrator.Too many authentication failures: addIdentitiesOnly yesand anIdentityFileline for that host.Permission denied (publickey): the key isn't in the right user'sauthorized_keys, the username is wrong, or permissions are too open.ssh -vshows which key was offered.REMOTE HOST IDENTIFICATION HAS CHANGED: if you know why (the server was rebuilt), remove the old entry withssh-keygen -R "[server.example.com]:2222". If you don't know why, stop and find out first.- Git on Windows ignores your agent: Git for Windows can use its own bundled ssh. Point it at Windows OpenSSH with
git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe".
Wrapping up
One key per machine, the agent holding it, and a config file full of short aliases. Set it up once on each of your Linux, macOS and Windows machines and ssh web works the same everywhere. Once that's in place, switching off password logins on your servers is the single biggest SSH security win you can make.
Comments (0)
No comments yet. Be the first to leave one!
Leave a Comment