Step 5 of 5

Disabled Password-Based Authentication

2 min

Password authentication can be a security risk, there are unlimited number of bots trying to brute force ssh every second on the open internet. while changing the default ssh port can reduce the number of non targeted attacks, it does nothing against targeted attacks. so it is always highly recommended to disabled password authentication.

Backup SSH configuration file

Before making any changes, it is a good practice to backup the SSH configuration file:

bash logo
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

Open SSH configuration file

To disable password authentication, you need to edit the SSH daemon configuration file:

nano
sudo nano /etc/ssh/sshd_config
vim
sudo vim /etc/ssh/sshd_config

Find and update the following lines

Find the following lines and change them to look like this, if a line does not exists you can add it:

sshd_config ssh-config logo
PasswordAuthentication no
PermitRootLogin prohibit-password
# optional if you are not planing to use 2fa
KbdInteractiveAuthentication no
# optional if you are not using password or 2fa authentication
UsePAM no

Save the file and exit the editor.

Test The configuration

Test your configuration before restarting the SSH service:

bash logo
sudo sshd -t

Restart SSH service

If there are no errors, restart the SSH service.

Recent ubuntu versions
sudo systemctl restart ssh
Older ubuntu versions
sudo systemctl restart sshd

Validate the changes

Before closing your current SSH session, open a new terminal/cmd/powershell window and try to connect to your server.

On your local machine bash logo
ssh your-user@your-server-ip

If you can connect successfully, then the changes are applied correctly.

Restore SSH configuration (if needed)

If for any reason when validating the changes you encounter issues, you can restore the previous configuration using the backup file we created at the beginning of this guide:

bash logo
sudo mv /etc/ssh/sshd_config.bak /etc/ssh/sshd_config