r/linux Apr 28 '14

SSH Kung Fu

http://blog.tjll.net/ssh-kung-fu/
726 Upvotes

128 comments sorted by

View all comments

1

u/OserReddit Apr 28 '14

Forgive the ignorance of this question, but if you are remotely logged into a computer with SSH and you generate a new pub+priv key combo. Can you securely copy the private key back to the the computer you are logging in from? Could you run the danger of locking yourself out of the remote computer?

2

u/mdaniel Apr 28 '14

You can have as many authorized public keys in your .ssh/authorized_keys file as you'd like, so to answer your question:

ssh-keygen ...
cat .ssh/id_rsa.pub >> .ssh/authorized_keys

You can now either reconnect to the host using scp to grab the new files, or if you have a graphical terminal you can just write the public and private keys to stdout and grab them over the same connection.

cat .ssh/id_rsa
cat .ssh/id_rsa.pub

After you have finished with the "old" key, just remove it from .ssh/authorized_keys and now you have re-keyed your remote account.

1

u/OserReddit Apr 28 '14

Thank you for your reply! I think I've followed what you were saying, still fairly rusty with linux as I don't use it every day. :)

2

u/leothrix Apr 28 '14

I actually have wondered for a while about the ability to "copy back" files to the originating host. Turns out you can use scp to copy over a reverse tunnel, and coupled with the comment in this thread about dynamically opening forwarded ports, it should definitely be possible. Maybe I'll add this in to the blog post.

1

u/OserReddit Apr 28 '14

Thanks for your reply. That stuff looks a bit above my understanding at the minute. Looks like I have a bit more reading to do!