disable password login ssh centos

admin3 April 2024Last Update :

Securing SSH Access: Disabling Password Authentication on CentOS

In the realm of server management and security, one of the most critical steps an administrator can take is to secure Secure Shell (SSH) access. SSH is a protocol that provides a secure channel over an unsecured network in a client-server architecture, allowing users to log into another computer over a network, execute commands in a remote machine, and move files from one machine to another. However, with convenience comes potential vulnerability. Password-based logins are susceptible to brute-force attacks and other security breaches. In this comprehensive guide, we will delve into the process of disabling password login for SSH on CentOS, thereby enhancing your system’s security posture.

Understanding the Need for SSH Security Enhancements

Before we dive into the technicalities of disabling password authentication, it’s crucial to understand why this measure is so important. Passwords can be guessed, cracked, or obtained through phishing attacks. By switching to key-based authentication, you significantly reduce the risk of unauthorized access. Key-based authentication uses cryptographic keys which are much more difficult to compromise.

The Basics of SSH Key Authentication

SSH key authentication relies on a pair of keys – a private key that remains with the user and a public key that is placed on the server. The private key is never shared and is used to decrypt information encrypted by the public key. This method ensures that even if a third party intercepts the communication, they cannot decipher the content without the private key.

Step-by-Step Guide to Disable Password Login for SSH on CentOS

Now let’s walk through the process of securing your CentOS server by disabling password-based SSH logins. We’ll assume you have root access or equivalent sudo privileges for the following steps.

Step 1: Backup Your Current SSH Configuration

Before making any changes, it’s always wise to backup your current configuration. This way, you can easily revert back if needed.

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

Step 2: Generate SSH Key Pair

If you haven’t already done so, generate an SSH key pair on your local machine using the following command:

ssh-keygen -t rsa -b 4096

This creates a new SSH key, using the provided email as a label. You’ll be prompted to enter a file in which to save the key; press Enter to accept the default location.

Step 3: Copy the Public Key to Your CentOS Server

Next, copy your public key to the CentOS server you wish to secure. You can do this using the ssh-copy-id utility:

ssh-copy-id user@your_centos_server_ip

Replace “user” with your actual username and “your_centos_server_ip” with the server’s IP address.

Step 4: Disable Password Authentication on Your CentOS Server

With your key securely copied, it’s time to disable password authentication on your CentOS server. Edit the SSH configuration file using your preferred text editor:

nano /etc/ssh/sshd_config

Find the following lines and modify them accordingly:

PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

Save and close the file. For nano, you can do this by pressing CTRL + X, then Y, and Enter.

Step 5: Restart the SSH Service

For the changes to take effect, restart the SSH service:

systemctl restart sshd

Step 6: Test Your New Configuration

It’s crucial to test your new setup before closing your current session. Open a new terminal window and attempt to SSH into your server:

ssh user@your_centos_server_ip

You should be logged in automatically using your SSH key. If you encounter any issues, revert to your backup configuration and troubleshoot accordingly.

Best Practices for Managing SSH Keys

  • Keep your private key secure: Never share your private key and ensure its permissions are set to read-only by you.
  • Regularly rotate your keys: Change your SSH keys periodically to mitigate the risk of old keys being exploited.
  • Use strong passphrases: Protect your private key with a robust passphrase.
  • Limit SSH access: Use ‘AllowUsers’ or ‘AllowGroups’ in your sshd_config file to restrict which users can SSH into the server.

Frequently Asked Questions

What if I lose my private key?

If you lose your private key, you’ll need to generate a new key pair and copy the new public key to your server. Ensure you have an alternative access method set up beforehand, such as another user account with sudo privileges.

Can I use password authentication for specific users?

Yes, you can enable password authentication for specific users using Match blocks in your sshd_config file. However, this is not recommended due to security concerns.

Is it possible to recover from a lockout without physical access?

Recovering from a lockout without physical access can be challenging. It’s essential to have a backup access method, such as a console provided by your hosting provider.

How often should I change my SSH keys?

There is no hard rule, but it’s generally recommended to change your keys at least once a year or whenever you suspect they may have been compromised.


Disabling password login for SSH on your CentOS server is a significant step towards securing your systems against unauthorized access. By following the steps outlined in this guide, you can enhance your server’s security and protect sensitive data. Remember to manage your SSH keys responsibly and always keep backups of your configurations for quick recovery in case of errors.


Leave a Comment

Your email address will not be published. Required fields are marked *

Comments Rules :

Breaking News