5

I have been provided with an ssh key by a colleague to add to the authorized_keys file for an account on a linux server so they can access that account.

The file looks something like this:

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20160816"
AAAAB3NzaC1yc2EAAAABJQAAAQEApoYJFnGDNis/2oCT6/h9Lzz2y0BVHLv8joXM
s4SYcYUVwBxNzqJsDWbikBn/h32AC36qAW24Bft+suGMtJGS3oSX53qR7ozsXs/D
lCO5FzRxi4JodStiYaz/pPK24WFOb4sLXr758tz2u+ZP2lfDfzn9nLxregZvO9m+
zpToLCWlXrzjZxDesJOcfh/eszU9KUKXfXn6Jsey7ej8TYqB2DgYCfv8jGm+oLVe
UOLEl7fxzjgcDdiLaXbqq7dFoOsHUABBV6kaXyE9LmkbXZB9lQ==
---- END SSH2 PUBLIC KEY ----

The man page for authorized_keys (well, sshd) makes it clear that the file expects each key to take up a single line. So I guess I need to convert this key to a single-line format? How do I accomplish this?

9
  • 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

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/---.*//'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.