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

8

177

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?

Ballerina answered 26/5, 2012 at 15:28 Comment(1)
Here is an alternative method: You can automate interactive ssh prompts like thisPinot
R
247

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

$ ssh-keygen -f id_rsa -t rsa -N ''
Ripuarian answered 18/2, 2013 at 22:45 Comment(4)
Worked 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)"Roister
On RHEL 6, I got this error: "ssh-keygen: option requires an argument -- N" :(Trinity
OK 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 "''"Trinity
It appears that the space separating the option and the empty passphrase -N '' is significant. When I added the space it succeeded!Similar
G
46

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"
Gillum answered 2/11, 2013 at 0:15 Comment(8)
Thanks for this. BTW, git\usr\bin is the correct dir.Guarnerius
This 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 '""'.Pastoralize
@AaronJensen same for me. Thanks for the fix. I used '""'Purview
@AaronJensen Thanks! '""' is the only variant which worked for me.Quechuan
@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?Sulphate
I have searched for this for too long, I had overread this answer. I did not have the idea that Windows PowerShell would be different from other command prompts. This special case must have its own Q/A.Mencher
The first and Second line of this Answer worked for me. except I had an underscore in my file name But why does this answer contain \git\bin, what does git have to do with the question/answer.Blevins
if you are parameterising your Powershell Script then... -f $HOME/.ssh/$keyName`_$keyType would be used... `_ required to escape the _Blevins
J
21
$ ssh-keygen -f $HOME/.ssh/id_rsa -t rsa -N ''
Juggle answered 9/10, 2016 at 1:29 Comment(2)
This answer is essentially copy and paste from the answer by ShamoonFingered
@oᴉɹǝɥɔ The answer by Shamoon does not explicitly specify the path to the private key.Juggle
R
13

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 ''
Rehearing answered 16/12, 2014 at 9:29 Comment(2)
As 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.Mighty
@Mighty 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.Austria
T
12

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)

Tachygraphy answered 20/4, 2018 at 11:21 Comment(2)
Worked for me thanks. (OpenSSH_7.6p1 Ubuntu-4ubuntu0.2)Marisolmarissa
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 with ssh-keygen -t ed25519 -f id_ed25519, which will ask me Enter passphrase (empty for no passphrase): twice. How can I avoid having to press any enter button?Mencher
K
0
$ printf '\n' | ssh-keygen -N ''
Kit answered 9/8, 2017 at 4:11 Comment(0)
S
0

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.

Sarsenet answered 23/8, 2017 at 14:44 Comment(0)
B
0

Please Enjoy this script... Powershell script (e.g. for github)

paste this into your myscript.ps1 file...

param(
    [Parameter(Mandatory)]
    [string]$keyName=$(throw "keyName - Param must be supplied"),
    [Parameter(Mandatory)]
    [string]$email=$(throw "email - Param must be supplied"),
    $u="git",
    $d="github.com",
    $c="rsa"
)
$repo="repoName"
$account=":accountName"
$currentDir = Get-Location
Write-Host $HOME/.ssh/$keyName`_$c
mkdir $HOME/.ssh
Set-Location $HOME/.ssh
ssh-keygen -f ./$keyName`_$c -t $c -C $email -N '""'
Add-Content -Path ./config -Value "
Host $keyName
    User $u
    Hostname $d
    PreferredAuthentications publickey
    IdentitiesOnly yes
    IdentityFile $HOME/.ssh/$keyName`_$c"
Write-Host "`n
Put this key into $d :"
cat $HOME/.ssh/$keyName`_$c.pub
Write-Host "`n
Use this to Clone the $repo repo :
    git clone $u@$keyName$account/$repo.git"
cat config
Set-Location $currentDir

above is untested but it is close to what I have working

Command to execute

> myscript.ps1 -keyName yourname -email [email protected]
Blevins answered 15/6, 2022 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.