Connect over SSH with an Ed25519 key
Create an Ed25519 SSH key, install it on your Linux server, test it, then disable password login without locking yourself out.
On this page
Your values
Enter them once and the commands in this guide update.
An SSH key replaces the password with a pair of files: a private key, which stays on your computer, and a public key, which you place on the server. Once passwords are disabled, the bots that try combinations on the SSH port have nothing left to guess.
This guide follows a precise order so that you do not lock yourself out: create the key, install it, test it, and only then disable the password.
Prerequisites
- A Linux server you can reach over SSH with a password. If that is not the case yet, follow Connect to your Linux server over SSH
- The account password, which you still need for the emergency console
- A fallback access: on a VPS, the NoVNC console in your client area. If your plan does not include one, ask support how to reach the server if you get locked out before you disable the password
- A terminal on your computer: PowerShell on Windows, Terminal on macOS or Linux
In the commands, replace YOUR_USER with your username and YOUR_SERVER_IP with the server's IP address. If your SSH port is not 22, add -p followed by the port to the ssh and ssh-copy-id commands.
Step 1: create the key on your computer
Run this command on your computer, not on the server. It is the same on Windows, macOS and Linux:
ssh-keygen -t ed25519 -C "my-computer"The text after -C is a simple comment that helps you recognize the key.
- Press Enter to accept the suggested location.
- Choose a passphrase. It encrypts the private key: anyone who copies the file cannot use it without the passphrase.
Two files are created in the .ssh folder of your account, ~/.ssh on macOS and Linux, C:\Users\YourName\.ssh on Windows:
| File | Role | Can it be shared? |
|---|---|---|
id_ed25519 |
Private key | Never, neither by email nor in a ticket |
id_ed25519.pub |
Public key | Yes, this is the one that goes on the server |
Step 2: install the public key on the server
Connect with the account you will use afterwards: the key only applies to that account.
From macOS or Linux
ssh-copy-id -i ~/.ssh/id_ed25519.pub YOUR_USER@YOUR_SERVER_IPEnter your password. The message Number of key(s) added: 1 confirms the addition.
From Windows
ssh-copy-id does not ship with Windows. Run this command in PowerShell:
Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub" | ssh YOUR_USER@YOUR_SERVER_IP "umask 077; mkdir -p ~/.ssh; tr -d '\r' >> ~/.ssh/authorized_keys; chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys"It appends the key to the authorized_keys file, removes Windows line endings and applies the correct permissions.
Check the installation
Compare your key's fingerprint with those on the server. On your computer:
ssh-keygen -lf ~/.ssh/id_ed25519.pubOn Windows:
ssh-keygen -lf "$env:USERPROFILE\.ssh\id_ed25519.pub"Then, in your session on the server:
ssh-keygen -lf ~/.ssh/authorized_keysThe same SHA256:... fingerprint must appear on both sides.
Step 3: test the key login
In a new window, connect with passwords forbidden:
ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no YOUR_USER@YOUR_SERVER_IP- The message
Enter passphrase for keyis normal: enter your key's passphrase. - If you reach the server, the key works.
- If you get
Permission denied (publickey), stop here and see Troubleshooting. Do not disable the password.
Avoid retyping the passphrase
The SSH agent keeps the key unlocked during your session.
On macOS, the passphrase is stored in the keychain:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519On Linux:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519On Windows, in PowerShell opened as administrator:
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add "$env:USERPROFILE\.ssh\id_ed25519"Step 4: disable password login
Before continuing, check these three points:
- Key login worked in a new window
- Your first SSH session is still open
- You have your fallback access and the account password
The following commands run on the server as root. With another account, switch to root first:
sudo -iCheck the configuration folder
grep -i '^Include' /etc/ssh/sshd_configThe expected result is Include /etc/ssh/sshd_config.d/*.conf. If nothing is displayed, go to the variant without a configuration folder.
Create the configuration file
cat > /etc/ssh/sshd_config.d/00-key-only.conf <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
EOF
chmod 644 /etc/ssh/sshd_config.d/00-key-only.confCheck before applying
First check the syntax. The command must display nothing:
sshd -tThen display the configuration actually in effect:
sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|permitrootlogin)'You should get passwordauthentication no, kbdinteractiveauthentication no and pubkeyauthentication yes. If you log in as root, permitrootlogin must not be no.
Apply the change
On Debian and Ubuntu:
systemctl restart sshOn AlmaLinux, Rocky Linux, CentOS and Fedora:
systemctl restart sshdYour open session normally stays active.
Variant without a configuration folder
If sshd_config has no Include line, open it:
nano /etc/ssh/sshd_configAdd these lines at the very top of the file, then save. Next, run sshd -t, sshd -T and the restart as above.
PasswordAuthentication no
KbdInteractiveAuthentication noStep 5: check the result
Keep your first session open and run these two tests in a new window.
Key login must succeed:
ssh YOUR_USER@YOUR_SERVER_IPPassword login must fail:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password YOUR_USER@YOUR_SERVER_IPThe expected result is:
YOUR_USER@YOUR_SERVER_IP: Permission denied (publickey).If both tests pass, you can close your first session. To go further, see Secure your server.
Roll back
From the session still open, or from the NoVNC console, delete the file you created and restart the service. Replace ssh with sshd depending on your system.
rm /etc/ssh/sshd_config.d/00-key-only.conf
sshd -t
systemctl restart sshTo open the console: in your client area, open your service page, click the terminal-shaped icon at the top right, choose NoVNC Console graphique (NoVNC graphical console), confirm with Ok, then click Connecter (Connect). Log in with the account password. The console keyboard layout may differ from yours.
If you edited sshd_config directly, remove the two lines you added.
Manage your keys
- Several computers: create one key per device and add each public key to the server. You can then remove one without affecting the others.
- Remove an access: delete the matching line in
~/.ssh/authorized_keys, for example after losing a computer. - Backup: keep a copy of the private key somewhere safe, such as a password manager. A forgotten passphrase cannot be recovered: you will have to create a new key.
Troubleshooting
Permission denied (publickey)
The server rejects your key. Check the following, in order:
- The username: the key must be installed on that account.
- The key offered by your computer:
ssh -v YOUR_USER@YOUR_SERVER_IPLook for the lines Offering public key and Server accepts key.
- The permissions on the server, from a session opened with the account concerned:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R "$(id -un):$(id -gn)" ~/.ssh
chmod go-w ~- On AlmaLinux, Rocky Linux, CentOS and Fedora, the SELinux context:
restorecon -Rv ~/.ssh- The server logs, with
sshdinstead ofsshdepending on the system:
journalctl -u ssh -n 50 --no-pagerA message such as Authentication refused: bad ownership or modes points to a permissions problem.
The server still asks for a password
If the message is YOUR_USER@YOUR_SERVER_IP's password:, the key was not accepted and the password is still active. Check the key as described above, then run the sshd -T command from step 4 again. If passwordauthentication yes remains, make sure your file ends in .conf, that the service was restarted and that no other file has a prefix sorted before 00-.
Do not confuse this with Enter passphrase for key, which asks for the passphrase of your local key.
UNPROTECTED PRIVATE KEY FILE!
Your private key is readable by other accounts, so the client refuses to use it. On macOS and Linux:
chmod 600 ~/.ssh/id_ed25519On Windows:
icacls "$env:USERPROFILE\.ssh\id_ed25519" /inheritance:r /grant:r "${env:USERNAME}:F"Too many authentication failures
Your agent offers too many keys. Specify the one to use:
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 YOUR_USER@YOUR_SERVER_IPCould not open a connection to your authentication agent
The SSH agent is not running. Go back to the commands in Avoid retyping the passphrase.
sshd: command not found
The /usr/sbin folder is not in your path. Switch to root with sudo -i, or use the full path /usr/sbin/sshd.
You no longer have access to the server
Open the NoVNC console and follow Roll back. If you cannot log in there, open a ticket from your client area. Never send your private key or your password.