
Use an arbitrary key file with SSH, Scp, Rsync and Ansible
Connections to cloud-hosted servers are typically secured using an RSA Private Key rather than password. If you have the key saved to a file on a local machine, here is how to use it with SSH, Scp, Rsync and Ansible
SSH
To use SSH to perform a remote login, use the -i
parameter to specify a path to the local key file, like:
ssh -i /path/to/file-with-private.key remote-user@remote-ip-or-hostname
Scp
To use Scp to securely copy a file to or from a host, use the -i
parameter again. For example, to copy file.txt from your remote host to your local current directory:
scp -i /path/to/file-with-private.key remote-user@remote-ip-or-hostname:/remote-path/file.txt ./
Or, to copy file.txt from your local current directory to a remote host:
scp -i /path/to/file-with-private.key ./file.txt remote-user@remote-ip-or-hostname:/remote-path/
Rsync
Rsync, to synchronise with a remote filesystem, requires passing an SSH environment variable, like this:
rsync -avz -e "ssh -p1234 -i /path/to/file-with-private.key" /local/directory remote-user@remote-ip-or-hostname:/remote/directory
Ansible
To enable management of your remote host using Ansible, edit your /etc/ansible/hosts
file and append one line per remote host requiring a key file, as per the following format:
remote-host-or-ip ansible_user=remote-user ansible_ssh_private_key_file=/path/to/file-with-private.key