171

I have been using Putty on Windows XP and used the .ppk file to connect to my Linux servers (several servers).

On the servers I have the following folder and file ~/.ssh/authorized_keys

I now want to use a Mac to connect via the terminal. I have set up the remote connections to the servers manually and want to know how I can setup using the ppk file or the private/public key within it.

Please note : I already am using private/public key login from Windows so I do not need to create a new key using keygen, I just want to know how to set up now that I have the keys already. (In other words, I already have the authorized key lists on the server, and the public and private key).

Nicole
  • 32,841
  • 11
  • 75
  • 101
Anand
  • 4,182
  • 6
  • 42
  • 54

3 Answers3

442

You can ssh directly from the Terminal on Mac, but you need to use a .PEM key rather than the putty .PPK key. You can use PuttyGen on Windows to convert from .PEM to .PPK, I'm not sure about the other way around though.

You can also convert the key using putty for Mac via port or brew:

sudo port install putty

or

brew install putty

This will also install puttygen. To get puttygen to output a .PEM file:

puttygen privatekey.ppk -O private-openssh -o privatekey.pem

Once you have the key, open a terminal window and:

ssh -i privatekey.pem user@my.server.com

The private key must have tight security settings otherwise SSH complains. Make sure only the user can read the key.

chmod go-rw privatekey.pem
Piran
  • 7,180
  • 1
  • 24
  • 37
Purpletoucan
  • 6,472
  • 2
  • 21
  • 28
  • 1
    sudo port ?? that surely does not exists... at least in Mavericks – Pedro Luz Oct 09 '13 at 09:48
  • @Narven Please refer to: http://www.macports.org/ – Henry Cho Oct 30 '13 at 03:30
  • 23
    You may also install putty using [brew](http://brew.sh/): `brew install putty`. Note that we're not using `sudo` here ;) – GabLeRoux Jan 20 '14 at 22:56
  • 2
    Useful answer!! Thanks for that!! For those who does not have port installed, can follow following link to install git: http://codingsteps.com/installing-and-using-putty-on-mac-os-x/ – Viraj Jan 28 '14 at 13:26
  • 3
    What does this error mean when I try to run the puttygen command? Enter passphrase to load key: Assertion failed: (random_active), function random_byte, file ./../sshrand.c, line 313. Abort trap: 6 – fastasleep Jul 18 '14 at 01:12
  • 6
    You need to install the brew formula from HEAD so please `brew uninstall` first then use `brew install putty --HEAD`. This worked for me. – Dokie Aug 08 '14 at 09:41
  • If you're a developer and you don't have macports, there's a quick and easy Xcode project [here](https://github.com/eldridgegreg/puttygen-osx) that builds puttygen for you - it only took about 30 seconds to download and build. – Carlos P Feb 24 '15 at 09:05
  • NOTE: You have to run puttygen as ´sudo´ to allow access for cryptographic randomness generator. – Manuel Arwed Schmidt Jun 11 '15 at 21:12
  • For passphrase protected .ppk's, puttygen will preserve the passphrase and apply encryption to the output key. Very nice! – Chris Betti Oct 22 '15 at 18:58
  • Thanks man, this works in linux : puttygen privatekey.ppk -O private-openssh -o privatekey.pem – JRichardsz Jun 04 '16 at 11:36
  • 1
    It worked for me perfectly in Mac OS X! Thanks. – Supratim Roy Jan 02 '17 at 09:00
  • Why not use `sudo apt-get install putty` instead? – LZH Feb 02 '17 at 15:37
  • I'm navigating to my key's directory and changing the chmod to 400, but that still wasn't working (unprotected private key file! error). So I checked with `ls -l` and saw that nothign was changed (pem is still set at 777, even after doing a chmod 400 on it). Any ideas what could be causing this in mac? – Kyle Vassella Apr 09 '17 at 21:01
40

Convert PPK to OpenSSh

OS X: Install Homebrew, then run

brew install putty

Place your keys in some directory, e.g. your home folder. Now convert the PPK keys to SSH keypairs:cache search

To generate the private key:

cd ~

puttygen id_dsa.ppk -O private-openssh -o id_dsa

and to generate the public key:

puttygen id_dsa.ppk -O public-openssh -o id_dsa.pub

Move these keys to ~/.ssh and make sure the permissions are set to private for your private key:

mkdir -p ~/.ssh
mv -i ~/id_dsa* ~/.ssh
chmod 600 ~/.ssh/id_dsa
chmod 666 ~/.ssh/id_dsa.pub

connect with ssh server

ssh -i ~/.ssh/id_dsa username@servername

Port Forwarding to connect mysql remote server

ssh -i ~/.ssh/id_dsa -L 9001:127.0.0.1:3306 username@serverName
Piran
  • 7,180
  • 1
  • 24
  • 37
Rinkesh
  • 3,150
  • 28
  • 32
22

There is a way to do this without installing putty on your Mac. You can easily convert your existing PPK file to a PEM file using PuTTYgen on Windows.

Launch PuTTYgen and then load the existing private key file using the Load button. From the "Conversions" menu select "Export OpenSSH key" and save the private key file with the .pem file extension.

Copy the PEM file to your Mac and set it to be read-only by your user:

chmod 400 <private-key-filename>.pem

Then you should be able to use ssh to connect to your remote server

ssh -i <private-key-filename>.pem username@hostname
dasfrosty
  • 369
  • 2
  • 5
  • 20
    `There is a way to do this without installing putty on your Mac. You can easily convert your existing PPK file to a PEM file using PuTTYgen on Windows.` So the best way to do it on mac is to do it on windows?! – Roee Gavirel Apr 23 '17 at 11:06
  • 12
    Actually @sigi my answer was genuinely intended to be helpful. The primary goal of the question as I understand it is to connect from a Mac to a Linux server using an existing .ppk file from a Windows machine. Given the question asker has access to a Windows machine then converting the .ppk file to a .pem file prior to copying it to the Mac is just as valid a solution as first copying it to the Mac and converting it there. I suggested this approach because some people may prefer not to install additional software on their Mac that they don't otherwise need. – dasfrosty Sep 08 '17 at 05:05
  • 4
    This answer is very helpful, for exactly the defended reason: Someone coming from Windows to Mac with an existing ppk file is more likely to want to export from Windows as opposed to installing PuTTY on the Mac for no particular purpose. This is exactly what I chose to do. – trevorsky Jul 02 '18 at 14:38