Linux – scp to copy file to remote server fails because of permissions

linuxscpssh

I think my fundamental problem is that I don't fully understand how ssh / scp works. But I don't what to google to try to read up on my question.

Problem Description

I have created a bash script on serverA. It tries to scp a file from serverA to serverB. This script will be scheduled as a cron job.

To test whether this script will work, I just tried to manually scp the file from serverA to serverB.
But I'm getting a "Permission denied" error message on serverA.

I logged into serverA in the first place like this:

mycomputer#> ssh root@serverA.mydomain

Questions:

Is the manual scp test even a good idea? Because I'm logging in using my key into serverA but ultimately, it's the cron job that's going to trigger to scp command.

How do I know under what user ID / keys the cron job will attempt the copy?

What should I google to better understand how this all works?

Thanks.

Best Answer

If you want to automate something like this you will need to setup public / private keys on either side and then configure ssh for authorized keys in the direction you want to copy. Assuming you are using OpenSSh do the following on both systems:
ssh-keygen -t dsa
cd ~/.ssh
cp id_dsa identity
cp id_dsa.pub authorized_keys
cp id_dsa.pub systemname_id_dsa.pub

Now you will need to copy the last file from the source system to the target system's ~/.ssh directory and then run the following command:
cat systemname_id_dsa.pub >> authorized_keys

You should now be able to ssh / scp from source to target without a password for this user. I would suggest not doing this as root, but as another user as it opens up possible security issues when done as root.