I'm reinstalling my Linux desktop, how do I keep SSH logins working?

You need to back up your private keys, at the very least. They cannot be regenerated without having to replace your public key everywhere. These would normally have a name starting with id_ and no extension.

The public keys can be regenerated with this command: ssh-keygen -y -f path/to/private/key. Your user configuration (a file called "config") could also be useful if you have set any non-defaults.

All of these files would normally be in ~/.ssh, but check first!


For outgoing SSH connections, backing up your key pair is enough. For convenience, you might just backup your ~/.ssh directory and restore it onto the new installation.

Normally backing up and restoring that directory is enough, but it is possible to store the keys elsewhere by either using custom settings in ~/.ssh/config or /etc/ssh/ssh_config, or by using a SSH-agent and ssh-add to pick up the keys to be used on outgoing connections from an arbitrary location.

If you have incoming SSH connections, you might also consider backing up the SSH host keys at /etc/ssh/ssh_host_*. If you don't backup these, new host keys will be automatically generated by sshd as it starts for the first time on the new installation, and any incoming SSH connections will show the "SSH host key mismatch, someone may be doing something bad!" error message and usually reject the connection unless someone deletes the old host key on the SSH client and explicitly accepts the new one.

It sounds like you might not have incoming SSH connections on your Linux desktop system, so I'm mentioning this only for the sake of completeness. But on server systems, or if you have significant SSH-based automation set up, acknowledging a changed host key might be a major hassle.

Tags:

Ssh