Echo: Keep 2 terminal windows open, one as a back up to ensure you still have access if you get into issues and are able to revert the changes.
This is how I always do, and it works in one go.
First ensure you have generated a keygen (example puttygen for windows), and ensure the SSH keys folder is present & accessible:
ls -l ~/.ssh
If not present, make one and grant correct permissions as:
cd ~
mkdir .ssh
touch .ssh/authorized_keys
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
Ensure to create a backup of your sshd config file for any issues later on.
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
Now edit the filw as needed, like:
sudo nano /etc/ssh/sshd_config
Search for the relevant line and un-comment as:
#PubkeyAuthentication yes
PubkeyAuthentication yes
Change the YES to NO for password authetication:
PasswordAuthentication yes
PasswordAuthentication no
You can also disable SSH root login if you wish (using some other user for example) by changing the YES to NO:
PermitRootLogin yes
PermitRootLogin no
Once done restart the service:
sudo systemctl restart sshd
or
sudo service sshd restart