I have an existing SSH key (public and private), that was created with ssh-keygen. How can I add a comment to this existing key?
3 Answers
Just add a space after the key and put in the comment, e.g.:
ssh-dss AAAAB3NzaC1kc3MAAACBAN+NX/rmUkRW7Xn7faglC/pxqbVIohbcVOt41VThMYORtMQr
QSqMZugxew2s9iX4qRowHWLBRci6404nSydLiDe1q6/NmpK+oQ8zD1yXekl+fruBAYeno7f6dM7c
2swwwXY6knp4umXkLItxIUki6SXM0WfabJ8BwuNDyA8IrbFAAAAFQCynEN3MYXbs4AA7E/1I03jb
B1rewAAAIAztzZUygrUI8XX6eE4zEHdTbv89AHYsAsf7fSAWnPxWc63dV0P5lCPNk58nze6+N+MD
X7ZQADT6710fvbOmEFLciTwBGHHLxIV+1iTApJSsQp9T+pdkbFzBZ+mqQamZpSN1hC8fXe/Uty0D
SbhnQ1qanwrOdKP1JV7DUgzehSfAAAAIEAwAyNYxUsGil46gZQea6sfhUnrBwyM6JnEbA6ogfGdS
T2TDn1U5rfTV9UuNHzfoZ4CplVHclXyUPPhbKqcedpuRPJhHN/lp5MH7Q2tI/UxHvmePNHrXKk86
XYt7RzKHjWbHRxf84GIyTlKa8yfNfFlf9oNXdtBXcsJjHIvNsBk= ThisIsAComment
The man page for sshd has a section on the authorized_keys format, where it states that the comment extends to the end of the line. While I haven't tried it, you should be able to put spaces into the comment.
Just start the line with a # sign, then the whole line will be treated as a comment
See the man page of authorized_keys
- 1,078
- 1
- 8
- 10
Just adding a comment to the end of the file may suffice in most cases, but the private key still contains the comment, and extracting the pubkey from it (like so) will still give the old one. As the question body states you have both the private and public key, you should consider updating both.
To do that, you can use ssh-keygen -c:
-c Requests changing the comment in the private and public key files. The program will prompt for the file containing the private keys, for the passphrase if the key has one, and for the new comment.
The usage hint is like so:
ssh-keygen -c [-a rounds] [-C comment] [-f keyfile] [-P passphrase]
And you can use it like so:
ssh-keygen -c -C "ThisIsAComment" -f ~/.ssh/ssh_key_path
which should correctly update both the public and private key.
- 230