Connecting to remote servers using SSH is very common in system
administration and involves entering the command : ssh
user@remotehostname and the corresponding password.
It is possible to use public keys with SSH to allow the remote host to accept SSH connections from you without password authentication. This can be
useful for automatically scheduled tasks (CRON jobs) for example when backing up data between servers. The commands below are entered into a command-line terminal.
On the local machine:
ssh-keygen
Answer the questions and then copy the public key to the remote machine:
scp /home/jonny/.ssh/id_rsa.pub jonny@remotemachine:/home/jonny
On the remote machine:
cat id_rsa.pub >> ~/.ssh/authorized_keys
On the local machine test it out (-v option gives verbose output for testing):
ssh -v jonny@remotemachine
You should be logged in without having to enter a password.
Combine this with an rsync script such as the one below and it makes for a simple backup system:
/usr/bin/rsync -avz jonny@novatech-ubuntu:/home/jonny/test/ /home/jonny/test/
This script will backup all files in the remote machine directory ‘test’ and copy them to the local machine’s (calling the script) directory ‘test’.