12

I'm trying to test if ~/.ssh/id_rsa is actually password protected.

When you run ssh-keygen you can choose an empty password, and I'm trying to detect this.

Is that possible with a one-liner?

3 Answers 3

16

If you execute:

ssh-keygen -y -f ~/.ssh/name_of_key

you will get key printed if there is no password like this:

ssh-keygen -y -f ~/.ssh/id_dsa
ssh-dss AAAAB3NzaC1kc3M....

If there is password of the key you will be asked for it

1
  • 6
    To add to this, SSH_ASKPASS=/bin/false ssh-keygen -y -f test_rsa < /dev/null will simply fail instead of asking for the password. Jun 29, 2017 at 8:48
9

If you open the private key file in text editor you will be able to see ENCRYPTED in the second line of the RSA/DSA key file. For example:

 -----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,373B504621779D53C72BADE597C531A0
3
  • Just verified on CentOS 7, looks good
    – daisy
    Jun 29, 2017 at 8:46
  • 8
    Unless you are using the new OpenSSH key format.
    – Jakuje
    Jun 29, 2017 at 9:21
  • 2
    And if you are using the new OpenSSH key format?
    – RichieHH
    Jan 9, 2020 at 20:44
2

I don't have enough reputation to comment, so I add here.

Thanks to @Ulrich-Schwarz , I extended his answer to check all my keys (the one i generated, so I have the puublic/private pair) in my $HOME/.ssh/ :

for l in $(ls $HOME/.ssh/*.pub)
do         key=${l%.*}
   if SSH_ASKPASS=/bin/false ssh-keygen -y -f $key < /dev/null > /dev/null 2>&1
   then
       echo -n "NO  password: "
   else
       echo -n "YES password: "
   fi
   echo ${key}
done
2
  • Adding the loop is useful. You swapped the 'YES' and 'NO' though; if the command is successful then no password is present.
    – Quantum7
    Mar 1, 2022 at 14:32
  • 1
    thanks @Quantum7 , now should be ok.
    – kidpixo
    Mar 3, 2022 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .