2

I want to download the file from sever by using cron job, so I need to use scp command without giving password by keyword.

How do I do so?

4

First step is to generate a private-public key pair on the machine you copy from:

ssh-keygen

You can go with the defaults, and make sure you don't provide a password for usage. This can take a bit of time.

Second step, after the key generation has finished, is to copy the public key to the server using:

ssh-copy-id username@server

with the appropriate username and server. For this you need the password of username. If username doesn't have a password, you can copy the file with the public key via the root account, but it is much more easy to temporarily create a password for the account and use ssh-copy-id

Third step: test whether the installed public key is working using: ssh username@server or scp somefile username@server:/var/tmp

Fourth step: add a cron job (crontab -e) on the client machine:

15 23 * * * scp username@server:/path/on/server/downloadfile /local/destination
17 23 * * * scp /local/source/somefile username@server:/path/on/server/uploaddirectory

Which will copy the file /path/on/server/downloadfile every day at 11:15 PM from the server and two minutes later starts uploading /local/source/somefile

Things to consider:

  • Depending on the security of the access to the client machine (where you are copying from) and where the unprotected private key is located, you should consider changing the username account so that it only allows scp and not ssh.
  • If you copy the same file and the contents mostly change (e.g. appended to, not a complete rewrite), using rsync using the above ssh key pair) is more efficient.
  • Restricting the account is probably not doable, I expect there are other things to be done with this account beyond downloading the file. It's the key that should be restricted, with a command=… directive in ~/.ssh/authorized_keys. – Gilles Dec 17 '14 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.