Technical Anshu
Tutorials Blog
🐧 SSH Security

Secure SSH on a Fresh Linux Server with Keys and No Password Login

The automated setup gave your server an SSH door so you can manage it from your desk. This page locks that door properly: key login in, password guessing out — done in the safe order that cannot lock you out of your own server.

✍️ Technical Anshu🐧 Linux Server⏱️ 9 minute read
🎥 Your server from the video already has SSH running — this page is the security pass that should follow every fresh install

SSH is the front door of almost every Linux server, and the internet knocks on it constantly. Automated bots try common passwords against exposed SSH servers within hours of them appearing. A password — even a decent one — is a single secret that can be guessed, leaked or reused. An SSH key is a matched pair: a private half that never leaves your computer and a public half that lives on the server. No key, no entry, and there is nothing to guess. Here is the whole hardening sequence, in the only safe order.

Terminal — creating and installing an SSH key (example)
$ ssh-keygen -t ed25519 Your public key has been saved in id_ed25519.pub $ ssh-copy-id [email protected] Number of key(s) added: 1 $ ssh [email protected] Welcome to your server (no password asked)
Key creation and installation on your own computer. The address and username are examples — use your server’s real ones.
1

Step 1 — Create Your Key Pair on Your Own Computer

Run this on your machine, not the server. Accept the default location and set a passphrase on the key if the computer is shared.

  1. ssh-keygen -t ed25519 creates a modern, strong key pair
  2. Two files appear: the private key (never share it, never upload it anywhere) and the .pub public key (this half is safe to copy to servers)
  3. Already have a key from earlier work? Reuse it — one identity key for all your servers is normal
2

Step 2 — Install the Public Key on the Server

While password login still works, copy the public half across. On Linux and macOS the tool is built in; on Windows, the same command ships with the OpenSSH client, or you can paste the public key into the file by hand.

  1. ssh-copy-id [email protected] — use your real username and server address
  2. It asks for the server password one last time and appends your key to ~/.ssh/authorized_keys on the server
  3. Log in again with ssh [email protected] — if no password is asked, the key works
3

Step 3 — Prove It in a Second Session Before Changing Anything

This is the step that separates a secure server from a locked-out admin. Open a new terminal window and log in with the key while your first session stays open. Only when the second login succeeds do you touch the settings. If anything is wrong, your first session is still there to fix it.

4

Step 4 — Turn Off Password Login and Direct Root Login

Edit the SSH server settings on the server with sudo nano /etc/ssh/sshd_config, set the two lines below, then restart the service with sudo systemctl restart ssh. Test in yet another new session before closing the old ones.

  1. PasswordAuthentication no — passwords stop working for SSH; keys only
  2. PermitRootLogin prohibit-password — the root account cannot be reached with a password over SSH at all; you log in as yourself and use sudo
  3. Honest note: direct root login with a key is sometimes kept on rescue systems by experienced admins. For a normal office or home server, your named account plus sudo is the safer and more accountable pattern
5

Step 5 — The Safety Net You Keep

Keep one local way in that does not depend on SSH: the machine’s own screen and keyboard, the virtual machine console, or your hosting provider’s web console. Server rooms and VPS panels both offer one. As long as that exists, even a total SSH mistake is a five-minute fix, not a disaster.

🔴 Never turn off password login before a key login has been tested in a separate session. That single ordering mistake is how most people lock themselves out of a fresh server — and it is completely avoidable.

🎥 Watch the Full Setup in the Video

The video’s server is reachable by SSH the moment setup ends. Do this hardening pass right after your first login, while the steps are fresh.

▶️ Watch Full Video
❓ Common Questions (FAQ)

I lost my private key. Am I locked out forever?

No, as long as you still have the local console or provider web console. Log in there, add a new public key to ~/.ssh/authorized_keys, and remove the old one. This is why keeping a non-SSH way into the machine matters.

Can I use the same key on several servers?

Yes. One key pair per person, used on every server they administer, is the normal pattern. If a laptop is ever lost, you remove that one public key from each server and the lost laptop’s access ends everywhere.

Should I also change the SSH port to something random?

It reduces background noise in the logs but adds almost no real security — bots find open ports on any number. Keys plus password login off is the protection that matters. Change the port only if you enjoy quieter logs, and update your firewall rule to match.

This guide is based on the Technical Anshu video linked above • Stuck on a step? Ask in the video comments — answers come there 🙏