What an SSH key is and why you need one

An SSH key is a pair of digital codes — one public, one private — that lets you log into a server without typing a password. The public key lives on the server you want to access. The private key stays only on your computer. When you try to connect, the server checks that your private key matches the public key it has on file, and if they match, you're in.

SSH keys are more secure than passwords because they're mathematically harder to guess or steal. A password is something you type the same way every time. An SSH key is a long string of characters that changes based on what you're connecting to, and it never travels across the network — only a proof that you have it does.

If you manage servers, deploy code, or connect to remote machines regularly, SSH keys save you from typing passwords constantly and protect you from brute-force attacks that try thousands of password guesses per second.

Key Takeaways

  • SSH keys come in a public part (goes on the server) and a private part (stays on your computer only), and you generate both at once using ssh-keygen.
  • On Windows, use Windows PowerShell or Windows Subsystem for Linux; on Mac or Linux, open Terminal and run ssh-keygen with no arguments to use the defaults.
  • The private key file should have permissions set to 600 (readable and writable by you only) or SSH will refuse to use it.
  • After generating your key, you copy the public key to the server's ~/.ssh/authorized_keys file, and then test the connection before disabling password login.

Generating your SSH key pair on Windows, Mac, or Linux

The tool to create SSH keys is called ssh-keygen, and it comes built into Mac and Linux. On Windows 10 and later, it's built into PowerShell. Open your terminal or PowerShell and type:

ssh-keygen -t ed25519 -C "your_email@example.com"

Replace your_email@example.com with an email or label that helps you remember what this key is for. The -t ed25519 part tells ssh-keygen to use a modern, secure algorithm. If you're on an older system that doesn't support ed25519, use -t rsa -b 4096 instead.

The tool will ask where to save the key. Press Enter to accept the default location (~/.ssh/id_ed25519 on Mac and Linux, C:\Users\YourUsername\.ssh\id_ed25519 on Windows). Then it will ask for a passphrase — a password that protects your private key if someone gets access to your computer. You can leave it blank, but adding one is safer. Type it twice and press Enter.

When ssh-keygen finishes, you'll have two files: id_ed25519 (your private key, which never leaves your computer) and id_ed25519.pub (your public key, which goes on servers).

Setting the correct permissions on your private key

SSH is strict about who can read your private key. If the permissions are wrong, SSH will refuse to use the key and you'll get an error like "Permissions 0644 for '/home/user/.ssh/id_ed25519' are too open."

On Mac or Linux, fix this by opening Terminal in the directory where your key lives and running:

chmod 600 ~/.ssh/id_ed25519

This sets the permissions so only you can read and write the file. On Windows with PowerShell, the permissions are usually correct by default, but if you get a permissions error, right-click the file, choose Properties, go to the Security tab, and make sure only your user account has Full Control.

You only need to set permissions once, right after you generate the key.

Copying your public key to the server

Now the server needs to know about your public key. You have two ways to do this: the fast way (if you can still log in with a password) and the manual way (if you can't).

The fast way: On Mac or Linux, use the ssh-copy-id command. Open Terminal and type:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server_address

Replace user with your username on the server and server_address with the server's IP address or hostname. It will ask for your password one last time, then copy the public key to the right place automatically.

The manual way: If ssh-copy-id doesn't work (or you're on Windows), log into the server with your password and create the .ssh directory if it doesn't exist:

mkdir -p ~/.ssh

Then open the file ~/.ssh/authorized_keys in a text editor, paste the entire contents of your id_ed25519.pub file (the one ending in .pub) as a new line, and save. The public key is one long line of text — copy all of it, including any email address at the end.

Testing your SSH key before you disable passwords

Before you lock yourself out, test that the key works. From your computer, type:

ssh -i ~/.ssh/id_ed25519 user@server_address

If you set a passphrase on your key, you'll be asked for it now (not the server password). If you get in without being asked for a password, the key is working. Type exit to log out.

If you get a "Permission denied" error, the most common causes are: the public key isn't in authorized_keys, the authorized_keys file has the wrong permissions (should be 600), or the .ssh directory has the wrong permissions (should be 700). Check each of these before trying again.

Disabling password login for better security

Once you've confirmed the key works, you can turn off password-based login so only SSH keys will work. This prevents attackers from guessing passwords. On the server, open /etc/ssh/sshd_config in a text editor (you'll need root or sudo access) and find the line that says PasswordAuthentication yes. Change it to PasswordAuthentication no and save.

Then restart the SSH service. On most Linux systems, type:

sudo systemctl restart ssh

or

sudo systemctl restart sshd

Test the connection again to make sure you can still log in with your key. If you can, password login is now off. If you can't, revert the change and troubleshoot the key setup before disabling passwords.

Managing multiple SSH keys for different servers

If you manage several servers or use different keys for different purposes, you can create multiple key pairs. Generate each one with a different name:

ssh-keygen -t ed25519 -C "production_server" -f ~/.ssh/id_ed25519_prod

This creates id_ed25519_prod and id_ed25519_prod.pub. To use a specific key when you connect, add the -i flag:

ssh -i ~/.ssh/id_ed25519_prod user@server_address

If you get tired of typing -i every time, you can create a config file at ~/.ssh/config that tells SSH which key to use for which server automatically. A simple config looks like:

Host production_server HostName 192.168.1.100 User deploy IdentityFile ~/.ssh/id_ed25519_prod

Then you can just type ssh production_server and it uses the right key.

Frequently Asked Questions

What's the difference between ed25519 and RSA keys?

Ed25519 is newer and shorter (256 bits) but just as secure as RSA with 4096 bits. Ed25519 is faster and less prone to implementation mistakes. Use ed25519 unless you're connecting to an old system that doesn't support it, in which case use RSA with 4096 bits.

Can I use the same SSH key on multiple servers?

Yes. Copy the same public key to the authorized_keys file on each server. This is convenient but means if your private key is stolen, the attacker can access all those servers. For high-security environments, create separate keys for separate servers or purposes.

What do I do if I lose my private key?

You'll need to generate a new key pair and add the new public key to each server's authorized_keys file. You can remove the old public key from authorized_keys at the same time. This is why keeping a backup of your private key in a secure location is a good idea.

Do I need to share my private key with anyone?

Never. The private key should stay only on your computer. If someone asks for it, they're asking for access to every server that key can reach. Only the public key (the .pub file) is meant to be shared or put on servers.

Why does SSH ask for a passphrase when I connect?

If you set a passphrase when you created the key, SSH asks for it to unlock the private key before using it. This protects the key if your computer is stolen. You can skip the passphrase for convenience, but a passphrase is more secure. Some systems use ssh-agent to remember the passphrase for your session so you only type it once.