I use the ssh-keygen -t rsa to generate the RSA key pairs. I see that in the id_rsa.pub file, I see the username and the hostname.But I want to know that what elements will affects the key pairs. For example, if I change the host ip, need I regenerate the key pairs? And the hostname? or even I reinstall the operating system?When should I regenerate the key pairs?
What elements affects the key pairs generated by ssh-keygen?
ssh;ssh keygen
The key is randomly generated. There's nothing more special to its origin than that. Meaning that as long as only you hold the private key, there is no need to replace it.In SSH, a user key is generally used to identify a combination of user & origin system. Meaning the key is not shared among users on the same system, or the same user on multiple systems.Though this is not a technical limitation, as you can break either of these rules without issue. It's just good security practice.In a public key (id_rsa.pub), the last field is a comment. The ssh-keygen command typically puts your username & hostname as the last field. This serves no purpose other than as a comment to identify the key. As for why it puts username & hostname, see the previous paragraph.To directly answer your question, When should I regenerate the key pairs?:You should regenerate the key when the key has been compromised, and has potentially been obtained by someone else. At this time you should also revoke the previous public key from all remote systems which trust it (the authorized_keys file).That is literally the only reason. If you reinstall the OS, change hostname, etc, you do not need to recreate the key. Though you can if you'd like.