93

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?

Martin
  • 1,213
  • 1
  • 11
  • 10
  • 5
    To change the comment on the private key, use `ssh-keygen -c -f ~/.ssh/id_rsa` then follow the prompts. This comment is shown in the ssh-agent when you type `ssh-add -l` – sleblanc Oct 12 '19 at 12:51
  • 2
    Also you can use -C option. `ssh-keygen -c -C "comment" -f ~/.ssh/id_rsa` – hiroshi Jun 20 '20 at 07:46

2 Answers2

102

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.

kuttumiah
  • 107
  • 3
cjc
  • 24,916
  • 3
  • 51
  • 70
  • I've tried it and we will see if it works. My university requires ssh based login for some things and they said they use the comment (which must contain my username) matching an account to a key. Since I didn't wanted to create another SSH key I simply copied my public key, changed the comment and gave it to them. We will see if it works. – Martin Oct 29 '12 at 13:26
  • 8
    Well...it worked. – Martin Oct 29 '12 at 19:23
  • 2
    The part about it extending to the end of line is in the "SSH_KNOWN_HOSTS FILE FORMAT" section. "The keytype and base64-encoded key are taken directly from the host key; they can be obtained, for example, from /etc/ssh/ssh_host_rsa_key.pub. The optional comment field continues to the end of the line, and is not used." – Ceasar Jan 13 '17 at 20:57
  • 6
    Lines starting with `#` are treated as comments. See http://man.he.net/man5/authorized_keys. (I would post this as an answer but don't have enough reputation on this site) – cs01 Mar 30 '17 at 16:32
  • 5
    Comments can contain spaces. (Just tested.) – felwithe Mar 14 '18 at 21:18
78

Just start the line with a # sign, then the whole line will be treated as a comment
See the man page of authorized_keys

W4rlock
  • 968
  • 1
  • 7
  • 10