How can I regenerate ssh keys of my openssh-server in Debian, Kali, Ubuntu or other dpkg based distros. Host keys can regenerated safely without affecting any ssh session.
Remove All Existing Host Keys
We need to clear our home so we will remove our host keys. Host keys reside in /etc/ssh by default.
$ rm -v /etc/ssh/ssh_host_*
Now we will use dpkg-reconfigure our package management tool to generate host keys. dpkg will process openssh-server as if installing newly and rerun all setup configuration procedure from scratch.
$ dpgk-reconfigure openssh-server
2048 SHA256:HNV1iyADYaJ1QLLHNcfVUHE2XXmkzZnLvBEenr2ARpY root@ubu1 (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:/P08pY6CdnEcTpkDTMuiVDgnBwvGUe7N4FwHMO1nGbQ root@ubu1 (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:hJdUHaOpQ2Kyvm1wZa12vQc7liSpzHikr3a/dRgE+P4 root@ubu1 (ED25519)