Log In to Your Linux Server With an SSH Key
Bots try passwords on every server on the internet. With an SSH key you log in without a password, and once password login is switched off, guessing a password no longer gets anyone in. You create the key on your own computer, copy its public part to the server, test it, and then switch off password login.
1. Create a Key on Your Computer
On Linux and macOS open a terminal; on Windows 10 and 11 open PowerShell. Run:
ssh-keygen -t ed25519
Press Enter to keep the suggested file. Then type a passphrase: it protects the key if someone copies it from your computer. The command creates two files in the .ssh folder of your user:
id_ed25519: the private key. It stays on your computer; never send it to anyone.id_ed25519.pub: the public key. This is the part you copy to the server.
2. Copy the Public Key to the Server
Use the user you log in with (for example alex, or root). The server asks for that user's password one last time.
Linux and macOS:
ssh-copy-id alex@YOUR.SERVER.IP
Windows PowerShell (one line):
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh alex@YOUR.SERVER.IP "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
3. Test the Key
ssh alex@YOUR.SERVER.IP
You are logged in without the server's password; only your key's passphrase is asked, if you set one. Do not continue until this works.
4. Switch Off Password Login
Leave your current SSH session open and test a new login in a second window before you close it. If you lock yourself out, use Console Access under Information → Console for a VPS, or IPMI / noVNC for a dedicated server.
As root (or with sudo) run:
printf "PasswordAuthentication no\nPermitRootLogin prohibit-password\n" > /etc/ssh/sshd_config.d/10-no-passwords.conf sshd -t systemctl reload ssh
sshd -t checks the settings and prints nothing when they are correct. On AlmaLinux and Rocky Linux the last command is systemctl reload sshd.
The file name starts with 10- on purpose. On Ubuntu, the file 50-cloud-init.conf in the same folder switches password login on, and SSH uses the first value it reads, so your file has to come first. Check the result:
sshd -T | grep -E "passwordauthentication|permitrootlogin"
The answer must be passwordauthentication no and permitrootlogin prohibit-password. From now on, a login with a password is refused with Permission denied (publickey).
Keep your user's password anyway: sudo asks for it, and so does the X2Go desktop when it is locked.
Use the Key in Other Programs
- X2Go: in the session preferences, choose your private key in Use RSA/DSA key for ssh connection (1). An ed25519 key works there too. See Linux Desktop on Your Server With X2Go.
- WinSCP, FileZilla and FTP Rush: see How to Use SSH Keys with WinSCP, FileZilla and FTP Rush.
Switch Password Login On Again
rm /etc/ssh/sshd_config.d/10-no-passwords.conf systemctl reload ssh
