I want to check via BASH if root access on a certain server is possible without a ssh-key. So the script should check if you have access without SSH key or not. Here is my attempt: (pseudocode)

/usr/bin/ssh -o BatchMode=yes -o PreferredAuthentications=password
if [[ "$?" == "255" ]]; then
    echo "root access possible without key"
    echo "not possbile"

But this code does not work simply because the error code is always the same. No matter if the Server needs a SSH-key or not. My question is now, with which command can I tell if the server needs an SSH key to login or not? Or, which command gives me an error code to check it with?

My goal is to search the entire local network for servers that do not yet use an SSH key


  • 2
    You also have the third possibility that root access through SSH is simply denied on the server through PermitRootLogin no. It would be easiest to simply contact the admin of the server and ask. If you don't know the admin of the server, then you have no business probing it for methods of root access. – Kusalananda Jul 12 '18 at 9:45

I don't think you can do that without actually trying the correct key or password, or checking the server configuration.

sshd happily asks the client for a password, even if you have PermitRootLogin prohibit-password set, and the password will never work. It also asks for a password even if you have PermitRootLogin no and the login will not work even with a key. If I recall correctly, it actually even goes through PAM to fake trying the authentication, it just does so with a known-invalid password, so that you can't tell from the timing either if password authentication is allowed. This is similar for AllowUsers etc.

Of course, in addition, using BatchMode disables asking for passwords on the client side (that's mentioned as the first thing in the man page), so the combination of -o BatchMode=yes -o PreferredAuthentications=password isn't very useful.

You'll have to actually login to the system and check what authentication methods work. If you can login with a key, then that key is in use. If you can login with a password, then that password is in use. After you've logged in, check that the value of PermitRootLogin is not yes (the default is prohibit-password), and that root's authorized_keys contains the key(s) it's supposed to contain and no others.


I hope that you cannot check this.

An ssh server should not let you acquire this kind of information on its internal configuration from outside the server. To do otherwise would pose a security risk.

Please, if you achieve this goal, report your technique as a bug to openssh developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.