ArunNetworkingPro
๐Ÿ”‘

SSH keys: never type a password again

Passwords are so last decade.

Passwords can be guessed. A proper SSH key can't be, not in a million years. Set it up once and you'll log in to your servers with a single short command and no password.

Beginner30 minutesSSH
๐Ÿ’ปprivate key ๐Ÿ”‘๐Ÿ–ฅ๏ธpublic key ๐Ÿ”’prove it!*math happens*password123 โŒ

๐Ÿงฐ What you need

Let's build it

1

Make your key pair

ssh-keygen -t ed25519 -C "my laptop"

Press Enter to accept the location, and set a passphrase (your key's own password). You now have two files: id_ed25519 (private: never share it) and id_ed25519.pub (public: share it freely).

2

Give the server your public key

ssh-copy-id your-username@your-server-ip

On Windows, where ssh-copy-id doesn't exist, run this in PowerShell instead:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh your-username@your-server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

3

Log in with the key

ssh your-username@your-server-ip

It asks for your key's passphrase (or nothing at all, if you left it empty), not the server's password.

4

Give it a nickname

Add this to ~/.ssh/config on your computer:

Host lab
    HostName your-server-ip
    User your-username

Now ssh lab is all you ever type.

5

Close the password door (optional, pro move)

Only once key login definitely works, on the server:

echo 'PasswordAuthentication no' | sudo tee /etc/ssh/sshd_config.d/10-no-passwords.conf
sudo systemctl restart ssh

โœ… How you know it worked

ssh lab logs you straight in, and ssh -o PubkeyAuthentication=no lab is refused if you closed the password door.

๐Ÿ’ฅ Break it on purpose

On the server, rename your key away:

mv ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak

Open a new terminal and try ssh lab. If you disabled passwords, you're now locked out, so do this with a second session still open. Put it back with mv ~/.ssh/authorized_keys.bak ~/.ssh/authorized_keys.

๐Ÿง  What's really going on

Your private key never leaves your computer. When you connect, the server uses your public key to set a challenge that only the matching private key can answer. Nothing secret crosses the network, so there's nothing to steal or guess, and an ed25519 key is far stronger than any password a human could remember.

โ† Back to all Linux labs ยท Stuck? Email me