Kavindu Tharushan

Someone is guessing your server's password

Put a Linux server on the internet and within minutes, bots start trying to log in. Don't believe me? Check your own logs:

$ sudo grep "Failed password" /var/log/auth.log | tail -5

Those are strangers trying username and password combinations, all day, forever. You can't stop them from knocking. But you can make that knocking senseless.

The idea

Think of your server as a house. A password is a key anyone can copy by guessing the shape, and bots can try thousands of shapes a minute. An SSH key is different. You make two matching pieces: a private one that never leaves your laptop, and a public one that goes on the server. The server only checks that you hold the matching piece, and you prove it without ever sending it. There's nothing left to guess.

1. Make a key pair

$ ssh-keygen -t ed25519 -C "laptop"
$ ssh-copy-id user@server

Set a passphrase when asked, so a stolen key file is useless on its own.

2. Test it

$ ssh user@server

If it logs you in without asking for the server's password, the key works.

FYI. If you turn off passwords before the key works, you've locked yourself out of your own house.

3. Turn passwords off

On the server, edit /etc/ssh/sshd_config:

PasswordAuthentication no
PermitRootLogin no

Check for typos, then reload:

$ sudo sshd -t
$ sudo systemctl reload ssh

Keep your current session open and test a new one in a second window before you close anything. One more gotcha: look in /etc/ssh/sshd_config.d/. On some systems a file there overrides your setting, so you think it worked when it didn't. On Fedora the service is called sshd.