Langsung ke isi

FirKaizen DS

  • Home
  • VPS & Hosting
  • Control Panels
  • WordPress
  • Backups & Migration

How to Disable Password Login and Use SSH Keys Only

Oktober 23, 2026Oktober 20, 2026 oleh Firdaus Safari Putra

Watch /var/log/auth.log on any internet-facing VPS for a minute and you will see it: automated scripts cycling through usernames like root, admin, and test, hammering port 22. You can fight that noise with fail2ban, but there is a cleaner fix. Turn off password authentication entirely and let the server accept SSH keys only. When the password prompt is gone, guessing passwords stops working.

This guide walks through the full change on a Linux VPS running OpenSSH. You will generate a key pair, install the public key, confirm key login works, edit sshd_config, and test the change without locking yourself out. The whole process takes about ten minutes.

Why password login is the weak link

Passwords have three problems that no policy can fully fix. People reuse them across sites, they leak in breaches, and a short one can be guessed by a script that never sleeps. An SSH key pair avoids all three: the private key never leaves your machine, it is far too long to brute-force, and nothing about it appears in a breached password list.

That said, keys only help if the password door is actually closed. As long as PasswordAuthentication yes sits in your sshd_config, an attacker can simply ignore your key and keep guessing the password.

Password login SSH key login
Guessing attacks Possible around the clock Password prompt gone, guessing dead
Reuse risk Same password often works elsewhere Private key never typed into a website
Leaked credentials Breaches expose reusable secrets Only the public key sits on servers
Convenience Typing on every login One passphrase, cached by ssh-agent
Loss scenario Reset by email, often abused Revoked by deleting one line in authorized_keys

Before you change anything: the no-lockout checklist

Disabling password login is safe, but only if you set things up in the right order. Run through this list first:

  1. You have a back door. Know how to reach your server if SSH breaks: the web console or VNC panel from your VPS provider. Open it in a browser tab before you start.
  2. Your key is already installed. The next steps cover this, but do not edit sshd_config until key login works.
  3. Keep one SSH session open. Leave your current terminal connected while you test the new settings in a second terminal. If the test fails, the old session is still alive and you can fix things.

Step 1: create an SSH key pair

Do this on your own computer, not on the server. The private key should never travel to the VPS.

$ ssh-keygen -t ed25519 -C “[email protected]”

Accept the default file location. When it asks for a passphrase, pick a strong one. A passphrase means that even if someone copies the private key file from your laptop, they still cannot use it. Type it once per session and ssh-agent will remember it.

Step 2: copy the public key to the server

The public key goes into ~/.ssh/authorized_keys on the server. The easiest way:

$ ssh-copy-id user@your-server-ip

If ssh-copy-id is not available, copy the key by hand. On your computer, print the public key with cat ~/.ssh/id_ed25519.pub, then on the server append that line to ~/.ssh/authorized_keys. Set the right permissions:

$ chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

Step 3: confirm key login actually works

Log in with the key in a fresh session. If ssh-agent is running, you may not even need the -i flag:

$ ssh -i ~/.ssh/id_ed25519 user@your-server-ip

If you reach a shell without typing your server password, the key works. Do not skip this step. Everything after this depends on it.

Step 4: disable password authentication in sshd_config

Open the SSH daemon configuration on the server:

$ sudo nano /etc/ssh/sshd_config

Find each of these lines and set them exactly like this. Add the line if it is missing:

PubkeyAuthentication yes

PasswordAuthentication no

ChallengeResponseAuthentication no

PermitRootLogin prohibit-password

A few notes on what each line does. PubkeyAuthentication yes keeps key login enabled, which is the whole point. PasswordAuthentication no closes the password prompt. ChallengeResponseAuthentication no closes a second door that some setups use to ask for passwords through PAM. Leave UsePAM yes as it is on most distributions; turning PAM off entirely can break things like session setup, and it is not needed to stop password guessing. PermitRootLogin prohibit-password stops direct root login by password while keeping key-based root access possible; change it to no if you never log in as root at all.

Check the syntax before you apply anything:

$ sudo sshd -t

If that command prints nothing, the configuration is valid. Now reload the daemon. A reload keeps existing sessions alive:

$ sudo systemctl reload sshd

Step 5: test in a fresh session, keep the old one open

This is the step that saves you from yourself. Do not close your current session. Open a second terminal and log in again with your key:

$ ssh -i ~/.ssh/id_ed25519 user@your-server-ip

Once that works, verify that password login is really dead. Force a password-only attempt from the second terminal:

$ ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@your-server-ip

You should see “Permission denied” before any password prompt appears. That is the confirmation. If the key login failed instead, your first session is still connected: fix the key setup, reload sshd again, and retest. Only close the old session once the new one works.

Three extra hardening steps worth doing next

Key-only login is the big win, but these three take a minute each and close the remaining gaps:

  • Restrict who can log in. Add AllowUsers yourname to sshd_config so only your account can reach SSH at all. Other accounts stop being valid targets.
  • Block root login completely. If you always log in as a normal user and use sudo, set PermitRootLogin no. There is no reason to leave the root door visible.
  • Install fail2ban. Password guessing is gone, but the connection attempts still waste log space and CPU. Fail2ban bans the source IPs after a few failures, which quiets the noise.

Locked out? Here is the way back in

If you ignored the checklist and locked yourself out, do not panic. Open your provider’s web console or VNC panel and log in directly. Revert the lines you changed in /etc/ssh/sshd_config, run sudo sshd -t to check the syntax, then sudo systemctl reload sshd. SSH with a password works again. Then start over, this time with the second-terminal test.

Some providers also let you boot into recovery mode or mount the disk from another instance. Check your provider’s docs before you need them, not after.

FAQ

Does disabling password login break SFTP?

No. SFTP runs over the same SSH connection, so it uses your key exactly like a shell session does. FileZilla, WinSCP, and rsync all work with key authentication.

What about my sudo password?

Unchanged. This setting only affects how you log in over SSH. Sudo still asks for your account password inside the session.

Can I still use the same key from multiple computers?

You can copy the private key to a second machine, but the safer habit is generating a separate key pair per machine. Then you can revoke one device by deleting its public key line from authorized_keys without touching the others.

Should I change the SSH port too?

Moving off port 22 cuts automated scan noise, but it is obscurity, not security. Do it after key-only login, not instead of it.

Quick recap

Generate a key, copy it to the server, confirm key login works, set PasswordAuthentication no and ChallengeResponseAuthentication no in sshd_config, check the syntax with sshd -t, reload, and test in a fresh session while the old one stays open. Ten minutes of work, and password guessing against your server is finished for good.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.

Kategori VPS & Hosting Tag disable password login and use ssh keys only, how disable password login ssh keys only, how to disable password login and use ssh keys only, VPS & Hosting
Should You Change the Default SSH Port? How to Do It Safely
Control Panel vs Command Line: Which Is Better for Managing a VPS?

Recent Posts

  • How to Set Up a Mail Server on a VPS for Transactional Email
  • How to Set Up Uptime Monitoring for a VPS with a Free Tool
  • How to Tune Nginx Worker Processes for a Busy WordPress Site
  • How to Fix WordPress Upload Limits on a VPS (php.ini and Nginx)
  • How to Install and Configure Fail2Ban on Ubuntu Server
  • About FirKaizen
  • Contact
  • Privacy Policy
  • Terms of Service
  • Disclaimer
© 2026 PT FirKaizen Digital Solutions — Firdaus Safari Putra