Tampa
Tampa

Reputation: 78254

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

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?

Upvotes: 179

Views: 101269

Answers (8)

TheArchitecta
TheArchitecta

Reputation: 325

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]

Upvotes: 0

Daniel Little
Daniel Little

Reputation: 17263

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"

Upvotes: 48

Manumie
Manumie

Reputation: 156

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)

Upvotes: 12

Yang Young
Yang Young

Reputation: 622

$ printf '\n' | ssh-keygen -N ''

Upvotes: 0

GMaster
GMaster

Reputation: 1521

$ ssh-keygen -f $HOME/.ssh/id_rsa -t rsa -N ''

Upvotes: 23

Shamoon
Shamoon

Reputation: 43501

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

$ ssh-keygen -f id_rsa -t rsa -N ''

Upvotes: 249

mponceantolin
mponceantolin

Reputation: 3

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.

Upvotes: 0

ASHU
ASHU

Reputation: 214

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 ''

Upvotes: 13

Related Questions