for better understanding, I would recommend you to look into these two components from OpenSSH suite (you can do it by man ssh-keygen
and/or man ssh-copy-id
):
SSH-KEYGEN(1)
— authentication key generation, management and conversion
SSH-COPY-ID(1)
— use locally available keys to authorise logins on a remote machine
example (how to make key pair and copy it over to remote host):
SSH-KEYGEN(1)
:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/X/.ssh/id_rsa):
Created directory '/X/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /X/.ssh/id_rsa.
Your public key has been saved in /X/.ssh/id_rsa.pub.
The key fingerprint is:
e0:cd:fd:18:45:66:0d:11:a0:08:75:6a:f3:1a:6c:45 X@Z
The key's randomart image is:
+--[ RSA 2048]----+
| ... E ..B= |
| . = . + . |
| * o . |
| + B . . |
| = S o |
| . o + |
| . . . |
| |
| |
+-----------------+
$
&
SSH-COPY-ID(1)
:
$ ssh-copy-id root@Y
The authenticity of host 'Y (Z.Z.Z.Z)' can't be established.
RSA key fingerprint is 5e:8e:ad:71:77:6a:c4:16:e6:0e:34:f8:92:b2:ce:9f.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@Y's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@Y'"
and check to make sure that only the key(s) you wanted were added.
$
You then will be able to use scp
to copy without password prompt...
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.
before scheduling to cron, I'd do manual test as this way you know what to expect without waiting for scheduled time.
How do I know under what user ID / keys the cron job will attempt the
copy?
crond
usually runs under root, but to verify you can use following:
# ps aux | grep crond | grep -v grep
root 2696 0.0 0.0 126336 1712 ? Ss May13 0:01 /usr/sbin/crond -n
#
cron
job? Did you put it in/etc/cron.daily
or did you usecrontab -e
? Also, where is thepermission denied
coming from? Is it trying to execute the script, or is it logging in to the server, or is it trying to write a file on the remote system?root
then, which I'm guessing is not what you want.$USER
to a file). To get it to run as the right user, you should look at usingcrontab
as the desired user to schedule the script, or I suppose if you wanted to in the script running asroot
you could have it usesu
to run the command as the desired user, but that feels less good