Network security

How to Set Up SSH: Key Authentication on Linux, macOS and Windows

SSH keys replace guessable passwords with cryptography. This guide walks through generating a key, installing it on a server, and locking password logins out — without locking yourself out.

Cysoro Security TeamPublished · 8 min read
NETWORK SECURITY Set Up SSH Keys Key login in 4 steps — no more passwords publickey ✓
Quick answer

To set up SSH, generate an Ed25519 key pair on your own computer with ssh-keygen -t ed25519, copy the public key to the server with ssh-copy-id user@server, test the login from a second terminal, and only then disable password authentication in the server's sshd configuration. Keep your first session open until the key login works.

Key takeaways

  • Generate the key on your own computer, never on the server, and protect the private key with a passphrase.
  • Test key login from a second terminal before you disable password authentication, or you can lock yourself out.
  • Ed25519 is the recommended key type; use 4096-bit RSA only for very old systems that do not support it.

Every Linux server on the internet gets probed for SSH logins, usually within minutes of going live. Passwords lose that fight eventually. SSH keys do not guess, do not get phished, and do not appear in breach dumps — which is why key authentication is the single most important SSH setup step.

What is SSH?

SSH, Secure Shell, is an encrypted protocol for logging in to a remote computer, running commands on it and copying files to and from it. It replaced Telnet and other plaintext remote-login tools, and it is the transport behind SFTP, rsync over SSH and Git over SSH.

When you connect, the server proves its identity with a host key, and you prove yours with either a password or a cryptographic key. The connection itself is encrypted end to end, so passwords and commands cannot be read by anyone watching the network.

The OpenSSH project, which ships the client and server on most Linux systems and on Windows, documents the protocol as standardized by the IETF in several RFCs. You can read the full reference in the OpenSSH manual pages.

How does SSH key authentication work?

Key authentication uses a pair of files. Your private key stays on your own computer and must never be shared or copied to a server. Your public key can be shared freely and is installed on every server you want to reach.

When you log in, the server sends a challenge that only the matching private key can answer. The private key itself never crosses the network, so there is nothing for an attacker to intercept or replay. If you add a passphrase, the private key is encrypted at rest, so a stolen laptop does not automatically mean a stolen server.

How SSH key authentication works Your computer Private key — never leaves Public key (.pub) — shareable ssh-keygen creates both files Your server ~/.ssh/authorized_keys holds your public key, one line server sends a challenge only the private key can answer The private key never travels over the network.
SSH key authentication: the private key stays home, the public key lives on the server. Illustration by Cysoro.

What do you need before you start?

You need a server or computer running an OpenSSH server, a way to log in to it once with a password or through your hosting provider's console, and an OpenSSH client on your own machine. Linux and macOS ship one in the terminal. Windows 10 and 11 include the OpenSSH client, usable from PowerShell.

Check that the client is present by running ssh -V. On the server, check the service with sudo systemctl status ssh on Ubuntu and Debian, or sudo systemctl status sshd on RHEL-based systems. If the service is not installed, install the openssh-server package first.

Step 1: Generate an Ed25519 key pair

Run this command on your own computer, in a terminal or PowerShell, not on the server. Ed25519 is the recommended key type: it is fast, compact and supported by every OpenSSH release of the last decade.

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

Accept the default file location unless you need a separate key for this server, in which case use -f ~/.ssh/myserver. When asked for a passphrase, set a strong one.

You will type the passphrase once per session if you use an SSH agent, so it costs little convenience and buys a lot of protection. If you must connect to a very old system that does not support Ed25519, generate a 4096-bit RSA key instead with ssh-keygen -t rsa -b 4096.

The command creates two files: ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public). Check for existing keys first with ls -la ~/.ssh — if a key already exists, reuse it or create a named one rather than overwriting it.

Step 2: Copy the public key to the server

The easiest method on Linux and macOS is ssh-copy-id. Log in with your password one last time and run it from your own computer.

ssh-copy-id user@server-ip

For a key with a custom filename, add -i ~/.ssh/myserver.pub. For a server on a non-default port, add -p 2222. The tool appends the public key to ~/.ssh/authorized_keys on the server and fixes the permissions for you.

If ssh-copy-id is unavailable, do it manually. On the server, run mkdir -p ~/.ssh && chmod 700 ~/.ssh && touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys, then paste the full single-line public key from your computer into that file. Never paste the private key.

Each public key must occupy exactly one line, and the files must belong to the login user, not root. DigitalOcean's Ubuntu SSH keys tutorial walks through the same steps with screenshots of the expected output.

Step 3: Test the key login before changing anything

Keep your original SSH session open. In a second terminal, connect with the key.

ssh user@server-ip

If you used a custom key file, add -i ~/.ssh/myserver. You should be asked for your key passphrase, not the account password, and then land in a shell. If it still asks for a password, run ssh -v user@server-ip and read the debug output: it shows which keys were offered and whether the server accepted them. Do not proceed to the next step until this test works.

Step 4: Disable password authentication

Only disable passwords after key login works. On modern Ubuntu and Debian systems, put the change in a drop-in file rather than editing the main sshd_config, because drop-ins survive package upgrades better and, on Ubuntu, a cloud-init file in /etc/ssh/sshd_config.d/ can otherwise override your setting.

Create /etc/ssh/sshd_config.d/10-keys-only.conf on the server with these lines:

PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitRootLogin prohibit-password

PermitRootLogin prohibit-password still allows root to log in with a key but never with a password. Some guides also set ChallengeResponseAuthentication no, the older name for the same setting on OpenSSH versions before 9.x.

Validate the configuration with sudo sshd -t, confirm the effective values with sudo sshd -T | grep -i passwordauthentication, then reload with sudo systemctl reload ssh on Ubuntu/Debian or sudo systemctl reload sshd on RHEL-based systems. Existing sessions stay connected across a reload. Ubuntu's OpenSSH server documentation covers the service commands for each release.

Now verify from a second terminal that password login is really off: ssh -o PubkeyAuthentication=no user@server-ip should fail with Permission denied (publickey). If a password still works, a drop-in file such as 50-cloud-init.conf is overriding you — remember that OpenSSH uses the first value it finds for each setting, so your file must sort before it, or you must remove the conflicting line.

How do you make SSH easier day to day?

An ~/.ssh/config file on your own computer turns a long command into a short nickname. Add a block per host:

Host myserver
HostName 203.0.113.20
User deploy
IdentityFile ~/.ssh/myserver

Then ssh myserver is all you type. An SSH agent saves you from retyping your passphrase: start it with eval "$(ssh-agent -s)" and add your key once with ssh-add ~/.ssh/id_ed25519. macOS and many Linux desktops run an agent automatically. For file transfers, the same keys work with scp and sftp, and for Git, a key added to your Git hosting account authenticates pushes without a password.

How do you set up SSH on Windows?

Windows 10 and 11 include the OpenSSH client and server as optional features. In PowerShell, the same commands work: ssh-keygen -t ed25519 creates the key in $env:USERPROFILE\.ssh, and ssh user@server-ip connects.

ssh-copy-id is not included, so copy the public key with Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub and paste it into the server's authorized_keys manually, or use the one-line remote append shown in many guides. PuTTY users can generate an Ed25519 key in PuTTYgen instead and paste the public key text from its window into authorized_keys.

What else should you harden after keys are working?

Key-only authentication removes the biggest risk, but a few more settings are worth adding. Limit who can log in with AllowUsers deploy or AllowGroups sshusers. Set MaxAuthTries 3 and LoginGraceTime 30 to slow down guessing.

Consider fail2ban or your firewall to block repeated failures, and keep OpenSSH updated — the server version is shown by sshd -V. These controls sit alongside the wider baseline in our small business cybersecurity checklist.

Changing the port from 22 to something else only reduces log noise; it is not a security control, and it can break tooling that assumes port 22. Agent forwarding (ssh -A) is convenient but exposes your agent to a compromised server, so use it sparingly and prefer ProxyJump (ssh -J) for hopping between hosts.

SSH setup checklist — tick these off in order ✓Key generated on your computer (Ed25519, with a passphrase) ✓Public key installed in ~/.ssh/authorized_keys (700 / 600 perms) ✓Key login tested from a second terminal, first session still open !PasswordAuthentication no — verified with sshd -T, then re-tested Never close your first session until the second one works.
The order matters: generate, install, test, then disable passwords. Illustration by Cysoro.

How do you fix common SSH setup problems?

Most failures fall into a small set of causes. Work through them in order.

SymptomLikely causeFix
"Permission denied (publickey)"Key not installed, wrong user, or bad permissionsCheck authorized_keys content, ownership and 600/700 modes; use ssh -v
"Permissions are too open"Private key readable by otherschmod 600 on the private key, chmod 700 on ~/.ssh
Password still works after disabling itDrop-in file overrides your settingCheck sshd -T output; fix or remove the conflicting file in sshd_config.d
Connection refusedsshd not running, wrong port, or firewallCheck systemctl status, the Port setting and firewall rules
Locked out entirelyPasswords off before keys workedUse the hosting provider console or recovery mode to restore access

If you are locked out, your hosting provider's web console or rescue mode is the recovery path — another reason to keep that console access ready before you start. Once back in, restore password authentication temporarily, fix the key, test again, and only then disable passwords.

How should you manage SSH keys over time?

Rotate keys when someone leaves, a laptop is lost, or a key may have leaked: generate a replacement, install it, then delete the old line from authorized_keys on every server.

Keep a backup of private keys in an encrypted password manager, not in plain files or chat messages, and never email a private key. For teams, consider hardware-backed FIDO2 keys (ssh-keygen -t ed25519-sk) or short-lived certificates instead of long-lived keys shared between people.

Also verify host keys the first time you connect. SSH warns you if a server's host key changes, which can mean the server was rebuilt — or that someone is intercepting the connection. Do not blindly accept a changed host key on a production server; confirm the new fingerprint with your provider first.

The bottom line

Setting up SSH properly takes about ten minutes: generate an Ed25519 key on your computer, install the public half on the server, test it from a second terminal, and then turn password authentication off. That one change removes the password-guessing attacks that hammer every public server.

Keep your private key protected with a passphrase, and use separate keys for separate servers where it matters. You can check your server's exposure with our free IP lookup tool.

For the network around it, see our guide to securing your Wi-Fi.

Frequently asked questions

What is SSH used for?

SSH, or Secure Shell, gives you an encrypted remote login and file transfer to another computer, usually a Linux server. It replaces older, unencrypted tools such as Telnet, and it is also the transport behind tools like SFTP, rsync over SSH and Git over SSH.

Is SSH key authentication safer than a password?

Yes. A password can be guessed, phished or reused from a breach. A private key is a long cryptographic secret that never travels over the network, and with a passphrase it stays useless even if someone copies the file. That is why servers exposed to the internet should use keys and disable password logins.

Where do I generate the SSH key, on my computer or on the server?

On your own computer, the machine you connect from. The private key must stay there. Only the public key, the file ending in .pub, is copied to the server. Generating a key on the server and downloading the private key defeats the purpose.

What is ssh-copy-id?

ssh-copy-id is a small utility that copies your public key into the server's ~/.ssh/authorized_keys file and sets the right permissions. It is the simplest way to install a key on Linux and macOS. On Windows, or when ssh-copy-id is unavailable, you can paste the public key into authorized_keys manually.

Why does SSH ignore my key and ask for a password?

The most common causes are wrong permissions, the key pasted onto more than one line, or the key added to the wrong user's authorized_keys file. The .ssh directory must be mode 700 and authorized_keys mode 600, both owned by the login user. Running ssh -v shows which keys the client offered and why the server refused them.

Can I use the same SSH key for several servers?

You can, but separate keys per server or per purpose limit the damage if one key leaks, and they make revocation simple: delete one line from authorized_keys. A ~/.ssh/config file lets you assign the right key to each host automatically.

Should I disable root login over SSH?

Yes, for most servers. Log in as a normal user and use sudo for administration. If root login must stay, set PermitRootLogin to prohibit-password so root can only log in with a key, never with a password.

What port does SSH use by default?

TCP port 22. Changing the port does not add real security, it only reduces log noise from automated scanners. Real protection comes from key-only authentication, keeping OpenSSH updated and limiting which users and addresses may connect.

Sources

  1. DigitalOcean — How to Set Up SSH Keys on Ubuntu
  2. OpenSSH — Manual Pages
  3. Ubuntu — OpenSSH Server documentation
Written & fact-checked byCysoro Security Team

The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.

  • Email authentication
  • Phishing & BEC
  • Domain & DNS security
  • Small-business security

Checked against the OpenSSH manual pages, Ubuntu's OpenSSH server documentation and DigitalOcean's Ubuntu SSH keys tutorial.

How we write and fact-check →

Free tools for this topic

No account needed.

See where your domain stands

Free for up to 3 domains. No card, no installs.

Start free