Password login for SSH works, but it has two problems. Passwords can be guessed or leaked. And password logins are what automated bots hit thousands of times a day against any server with port 22 open to the internet. If you look at your SSH auth logs on a public server, you will see failed login attempts from dozens of IP addresses every hour, cycling through common username and password combinations.
Key authentication removes both problems at once. You generate a key pair: a private key that never leaves your machine and a public key that you put on the server. When you connect, the server issues a cryptographic challenge using your public key. Your client signs that challenge with the private key and sends back the signature. The server verifies it against the public key. Your actual private key never travels across the network at any point in this process.
An attacker watching the network traffic sees a signature they cannot reverse into a private key. An automated bot scanning for passwords will get nowhere because there is no password to guess.
Here is how to set it up.
Generate a key pair
On Linux, macOS, or Windows with OpenSSH (available by default on Windows 10 and later):
ssh-keygen -t ed25519 -C "your@email.com"
The -t ed25519 flag specifies Ed25519, the recommended key type for new keys. It produces shorter keys than RSA with equivalent or better security, and modern SSH servers have supported it for years.
The command will ask where to save the key. The default is ~/.ssh/id_ed25519 for the private key and ~/.ssh/id_ed25519.pub for the public key. The default location is fine unless you need multiple keys for different purposes.
It will also ask for a passphrase. A passphrase encrypts your private key file on disk, so even if someone copies the file, they cannot use it without the passphrase. Setting a passphrase is worth doing for any key that gives access to a production server or anything important. For low-stakes internal servers you control entirely, skipping it is more convenient and still much more secure than passwords.
If you need RSA for compatibility with older servers or older SSH configurations:
ssh-keygen -t rsa -b 4096 -C "your@email.com"
Use -b 4096 to get a 4096-bit key. The default RSA key size used to be 2048, which is still acceptable but 4096 gives more margin. See our RSA vs Ed25519 comparison for more detail on how the two key types compare.
Copy the public key to the server
The simplest way:
ssh-copy-id user@your-server
This appends your public key to ~/.ssh/authorized_keys on the server. It will prompt you for a password, connect, and make the change. After this, your key will be accepted for future connections.
If ssh-copy-id is not available (on some minimal systems or older macOS versions), you can do it manually:
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Then set the right permissions on the server. SSH is strict about file permissions and will silently ignore authorized_keys if they are wrong:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
If your authorized_keys file is world-readable or the .ssh directory is group-writable, SSH will refuse to use it and fall back to password authentication. This is a common source of confusion: you set up the key, but SSH keeps asking for a password. Check the permissions first.
Test the connection
ssh -i ~/.ssh/id_ed25519 user@your-server
The -i flag explicitly specifies which private key to use. If it connects without asking for a password, the key is working. Once you confirm it works, you can drop the -i flag from future connections (SSH will find the key automatically from the default location) or add an entry to ~/.ssh/config for the host.
A config file entry looks like this:
Host myserver
HostName 192.168.1.100
User admin
IdentityFile ~/.ssh/id_ed25519
After that, ssh myserver is all you need.
Disable password authentication
Once key authentication is working, you can turn off password logins entirely. This is strongly recommended for any server accessible from the internet.
Open /etc/ssh/sshd_config on the server:
sudo nano /etc/ssh/sshd_config
Find and set these lines:
PasswordAuthentication no
PubkeyAuthentication yes
If PasswordAuthentication is commented out (starts with #), uncomment it and set it to no. Then reload the SSH daemon:
sudo systemctl reload sshd
Do not do this before confirming your key login works. If you disable passwords before the key is correctly configured, you will lock yourself out. If that happens and you have console access (through a cloud provider's web console, like EC2 Instance Connect or DigitalOcean's web terminal), you can re-enable password auth. If you have no console access, you will need to recover the volume externally.
Managing multiple keys
If you have more than one key (for different servers, or a personal key and a work key), SSH handles this through the config file:
Host work-server
HostName work.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_work
Host personal-server
HostName 203.0.113.10
User admin
IdentityFile ~/.ssh/id_ed25519
SSH will use the specified key for each host automatically.
You can also add keys to ssh-agent, which holds decrypted keys in memory so you do not have to type the passphrase every time:
ssh-add ~/.ssh/id_ed25519
The agent will prompt for your passphrase once and hold the key until you log out or explicitly remove it with ssh-add -d.
What to do when it stops working
A few things commonly break key authentication:
Wrong file permissions: Run chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys on the server. This fixes most permission-related issues.
Wrong authorized_keys file: Make sure the public key is in ~/.ssh/authorized_keys for the user you are logging in as, not for root or another user.
Key type not supported: Very old SSH servers may not support Ed25519. Use RSA in that case.
SELinux or AppArmor restrictions: On some Linux distributions with SELinux enabled, the .ssh directory and files need the correct SELinux context. Run restorecon -Rv ~/.ssh to restore the defaults.
The server requires specific key types: Some hardened SSH configurations restrict which key types are accepted. Check /etc/ssh/sshd_config on the server for PubkeyAcceptedKeyTypes or PubkeyAcceptedAlgorithms lines.
To see verbose debug output from the client side:
ssh -vvv user@your-server
This shows exactly what the client is trying and what the server is responding. Look for lines starting with debug3: that mention key loading or authentication to see where the process fails.
Using keys with SSHDock
SSHDock supports private key authentication from the browser. In the connection form, paste the full contents of your private key file (the one without the .pub extension). SSHDock stores it in your browser's local storage and uses it to authenticate against the server.
If your key has a passphrase, you will be prompted to enter it when you connect. The passphrase decrypts the key locally in the browser; the passphrase itself does not go anywhere.
This works with both Ed25519 and RSA keys in PEM format (the standard format output by ssh-keygen).