What an SSH key does and why you need one

An SSH key is a pair of digital locks that let you log into a server or computer without typing a password each time. One key stays on your machine (the private key), and one goes on the server you want to access (the public key). When you connect, the server checks that your private key matches the public key it has on file — if they match, you're in.

SSH keys are more secure than passwords because they're much longer and harder to guess or steal. They also let you automate tasks — scripts and programs can log in using the key without you having to enter anything by hand. If you manage servers, work with cloud platforms like AWS or DigitalOcean, or use version control systems like GitHub, you'll need SSH keys to do most of that work.

Key Takeaways

  • SSH keys come in a pair: a private key that stays on your computer and a public key that goes on the server you want to access.
  • On Windows, use PuTTYgen or the built-in OpenSSH tool; on Mac and Linux, use the ssh-keygen command in Terminal.
  • Your private key file should never be shared, moved to email, or stored anywhere except your computer — treat it like a password.
  • After creating the key pair, you upload only the public key to the server or service, then test the connection before deleting your old password.

Creating an SSH key on Mac or Linux

Open Terminal and type this command:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa

This tells the system to create a new key pair using the RSA algorithm with 4096 bits of encryption (strong enough for most purposes). The key files will be saved in a folder called .ssh inside your home directory. When you press Enter, the system will ask you to enter a passphrase — this is an extra password that protects your private key if someone gets access to your computer. You can leave it blank if you want, but adding one is safer.

After you finish, two files appear in your ~/.ssh folder: id_rsa (your private key — keep this secret) and id_rsa.pub (your public key — this is what you share). You can check that they were created by typing ls ~/.ssh and pressing Enter.

Creating an SSH key on Windows

Windows 10 and newer have OpenSSH built in. Open PowerShell (right-click the Start button and select Windows PowerShell or Terminal), then type:

ssh-keygen -t rsa -b 4096 -f $env:USERPROFILE\.ssh\id_rsa

This works the same way as the Mac and Linux command — it creates your key pair in a .ssh folder in your user directory. You'll be asked for a passphrase; add one if you want extra security, or press Enter twice to skip it.

If PowerShell doesn't recognize the ssh-keygen command, you may need to install OpenSSH. Go to Settings > Apps > Apps & Features > Optional Features, click Add a Feature, search for OpenSSH Client, and install it. Then try the command again.

Alternatively, you can use PuTTYgen, a separate tool that some people find easier. Download it from the PuTTY website, open it, click Generate, move your mouse around the window to create randomness, then click Save Private Key and Save Public Key. This creates two files you can use the same way.

Uploading your public key to a server

Once your keys exist, you need to put the public key on the server or service you want to access. The exact steps depend on where you're connecting to — GitHub, AWS, a VPS provider, or your own server all have slightly different processes.

For a server you control directly, you'll usually log in with a password one last time, then add your public key to a file called authorized_keys in the .ssh folder. Open the id_rsa.pub file on your computer (the one with .pub at the end), copy all the text inside it, then paste it into the authorized_keys file on the server. Make sure it's all on one line.

For services like GitHub or AWS, look for a Settings or Security section in your account, find SSH Keys or Key Pairs, and paste the contents of your id_rsa.pub file into the box provided. Each service labels this differently, but the idea is the same: you're telling the service "when someone connects with the private key that matches this public key, let them in."

Testing your SSH key and removing password login

Before you delete your password or disable password login, test that the key actually works. Open Terminal or PowerShell and type:

ssh -i ~/.ssh/id_rsa username@server-address

Replace username with your actual username on that server and server-address with the server's IP address or domain name. If you set a passphrase on your key, you'll be asked for it now. If the connection succeeds, you're logged in — that means the key pair is working correctly.

Once you've confirmed the key works, you can disable password login on the server to make it more secure. This step is optional but recommended. Log in using your SSH key, then edit the file /etc/ssh/sshd_config and change the line that says PasswordAuthentication yes to PasswordAuthentication no. Save the file, restart the SSH service, and test the connection one more time to make sure you can still log in with the key.

Keeping your private key safe

Your private key is like a master password — anyone who has it can log into any server where you've uploaded the matching public key. Never email it, upload it to cloud storage, or share it with anyone. Never paste it into a website or form unless you are absolutely certain the website is legitimate and secure.

Keep your private key file on your computer only. If you use multiple computers, you can create a separate key pair for each one, or you can copy the private key file to the other computer — but do this carefully, using a secure method like an encrypted USB drive or a secure file transfer tool. If you think your private key has been compromised, delete it and create a new one, then upload the new public key to replace the old one everywhere you've used it.

If you set a passphrase when you created the key, you'll need to enter it each time you use the key — unless you add it to an SSH agent, which remembers it for the duration of your session. On Mac and Linux, the ssh-add command does this. On Windows, the SSH agent service can store it automatically.

Troubleshooting common problems

If you get a "Permission denied" message when you try to connect, the most common cause is that the public key on the server doesn't match the private key on your computer. Double-check that you copied the entire contents of id_rsa.pub and pasted it correctly into the authorized_keys file. Make sure there are no extra spaces or line breaks.

If the system says it can't find your key file, check that the path is correct. On Mac and Linux, the default location is ~/.ssh/id_rsa. On Windows with PowerShell, it's $env:USERPROFILE\.ssh\id_rsa. If you saved it somewhere else, use the full path to that location instead.

If you forgot the passphrase you set on your key, there's no way to recover it — you'll need to create a new key pair. This is actually a security feature: it means no one else can recover it either.

Frequently Asked Questions

Can I use the same SSH key on multiple servers?

Yes. You can upload the same public key (id_rsa.pub) to as many servers as you want. Each one will recognize your private key and let you in. This is convenient, but it also means if your private key is compromised, someone could access all those servers. Some people create separate key pairs for different purposes — one for GitHub, one for work servers, one for personal projects — to limit the damage if one key is stolen.

What's the difference between RSA and ED25519 keys?

Both are secure, but ED25519 is newer and slightly faster. If the server or service you're connecting to supports it, ED25519 is a good choice. To create an ED25519 key instead of RSA, use ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519. Most modern systems support both, so either one will work.

Do I need to back up my SSH key?

Yes, but carefully. If your computer dies and you lose the private key, you'll need to create a new one and upload it to every server where you used the old one. Keep a backup on an encrypted external drive or in a password manager that supports file storage — never in plain cloud storage or email. Treat the backup like you treat the original: keep it secret and keep it safe.

Can I change the passphrase on an existing key?

Yes. Use the command ssh-keygen -p -f ~/.ssh/id_rsa (or the path to whichever key you want to change). It will ask for the old passphrase, then ask you to enter a new one. This doesn't change the key itself, just the password that protects it.

What if I need to use my SSH key on a different computer?

Copy the private key file (id_rsa, not id_rsa.pub) to the .ssh folder on the new computer using a secure method. On the new computer, make sure the file permissions are correct: the private key should be readable only by you. On Mac and Linux, use chmod 600 ~/.ssh/id_rsa. On Windows, right-click the file, select Properties, go to Security, and make sure only your user account has read and write access.