Sometimes I want to run commands on one of my computers from another, for example I want to execute my server's backup script from my desktop PC. This is not only out of convenience, but sometimes I can't get to my server for various reasons. OpenSSH allows you to securely run commands on remote computers, so it seemed perfect for what I needed. I was shocked at how easy it was to set up and I could soon run my server's backup script securely from my PC, even without a password. Anyway, if you want something similar, here's how to do it.
Make sure the
openssh-server package is installed, as this allows the server to listen for connections from remote computers, in this case the PC. Amazingly enough, that's the server set up, unless of course you need to open firewall ports, etc.
Moving onto the PC, I found OpenSSH to be installed by default on most Linux distributions, but you might want to check. Assuming it is, you can create a key with or without a password. The only difference is that you should leave the passphrase empty if you don't want one.
$ ssh-keygen -t rsa
When the key is generated, you can install it on the server with the following command:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
That's it! Now you can log into the server and execute any command as if you were sitting in front of it! To login, use the command:
$ ssh [email protected]
You can also pass a command or script to execute, so the session doesn't stay open like in the command above. So that backup script I wanted to execute could be executed with the command:
$ ssh [email protected] ~/bin/backup
Of course, you could always repeat the process on other computers to allow them access to the server too, or you could install the
openssh-server package on the PC to allow the server to access the PC. Just make sure the private keys are kept safe, as this is what secures the connection.