How to Set Up SSH Keys and Disable Password Login
How to set up SSH keys: create an ed25519 key with ssh-keygen, copy it to your Linux server, test the login and safely turn off password login.

Contents
- Why keys beat passwords
- Step 1: Create a key pair with ssh-keygen
- Step 2: Passphrase and ssh-agent
- Step 3: Copy the public key to the server
- Step 4: Test the key login
- Step 5: Disable password login
- A shortcut with ~/.ssh/config
- Using multiple keys
- Locked out? Use the web console
- Common errors
- SSH keys on Nodesty servers
Setting up SSH keys is the most effective single step you can take to protect a Linux server. Once the server accepts only keys, the bots that try passwords on port 22 all day have nothing left to guess, and you no longer type a password every time you connect.
This guide shows how to set up SSH keys from start to finish: creating an ed25519 key on Linux, macOS and Windows, protecting it with a passphrase and ssh-agent, copying it to the server, testing the login and only then turning off password login in sshd_config. If SSH itself is new to you, start with our What is SSH? guide.
Why keys beat passwords
- A key can't be guessed. A password is something a person chose; an ed25519 key is 256 random bits. Brute-force attacks against it are pointless.
- The secret never leaves your computer. The server only stores the public key, which is useless to an attacker. With a password, the secret is sent to the server on every login.
- Access can be revoked per device. Each key is one line on the server. If a laptop is lost, you delete that line and everything else keeps working.
Step 1: Create a key pair with ssh-keygen
Create the key on your own computer, not on the server.
Linux and macOS
ssh-keygen -t ed25519 -C "alice@laptop"
-t ed25519 selects the modern key type, and -C adds a comment that helps you recognize the key later. Press Enter to accept the default location, then enter a passphrase. You now have two files:
| File | What it is |
|---|---|
~/.ssh/id_ed25519 | The private key. It stays on your computer, always. |
~/.ssh/id_ed25519.pub | The public key. This is what you add to servers. |
If you have to connect to a very old server that doesn't accept ed25519, create an RSA key instead with ssh-keygen -t rsa -b 4096.
Windows (PowerShell)
Windows 10 and 11 include the OpenSSH client, so the same ssh-keygen command works in PowerShell. The keys are saved in C:\Users\<name>\.ssh\. To print the public key:
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
PuTTYgen
If you prefer PuTTY, open PuTTYgen, select EdDSA (Ed25519) as the key type, click Generate and move the mouse. Set a key passphrase and save the private key as a .ppk file. The line you add to the server is the text in the box "Public key for pasting into OpenSSH authorized_keys file". In PuTTY, select the .ppk file under Connection, SSH, Auth, Credentials.
Step 2: Passphrase and ssh-agent
The passphrase encrypts the private key file on your disk, so a stolen laptop or a copied file isn't enough to log in. To avoid typing it on every connection, load the key into ssh-agent once; the agent keeps the unlocked key in memory for your session.
On Linux (many desktops already run an agent, in which case ssh-add is enough):
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
On macOS, ssh-add --apple-use-keychain ~/.ssh/id_ed25519 also stores the passphrase in the Keychain. On Windows, enable the agent service once in a PowerShell window opened as administrator, then add the key:
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519
ssh-add -l lists the loaded keys. To add or change the passphrase of an existing key, run ssh-keygen -p -f ~/.ssh/id_ed25519.
Step 3: Copy the public key to the server
Keys belong to a user: the public key goes into the ~/.ssh/authorized_keys file of the account you log in with. The examples use root; if you work with a regular user, replace the name. Our How to add a user on Linux guide shows how to create one with sudo rights.
With ssh-copy-id
On Linux and macOS, one command is enough while password login is still on:
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@203.0.113.10
It asks for the password one last time, appends the key to authorized_keys and sets the right permissions.
Windows has no ssh-copy-id. Use this in PowerShell instead:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh root@203.0.113.10 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
The manual way
Print the public key on your computer with cat ~/.ssh/id_ed25519.pub and copy the whole line. Then, on the server, logged in as the user the key is for:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Paste the key as a single line (it starts with ssh-ed25519 AAAA...), one key per line. The permissions matter: 700 for the .ssh folder and 600 for authorized_keys, both owned by that user. If others can write to them, sshd ignores the file. Our Linux file permissions guide explains these numbers.
Step 4: Test the key login
Keep your current session open and open a second terminal. Connect with keys only:
ssh -o PreferredAuthentications=publickey root@203.0.113.10
If you get a shell, the key works. At most you are asked for Enter passphrase for key, which is the passphrase of your own key. Permission denied (publickey,password) means the server did not accept the key; with a plain ssh root@203.0.113.10, the sign of a rejected key is that the server asks for root@203.0.113.10's password:. Don't continue until this test passes; the common errors section below helps.
Step 5: Disable password login
Open the SSH server configuration:
sudo nano /etc/ssh/sshd_config
Set these lines (remove the # in front if they are commented out):
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
PasswordAuthentication no turns off password login for all users, and KbdInteractiveAuthentication no closes the second way a password prompt can appear (Ubuntu and Debian already ship with it set to no; older releases call the option ChallengeResponseAuthentication). PermitRootLogin prohibit-password lets root log in with a key only. If you log in as a regular user and use sudo, set PermitRootLogin no instead.
Check the drop-in files. On Ubuntu and other distributions whose sshd_config starts with an Include line, the files in /etc/ssh/sshd_config.d/*.conf are read first, and for SSH the first value found wins. A file such as 50-cloud-init.conf containing PasswordAuthentication yes overrides what you wrote in the main file. Find every place where these settings appear and fix the value in each file:
sudo grep -riE "passwordauthentication|kbdinteractive|challengeresponse|permitrootlogin" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
Test the configuration, then restart the service:
sudo sshd -t
sudo systemctl restart ssh
sshd -t prints nothing if the configuration is valid. The service is called ssh on Ubuntu and Debian and sshd on AlmaLinux (sudo systemctl restart sshd). Open sessions survive the restart, so keep yours open and verify the result:
sudo sshd -T | grep -Ei "passwordauthentication|kbdinteractiveauthentication|permitrootlogin"
Finally, try to log in with a password from your computer. The server should refuse with Permission denied (publickey):
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password root@203.0.113.10
Key login is the core of SSH security. For the next layers, see our How to install Fail2ban guide and the Linux server security checklist.
A shortcut with ~/.ssh/config
Instead of typing the user, IP address and key every time, add a Host entry to ~/.ssh/config on your computer (C:\Users\<name>\.ssh\config on Windows):
Host myserver
HostName 203.0.113.10
User root
Port 22
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Now ssh myserver is enough, and scp, rsync, Git and SSH tunnels use the same entry.
Using multiple keys
A good habit is one key per device: instead of copying your private key from the laptop to the desktop, create a new key on each and add both public keys to authorized_keys. You can also create separate keys for different purposes by choosing a file name with -f:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "alice@work"
Point each Host entry in ~/.ssh/config to the right file with IdentityFile. IdentitiesOnly yes makes the client offer only that key; without it, a client with many keys can run into Too many authentication failures. To see which key a line in authorized_keys belongs to, compare fingerprints with ssh-keygen -lf ~/.ssh/id_ed25519.pub.
Locked out? Use the web console
If you lose your key or disable passwords too early, SSH is closed, but the server is not lost. The web console (VNC) in your provider's panel shows the server's screen independently of SSH. PasswordAuthentication no only applies to SSH, so you can still log in there with the user name and password; keep the root password in a password manager for that reason.
Pasting a long key into a console is awkward, so the easiest fix is usually to set PasswordAuthentication yes again, restart the SSH service, copy your new key as in step 3 and then turn password login off again.
Common errors
- Permission denied (publickey): the server accepts only keys and none of yours matched. Check that you are logging in as the user whose
authorized_keysholds the key (a key added forrootdoesn't work foralice) and that the client sends the right key:ssh -i ~/.ssh/id_ed25519 root@203.0.113.10.ssh -vshows which keys are offered. - The server still asks for a password: the key was not accepted. The most common cause is wrong permissions or a wrong owner; the server log then shows
Authentication refused: bad ownership or modes. Fix it on the server:sudo chown -R alice:alice /home/alice/.ssh sudo chmod 700 /home/alice/.ssh sudo chmod 600 /home/alice/.ssh/authorized_keys
The home folder itself must not be writable by the group or others either. On AlmaLinux, SELinux can also blocksshdfrom reading a folder that was copied or moved into place;sudo restorecon -Rv /home/alice/.sshrestores the right labels. - The key is broken across lines: a public key is one long line. If it was wrapped while pasting, delete it and paste it again.
- UNPROTECTED PRIVATE KEY FILE: the private key on your computer is readable by others. Run
chmod 600 ~/.ssh/id_ed25519.
For other messages such as Connection refused or a changed host key, see our SSH connection errors guide.
SSH keys on Nodesty servers
Nodesty AMD Ryzen 9 9950X VPS and AMD EPYC 7763 VPS plans come with NVMe disks and full root access, and OpenSSH is preinstalled on Ubuntu, Debian and AlmaLinux, so you can follow every step in this guide right after setup. If you lock yourself out, the web console in the Nodesty customer panel gives you the server's screen independently of SSH. Daily backups are free on VPS plans and are kept for 7 days.
Frequently Asked Questions
Should I create an ed25519 or an RSA key?
Use ed25519 unless you have a reason not to. The keys are short, fast and secure, and every OpenSSH version from the last ten years supports them. Only fall back to a 4096-bit RSA key if you have to connect to a very old server or device that does not accept ed25519.
Do I really need a passphrase on my SSH key?
It is strongly recommended. Without a passphrase, anyone who copies the private key file from your computer can log in to your servers. With a passphrase the file is encrypted, and ssh-agent or the macOS Keychain remembers it, so you only type it once per session.
Can I use the same SSH key on several servers?
Yes. The public key is not secret, so you can add the same one to as many servers as you like. What you should avoid is copying the private key to several computers. Create a separate key on each device instead, so you can remove a single device from authorized_keys if it is lost.
I set PasswordAuthentication no but the server still accepts passwords. Why?
Either the SSH service was not restarted, or another file overrides your setting. On Ubuntu, files in /etc/ssh/sshd_config.d such as 50-cloud-init.conf are read first, and the first value wins. Run sshd -T as root to see the values that really apply, fix the file that sets them and restart the service.
What does PermitRootLogin prohibit-password mean?
Root may still log in over SSH, but only with a key and never with a password. It is the OpenSSH default and a sensible setting if you work as root. If you use a regular user with sudo rights, you can go one step further and set PermitRootLogin no.
I disabled password login and lost my key. How do I get back in?
Use the web console (VNC) in your provider's panel. It shows the server's screen independently of SSH, and you log in there with the user name and password. Then add a new public key to authorized_keys, or turn password login back on for a moment to copy the new key.
About the author
Nodesty Team · Infrastructure & Support
The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.
Related articles

WordPress Security: A Hardening Checklist for Your Site
WordPress security checklist: updates, strong logins and 2FA, file permissions, wp-config.php, no PHP in uploads, XML-RPC, firewalls and tested backups.

How to Install Let's Encrypt SSL with Certbot (Nginx and Apache)
How to install Let's Encrypt SSL with Certbot on a Linux server: Nginx and Apache setup, automatic renewal, wildcard certificates and common errors.

How to Add a User on Linux and Give Sudo Rights
Add a user on Linux with adduser or useradd, give sudo rights, copy your SSH key, turn off root login, and lock, expire or delete users safely.