Converting SSH2 Public Keys for Use in authorized_keys
When working with SSH keys, you may encounter different formats depending on the tool that generated the key. For example, PuTTYgen and older Windows tools often produce keys in the SSH2 public key format, which looks like this:
|
1 2 3 4 5 6 7 8 9 10 |
---- BEGIN SSH2 PUBLIC KEY ---- Comment: "rsa-key-example" AAAAB3NzaC1yc2EAAAADAQABAAABAQDcExampleKeyData1234567890 abcdefghijklmNOPQRSTUVWXYZ1234567890abcdefghi zyxwvutsrqponmlkjihgfedcba0987654321ZYXWVUTSRQ ExampleKeyDataContinuesHere1234567890ABCDEFGHI JKLMNOPQRSTUVWXYZabcdefghijklmno9876543210pqrs TUVWXYZabcdefghijklmnopqrstu1234567890vwxyzAB ---- END SSH2 PUBLIC KEY ---- |
Linux and OpenSSH, however, expect a different format for the ~/.ssh/authorized_keys file. If you try to paste the above directly, SSH will reject it. Fortunately, converting between formats is straightforward. Step 1: Save the SSH2 Key Copy the entire block, including the BEGIN and END lines, into a file. For example:
|
1 2 |
nano ssh2key.pub |
Paste the contents, then save and exit. Step 2: Convert to OpenSSH Format Use the ssh-keygen tool to convert the key:
|
1 2 |
ssh-keygen -i -f ssh2key.pub > id_rsa_converted.pub |
This tells ssh-keygen to import (-i) the SSH2 key and write the OpenSSH version into id_rsa_converted.pub. Step 3: Inspect the Converted Key Open the new file:
|
1 2 |
cat id_rsa_converted.pub |
You’ll see a single line similar to this:
|
1 2 |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcExampleKeyData1234567890abcdefghijklmNOPQRSTUVWXYZ1234567890abcdefghi zuser@example |
Notice