488

I've generated key pairs using PuTTYgen and been logging in using Pageant, so that I have to enter my pass-phrase only once when my system boots.

How do I achieve this in Linux? I've heard of keychain but I hear that it uses a different key pair format - I don't want to change my Windows keys and it would be nice if I could seamlessly connect in the same manner in both Windows and Linux.

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
TCSGrad
  • 11,324
  • 13
  • 46
  • 68
  • There shouldn't be any problems using the keys in linux as well... just copy them over and add the pub to authorized keys... though admittedly I don't entirely understand your question. – Randall Hunt Feb 08 '10 at 19:11
  • 17
    Nope , there is - I know this much that the key formats used by PuttyGen and ssh-agent are different , and they have to be converted explicitly - see Kaleb's detailed answer below. – TCSGrad Feb 09 '10 at 05:07

10 Answers10

738

puttygen supports exporting your private key to an OpenSSH compatible format. You can then use OpenSSH tools to recreate the public key.

  1. Open PuttyGen
  2. Click Load
  3. Load your private key
  4. Go to Conversions->Export OpenSSH and export your private key
  5. Copy your private key to ~/.ssh/id_dsa (or id_rsa).
  6. Create the RFC 4716 version of the public key using ssh-keygen

    ssh-keygen -e -f ~/.ssh/id_dsa > ~/.ssh/id_dsa_com.pub
    
  7. Convert the RFC 4716 version of the public key to the OpenSSH format:

    ssh-keygen -i -f ~/.ssh/id_dsa_com.pub > ~/.ssh/id_dsa.pub
    

See this and this for more information.

Charles
  • 48,924
  • 13
  • 96
  • 136
Kaleb Pederson
  • 43,537
  • 19
  • 96
  • 144
  • 12
    For those who get `It is required that your private key files are NOT accessible by others` error like I had run `cd ~/.ssh` and `chmod 700 id_rsa` – expert Oct 15 '12 at 17:48
  • 77
    You mean **chmod 600 id_rsa**. The file shouldn't need to be executable. :) – sig11 Nov 12 '12 at 19:44
  • 2
    This answer was the key to getting ssh from the windows command line using passwordless keys for me (specifically for git access). Would have saved me *hours* of pain if I had seen it earlier! Thanks! – cori Jan 25 '13 at 12:20
  • 2
    Can puttygen be executed from the CLI? – CMCDragonkai Nov 10 '14 at 10:03
  • 26
    I assume this is something new, but if you open your private key in puttygen, then it will automatically show you an OpenSSH compatible, copyable, public key string in the UI. – UpTheCreek May 03 '15 at 18:55
  • 1
    This is also helpful for users wanting to use Git for Windows as ssh client instead of PuTTY. Thanks! – Michael Murphy Aug 06 '20 at 12:38
174

If all you have is a public key from a user in PuTTY-style format, you can convert it to standard openssh format like so:

ssh-keygen -i -f keyfile.pub > newkeyfile.pub

References

Copy of article

I keep forgetting this so I'm gonna write it here. Non-geeks, just keep walking.

The most common way to make a key on Windows is using Putty/Puttygen. Puttygen provides a neat utility to convert a linux private key to Putty format. However, what isn't addressed is that when you save the public key using puttygen it won't work on a linux server. Windows puts some data in different areas and adds line breaks.

The Solution: When you get to the public key screen in creating your key pair in puttygen, copy the public key and paste it into a text file with the extension .pub. You will save you sysadmin hours of frustration reading posts like this.

HOWEVER, sysadmins, you invariably get the wonky key file that throws no error message in the auth log except, no key found, trying password; even though everyone else's keys are working fine, and you've sent this key back to the user 15 times.

ssh-keygen -i -f keyfile.pub > newkeyfile.pub

Should convert an existing puttygen public key to OpenSSH format.

slm
  • 12,534
  • 12
  • 87
  • 106
bukzor
  • 34,859
  • 10
  • 67
  • 104
  • 2
    Also, you may want to copy the user's comment from the `Comment:` line and paste it on the same line as the new key, separated with a space. I don't know why ssh-keygen won't do this by default. – Tobia Feb 06 '17 at 12:01
  • 3
    This gives me an error of: `decode blob failed: invalid format` – Chris Stryczynski Jan 10 '18 at 08:44
  • If you read the original article, the key detail is that this approach only works if you copy the key from puttygen when going through it's UI, it will not convert an already generated key from files produced by puttygen. – slm Apr 10 '18 at 14:44
  • Can I use the same tool to convert it the other way? – Olle Härstedt Sep 26 '18 at 09:18
  • yes, looks like you're looking at: ssh-keygen -e -f openssh_key.pub > putty.pub – Erik Sep 13 '19 at 17:27
  • It's funny that this is exactly what I was looking for, and also exactly what title is suggesting that question is about. This is not the answer to the question though. +1 for answering the title and helping me out :) – tishma Nov 21 '19 at 23:12
  • I get the same error as @ChrisStryczynski. Oddly, the file I'm looking at appears to be decimal? I'm assuming this was the file puttygen saved, not the text copied from the window. – vastlysuperiorman Dec 17 '19 at 21:11
153

Newer versions of PuTTYgen (mine is 0.64) are able to show the OpenSSH public key to be pasted in the linux system in the .ssh/authorized_keys file, as shown in the following image:

enter image description here

Zac
  • 4,076
  • 3
  • 33
  • 40
  • 7
    This answer covers how to set up key authentication on a remote that uses OpenSSH format. But in order to use a Putty-generated key on a Linux *client*, this answer is insufficient. I followed the accepted answer and it worked splendidly. – wberry Aug 08 '16 at 21:59
53

Alternatively if you want to grab the private and public keys from a PuTTY formated key file you can use puttygen on *nix systems. For most apt-based systems puttygen is part of the putty-tools package.

Outputting a private key from a PuTTY formated keyfile:

$ puttygen keyfile.pem -O private-openssh -o avdev.pvk

For the public key:

$ puttygen keyfile.pem -L

John Jawed
  • 641
  • 5
  • 3
  • If for some reason you MUST do this on a Windows box (cannot securely transfer all the keys to a *nix) and have so many keys using the GUI is cumbersome, try compiling the Unix source under Cygwin. That puttygen.exe will give you CLI "batch mode" like described above. – Toddius Zho Aug 22 '13 at 23:29
  • 2
    OSX: `brew install putty` – Juha Palomäki Nov 13 '16 at 12:04
  • 1
    That should be reversed: `puttygen inppk -O private-openssh -o outpem` and `puttygen inppk -L` (or `ssh-keygen -y -f outpem`) – dave_thompson_085 Dec 27 '16 at 08:02
29
sudo apt-get install putty

This will automatically install the puttygen tool.

Now to convert the PPK file to be used with SSH command execute the following in terminal

puttygen mykey.ppk -O private-openssh -o my-openssh-key

Then, you can connect via SSH with:

ssh -v user@example.com -i my-openssh-key

http://www.graphicmist.in/use-your-putty-ppk-file-to-ssh-remote-server-in-ubuntu/#comment-28603

Kamran
  • 773
  • 1
  • 6
  • 19
Purnendu Singh
  • 401
  • 4
  • 6
14

I recently had this problem as I was moving from Putty for Linux to Remmina for Linux. So I have a lot of PPK files for Putty in my .putty directory as I've been using it's for 8 years. For this I used a simple for command for bash shell to do all files:

cd ~/.putty
for X in *.ppk; do puttygen $X -L > ~/.ssh/$(echo $X | sed 's,./,,' | sed 's/.ppk//g').pub; puttygen $X -O private-openssh -o ~/.ssh/$(echo $X | sed 's,./,,' | sed 's/.ppk//g').pvk; done;

Very quick and to the point, got the job done for all files that putty had. If it finds a key with a password it will stop and ask for the password for that key first and then continue.

jfreak53
  • 2,067
  • 5
  • 33
  • 48
8

It's probably easier to create your keys under linux and use PuTTYgen to convert the keys to PuTTY format.

PuTTY Faq: A.2.2

Bradley Kreider
  • 1,093
  • 10
  • 16
6

I think what TCSgrad was trying to ask (a few years ago) was how to make Linux behave like his Windows machine does. That is, there is an agent (pageant) which holds a decrypted copy of a private key so that the passphrase only needs to be put in once. Then, the ssh client, putty, can log in to machines where his public key is listed as "authorized" without a password prompt.

The analog for this is that Linux, acting as an ssh client, has an agent holding a decrypted private key so that when TCSgrad types "ssh host" the ssh command will get his private key and go without being prompted for a password. host would, of course, have to be holding the public key in ~/.ssh/authorized_keys.

The Linux analog to this scenario is accomplished using ssh-agent (the pageant analog) and ssh-add (the analog to adding a private key to pageant).

The method that worked for me was to use: $ ssh-agent $SHELL That $SHELL was the magic trick I needed to make the agent run and stay running. I found that somewhere on the 'net and it ended a few hours of beating my head against the wall.

Now we have the analog of pageant running, an agent with no keys loaded.

Typing $ ssh-add by itself will add (by default) the private keys listed in the default identity files in ~/.ssh .

A web article with a lot more details can be found here

kovacsbv
  • 200
  • 3
  • 11
0

PPK → OpenSSH RSA with PuttyGen & Docker.

Private key:

docker run --rm -v $(pwd):/app zinuzoid/puttygen private.ppk -O private-openssh -o my-openssh-key

Public key:

docker run --rm -v $(pwd):/app zinuzoid/puttygen private.ppk -L -o my-openssh-key.pub

See also https://hub.docker.com/r/zinuzoid/puttygen

-2

Even faster than reopening puttygen, what I have often done is:

  1. Duplicate the public key file.
  2. In the copy, place the word "ssh-rsa " at the beginning.
  3. Remove the begin/end comment lines and all other line breaks.
  4. Save. The result is a one line key that works for openssh.
Ted Phillips
  • 171
  • 1
  • 7