Use PPK file in Mac Terminal to connect to remote connection over SSH

MacosSshSsh KeysOpenssh

Macos Problem Overview


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).

Macos Solutions


Solution 1 - Macos

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

Solution 2 - Macos

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

Solution 3 - Macos

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnandView Question on Stackoverflow
Solution 1 - MacosPurpletoucanView Answer on Stackoverflow
Solution 2 - MacosrinkeshView Answer on Stackoverflow
Solution 3 - MacosdasfrostyView Answer on Stackoverflow