Search code examples
linuxsshopenssh

SSH: How to replace an empty passphrase with a non-empty passphrase in script


In script I need to add a passphrase to a pirvate key that has empty passphrase.

I have just tried this:

echo asdf | ssh-keygen -p -f ~/.ssh/id_rsa

Solution

  • ssh-keygen has a command-line option to specify the new passphrase (and the old one, if the key already has a passphrase):

    ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
    [...]
    -p
    Requests changing the passphrase of a private key file instead of creating a new private key. The program will prompt for the file containing the private key, for the old passphrase, and twice for the new passphrase.

    -P passphrase
    Provides the (old) passphrase.

    -N new_passphrase
    Provides the new passphrase.