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 the differences:
- It begins with
ssh-rsa - The key is one long Base64 string
- The comment from the original file is preserved (
zuser@example)
Step 4: Add to authorized_keys
Append the converted line into your server’s ~/.ssh/authorized_keys file:
|
1 2 |
cat id_rsa_converted.pub >> ~/.ssh/authorized_keys |
Ensure the file permissions are correct:
|
1 2 |
chmod 600 ~/.ssh/authorized_keys |
Conclusion
If you receive an SSH2-style public key, you can’t use it directly with OpenSSH. By using ssh-keygen -i, you can quickly convert it into the proper authorized_keys format and enable secure key-based authentication.
This simple conversion can save time when integrating keys generated by PuTTYgen or other legacy tools into your Linux environments.




