If you manage a Linux VPS or a hosting account with SSH access, typing a password every time you connect gets old fast, and passwords are what automated bots guess at all day. SSH keys fix both problems. You keep a private key on your computer, put the matching public key on the server, and the server lets you in because you can prove you hold the private half.
This guide walks through the whole setup on Linux, macOS and Windows 10/11, then shows how to turn off password logins without locking yourself out.
What you need
- A server you can already log in to with a username and password (a VPS, or a hosting plan that includes SSH access)
- A terminal: Terminal on macOS or Linux, PowerShell on Windows 10 or 11
- About 15 minutes
- For the optional hardening step: root or
sudoaccess on the server
Step 1: Check whether you already have a key
List your SSH folder.
On macOS or Linux:
ls -al ~/.sshOn Windows (PowerShell):
ls $env:USERPROFILE\.sshIf you see id_ed25519 and id_ed25519.pub, you already have a key pair and can skip to Step 3. If the folder doesn't exist or only holds a known_hosts file, carry on.
On Windows, if PowerShell says ssh isn't recognized, open Settings, search for Optional features, and add OpenSSH Client. It's available on Windows 10 (version 1809 and later) and Windows 11.
Step 2: Generate an Ed25519 key
Run this on your own computer, not on the server:
ssh-keygen -t ed25519 -C "you@example.com"-t ed25519 picks the key type and -C adds a comment so you can recognize the key later. Any label works, such as laptop-2026.
Press Enter to accept the default location (~/.ssh/id_ed25519, or C:\Users\you\.ssh\id_ed25519 on Windows). Then choose a passphrase. The OpenSSH manual suggests 10 to 30 characters that aren't a simple sentence. There's no way to recover a lost passphrase, so store it in your password manager (see how to set up Bitwarden). You can change it later with ssh-keygen -p -f ~/.ssh/id_ed25519.
You now have two files:
id_ed25519is the private key. It stays on this computer.id_ed25519.pubis the public key. This is the one you hand out.
Step 3: Copy the public key to the server
The goal is to add one line, the contents of your .pub file, to ~/.ssh/authorized_keys in your account on the server. Pick the route that matches your computer.
Linux or macOS: use ssh-copy-id.
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-serverReplace user@your-server with your login and the server's hostname or IP address. You'll type your server password one last time. The tool creates ~/.ssh/authorized_keys if needed, appends your key, and skips keys that are already installed.
Windows 10/11: there's no ssh-copy-id in Windows' built-in OpenSSH, so pipe the key over yourself. This works when the server runs Linux:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@your-server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"In PowerShell, type is an alias for Get-Content, so this reads your public key, sends it over SSH, and appends it on the server. Use >> (append), never >, or you'll wipe any keys already there. If the server runs Windows instead, follow Microsoft's OpenSSH key guide, because Windows servers keep keys in different places.
Through your hosting panel: many hosts let you paste a public key into their dashboard instead. In cPanel, it's SSH Access > Manage SSH Keys > Import Key, and you must click Authorize afterward or the key won't work. Hostinger's hPanel has an SSH keys tab in each VPS's settings. Paste only the contents of the .pub file, as a single line.
Step 4: Check permissions on the server
The SSH server refuses to use your key if the files could be changed by other users. Log in (with your password, if the key isn't working yet) and run:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keysYour home directory must not be writable by other users either.
Step 5: Test the key login
Open a new terminal window and connect:
ssh user@your-serverIf it worked, you'll be asked for your key passphrase (the one from Step 2), not your server password, or you'll go straight in if you used no passphrase. If it still asks for the server password, see "If something goes wrong" below.
Step 6: Let ssh-agent remember your passphrase
ssh-agent holds your unlocked key in memory so you type the passphrase once per session instead of on every connection.
On Linux:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519On macOS, you can also store the passphrase in your Keychain:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519On Windows, the agent service is disabled by default. Open PowerShell as administrator once to enable it, then add your key from a normal window:
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519Step 7: Create a short host alias
Instead of typing ssh deploy@server1.example.com every time, add an entry to ~/.ssh/config on your computer (C:\Users\you\.ssh\config on Windows). Create the file if it doesn't exist:
Host myserver
HostName server1.example.com
User deploy
Port 22
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
AddKeysToAgent yesNow ssh myserver is all you need. HostName is the real address, IdentitiesOnly yes stops SSH from trying every key in your agent, and AddKeysToAgent yes loads the key into the agent the first time you use it. The file must not be writable by anyone but you. If your server uses a domain name, our DNS records guide explains how that name points to the server.
Step 8: (Optional) Turn off password logins
Once keys work, disabling passwords stops bots from guessing them. This needs root or sudo, so it applies to a VPS, not usually to shared hosting.
Edit the server's config:
sudo nano /etc/ssh/sshd_configSet these two lines (remove a leading # if there is one):
PasswordAuthentication no
KbdInteractiveAuthentication noMany distributions also read extra files from /etc/ssh/sshd_config.d/, through an Include line near the top of sshd_config, so those files are usually read first. Because OpenSSH uses the first value it finds for each setting, a drop-in file (on some Ubuntu servers, 50-cloud-init.conf) can quietly switch passwords back on. Check:
sudo grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/Change any yes you find to no. Then test the config and confirm the setting that will actually apply:
sudo sshd -t
sudo sshd -T | grep -iE 'passwordauthentication|kbdinteractiveauthentication'sshd -t prints nothing if the file is valid. Both lines from sshd -T should end in no. Now restart the service. The name depends on the distribution:
# Ubuntu
sudo systemctl restart ssh
# Red Hat Enterprise Linux and similar
sudo systemctl restart sshdIf one says the unit wasn't found, use the other. Finally, from a new window, confirm keys still work and passwords are refused:
ssh myserver
ssh -o PubkeyAuthentication=no user@your-serverThe second command should fail with "Permission denied." Only then close your original session.
If something goes wrong
- See what SSH is doing. Run
ssh -v user@your-server(add morevs, up to-vvv, for more detail). The output shows which key files your client tries and which login methods the server accepts. - Still asked for the server password? Check permissions with
ls -ld ~ ~/.ssh ~/.ssh/authorized_keyson the server. If your home folder or.sshis writable by group or others, the server ignores your key.chmod go-w ~fixes the home folder. - Wrong user. Each account has its own
~/.ssh/authorized_keys. A key added torootwon't let you in asdeploy, and the other way around. - Wrong key offered. Point SSH at the right one with
ssh -i ~/.ssh/id_ed25519 user@your-server, or setIdentityFilein your config. - Key pasted in a panel doesn't work. Make sure you pasted the whole
.publine, with no line breaks, and authorized it if the panel requires that. - Locked out after Step 8. Check whether your provider's control panel offers a web-based console or recovery mode, then undo the change.
- Lost or stolen laptop. Delete that key's line from
authorized_keyson every server, then generate a new key.
The bottom line
SSH keys are faster and much harder to attack than passwords. Generate an Ed25519 key with a passphrase, copy only the .pub file to your server, fix permissions, and let ssh-agent handle the typing. If you have root on the server, switch password logins off, but only with a second session open and a confirmed key login. While you're tightening up logins, passkeys apply the same public-key idea to websites.