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?
To generate a SSH keypair without being prompted for a passphrase you can do the following:
$ ssh-keygen -f id_rsa -t rsa -N ''
-
2Worked for me! Github always asked for a password without the -N option even when not entering a passwort after the promt "Enter passphrase (empty for no passphrase)" – Hollerweger Jun 9 '15 at 8:18
-
4On RHEL 6, I got this error: "
ssh-keygen: option requires an argument -- N
" :( – Anthony O. Dec 9 '15 at 15:02 -
1OK this was because I used
dzdo
command in front of it, so I had to write:dzdo -i -u target_user ssh-keygen -f id_rsa -t rsa -N "''"
– Anthony O. Dec 9 '15 at 15:09 -
1It appears that the space separating the option and the empty passphrase
-N ''
is significant. When I added the space it succeeded! – GH05T Nov 6 '18 at 7:11
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"
-
-
4This doesn't work for me. I get an error "Saving key "mykey" failed: passphrase too short (minimum four characters)". I had to switch the double/single quotes, e.g.
ssh-keygen -f $Name -t rsq -N '""'
. – Aaron Jensen Oct 3 '16 at 21:38 -
1
-
-
@Daniel-Little I can't find a module PoshGit, the only one I find with this name is posh-git which does not seem to have this cmdlet. Any more info on it? – Jim Mar 27 '20 at 13:17
$ ssh-keygen -f $HOME/.ssh/id_rsa -t rsa -N ''
-
-
@oᴉɹǝɥɔ The answer by Shamoon does not explicitly specify the path to the private key. – GMaster Mar 22 '18 at 2:09
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 ''
-
3As far as I understood
id_rsa
is the default file name for an RSA key, so if you wants to stick to the default, you are not forced to explicitly use the-f
option. – danidemi Aug 8 '15 at 10:14 -
4@danidemi while you're right that it's the default, one reason for specifying the
-f
option is so that you can execute this in a script. You can just reaffirm the default to disable the subsequent prompt. – Tristan Reid Oct 17 '15 at 6:49
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
)
-
-
ssh-keygen -q -t ed25519 -f id_ed25519 -N ''
does not work for me,Windows PowerShell in Admin mode on Windows 10 Home. I can only get it to run withssh-keygen -t ed25519 -f id_ed25519
, which will ask meEnter passphrase (empty for no passphrase):
twice. How can I avoid having to press any enter button? – questionto42 Mar 18 at 20:31
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.