Automate ssh-keygen -t rsa so it does not ask for a passphrase

后端 未结 7 1659
臣服心动
臣服心动 2020-11-30 23:25

I need to automate ssh-keygen -t rsa with out a password i.e. enter at the prompt.
How can I do that from a shell script?

相关标签:
7条回答
  • 2020-11-30 23:35
    $ printf '\n' | ssh-keygen -N ''
    
    0 讨论(0)
  • 2020-11-30 23:38

    I needed to automate in a bash script the ssh-keygen command and the final answer which works well to me:

    echo -e "\n" | ssh-keygen -N "" &> /dev/null
    

    The echo command with the -e interprets "\n" as an Enter key, but do not work with the passphrase. Then using the option -N "" (empty passphrase) the password will be empty and will not ask for anything. &> /dev/null will send the 'stdout' and 'stderr' to /dev/null so nothing is printed through the display.

    0 讨论(0)
  • 2020-11-30 23:40

    What about :

    ssh-keygen -q -t rsa -f ~/.ssh/id_rsa -N ''
    

    As noted in man ssh-keygen :

    SYNOPSIS
         ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1] [-N new_passphrase] [-C comment] [-f output_keyfile]
    (...)
          -q      Silence ssh-keygen.
    

    (that is with openssh-client package in Debian 9.4 stretch : OpenSSH_6.7p1 Debian-5+deb8u4)

    0 讨论(0)
  • 2020-11-30 23:45

    If you need to do this from PowerShell in windows use:

    ssh-keygen -f $Name -t rsa -N '""'
    

    note you also have to ensure the git bin directory is in your path:

    $sshPath = "<path>\git\bin\"
    
    $env:path += ";$sshPath"
    

    Then to use it in PoshGit it's just:

    Add-SshKey "<path>\.shh\KeyFilename"
    
    0 讨论(0)
  • 2020-11-30 23:53

    Just a correction to answer 2... I found out on my OL and RHEL system the file name should be id_rsa not id.rsa.

    So on a OL or RHEL system the command would be:

    $ ssh-keygen -f id_rsa -t rsa -N ''
    
    0 讨论(0)
  • 2020-11-30 23:57

    To generate a SSH keypair without being prompted for a passphrase you can do the following:

    $ ssh-keygen -f id_rsa -t rsa -N ''
    
    0 讨论(0)
提交回复
热议问题