Powered by coANDco UK

Howto use scp without password prompt (batch mode, scp -B)

From How2s

Assume you want to use a script/cron job that transfers data via scp from server AAA to server BBB, you would not want to have the password prompt all the time.

You need access to both servers, then exchange the public keys, in order to be able to access server BBB from server AAA without being prompted for the password.

This is how it works. Assume you are user FOO on server AAA and user BAR on server BBB.

Log into server AAA as FOO and enter

ssh-keygen -d

do not enter any passphrase. This will generate a couple of files in the folder ~/.ssh/

SCP the file rsa_id.pub (this is your public key) to the SERVER BBB:

scp ~/.ssh/id_dsa.pub BAR@BBB:/home/BAR/.ssh/authorized_keys2

ATTENTION: if authorized_keys2 already exists on server BBB, append the content of id_dsa.pub to the existing file!

Do the same vice versa (if you want access to both)

This now allows you to transfer files from one server to the other without being asked for the password. For example:

FOO@AAA:/my/project/folder$ find . -type f -ctime -10 -print -exec scp -B '{}' BAR@BBB:/my/project/folder/'{}' ';'

this syncs me all files in folders and subfolders created within the last 10 days to the same folder structure from server AAA to server BBB

Please check out our sponsor, thanks to whom How2s.org is FREE: