Before you do that, you must keep the following things in mind:
- Make sure to create your ssh key-pair on your personal/work computer and add this public SSH key to the server so that at least you can login to the server.
- Disabling password based authentication means you cannot ssh into your server from random computers.
- You must not lose your ssh keys. If you format your personal computer and lose the ssh keys, you’ll never be able to access the server.
- If you are locked out, you will not be able to access your server ever.
Login as root to your Linux server using key based authentication. Use an editor like Nano or Vim to edit the following file:
Find the following line:
And change it to:
If there is a # (means commented out) at the beginning of that line, remove it.
Save the file after making these changes and restart the SSH service using this command:
systemctl restart ssh
That’s it. You have successfully disabled password based authentication in SSH.