Linux/Unix: Force ssh client to use only password auth authentication when pubkey auth configured

last updated in Categories , , , , , , ,

I configured and use my openssh server to accept only public key based authentication. I need to test if my server accepting password. How to allow ssh client to use only password auth when pubkey auth configured?

You can force your ssh client on a Linux, MacOS, FreeBSD, OpenBSD and Unix-like system to use only password authentication.

How to get prompted for password when I use pubkey auth?

The syntax is:
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@server
For example:
$ ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no vivek@server1.cyberciti.biz
$ ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no root@ln.db1

Sample outputs:

Fig.01: How to force openssh client to use only password auth
Fig.01: How to force openssh client to use only password auth

Where,

  1. PreferredAuthentications=password – The methods available for authentication are like GSSAPI-based authentication, host-based authentication, public key authentication, challenge response authentication, and password authentication. Authentication methods are tried in the order specified above, though PreferredAuthentications can be used to change the default order.
  2. PubkeyAuthentication=no – Disable public key authentication and force ssh to use password using PreferredAuthentications=password

Please note that you must have PasswordAuthentication yes set in server’s /etc/ssh/sshd_config file. If you want root user access set PermitRootLogin yes in server’s /etc/ssh/sshd_config file. Otherwise you will get an error that read as follows:

Permission denied (publickey,keyboard-interactive).

A note about why root over SSH is bad idea with or without password

ssh root@mordor ?
ssh root@mordor ?

It is dangerous enabling root login. The attacker or bots try to log in your server using brute force methods. They start with a simple password like “123456” and so on. They do this for an extended time to gain root access. With advancement in bandwidth and given resources the attacker or bots might end up getting root access to your server. I do not trust root or any other users to use password-based login as most users are at bad choosing passwords. Hence, I recommend that you do not enable password based login. See how to use public key based login for more info:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.