Security hardening

How to disable SSH password login on CentOS without locking yourself out

A practical guide to using centos_disable_password_ssh.sh only after key-based access is confirmed, so you can turn off password login on CentOS safely and predictably.

7 min read · 2026-04-07

Use this only after key-based login already works

This script is not the step that creates SSH key access. It is the step that removes password-based access after key-based access is already in place and verified.

That distinction matters, because the most common failure pattern is disabling PasswordAuthentication first and only then realizing the target account does not actually have a usable authorized_keys file.

What the script changes in sshd_config

The script enforces PubkeyAuthentication yes, AuthorizedKeysFile .ssh/authorized_keys, PasswordAuthentication no, KbdInteractiveAuthentication no, and ChallengeResponseAuthentication no.

It deliberately keeps UsePAM enabled. On older CentOS hosts, turning PAM off is often unnecessary and can introduce avoidable session-handling regressions that do not help the key-only objective.

Terminal output showing centos_disable_password_ssh.sh checking authorized_keys, validating sshd -t, and reloading sshd successfully
A healthy run should show the authorized_keys check first, then the sshd_config backup, the auth changes, sshd -t validation, and only then the sshd reload.
  • Checks authorized_keys before making the change
  • Backs up sshd_config with a timestamp
  • Runs sshd -t before reload
  • Restores the backup automatically if validation fails

The safe operating pattern on a real server

Keep the current SSH session open. Open a second session, run the script there, and then start a third fresh login test before closing anything.

If the key belongs to a non-root account, pass TARGET_USER=<name> so the script validates the correct authorized_keys file. This is the main detail people miss on inherited servers.

関連トピック

CentOS 7 maintenance and recovery

Keep legacy CentOS 7 hosts installable and maintainable when yum repositories fail, mirrors expire, or Docker data needs to be moved off a crowded disk.

トピックを見る

問題ページ

Why can I not connect after disabling SSH password login on CentOS

The usual cause is disabling PasswordAuthentication before confirming that the target account already has a working authorized_keys file and a successful key-based login test.

ページを見る