openssh - Adding an ssh key from putty to authorized_keys

Solution 1:

There is an accepted answer for this question, but I think it's worth noting that there is a way to do this using the ssh-keygen tool rather than sed:

ssh-keygen -i -f ssh2.pub > openssh.pub

Where ssh2.pub is your existing ssh2 key and openssh.pub will be the key in openssh format. If you just want to copy and paste you can leave out the redirect and use:

ssh-keygen -i -f ssh2.pub

Solution 2:

  • Remove the BEGIN and END lines
  • Optionally remove the Comment line (you can keep note of this if you want to add it as a comment later)
  • Remove all remaining line-breaks
  • Add the text "ssh-rsa" to the start of the line

The key now becomes:

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEApoYJFnGDNis/2oCT6/h9Lzz2y0BVHLv8joXMs4SYcYUVwBxNzqJsDWbikBn/h32AC36qAW24Bft+suGMtJGS3oSX53qR7ozsXs/DlCO5FzRxi4JodStiYaz/pPK24WFOb4sLXr758tz2u+ZP2lfDfzn9nLxregZvO9m+zpToLCWlXrzjZxDesJOcfh/eszU9KUKXfXn6Jsey7ej8TYqB2DgYCfv8jGm+oLVeUOLEl7fxzjgcDdiLaXbqq7dFoOsHUABBV6kaXyE9LmkbXZB9lQ== rsa-key-20160816

This one-liner will do all of the above, apart from append the comment (this assumes GNU sed):

sed key.pub -e 's/---- B.*/ssh-rsa /;/Comment:/d;'|sed ':a;N;$!ba;s/\n//g;s/---.*//'