17

I'm facing problem when I pull a file from another machine to my machine using SCP. File is successfully transferred but each time it asks for password. I want to run this SCP command as a cronjob, How can I save password for this automation?

scp [email protected]:/usr/etc/Output/*.txt /usr/abc/  
[email protected]'s password:
Noam M
  • 441
  • 1
  • 7
  • 17
Nishat
  • 333
  • 2
  • 3
  • 8

3 Answers3

25

You can do:

  1. (if not already done) generate a set of public and private ssh keys on your machine for your user with:

    $ ssh-keygen

    Answer the questions in order to generate the set of keys.

  2. copy your public key to the remote host:

    $ ssh-copy-id remote-user@remote-host

    This will enable login-in from your username@host to remote-user@remote-host without being prompt with p/w authentication.

Noam M
  • 441
  • 1
  • 7
  • 17
fduff
  • 4,925
  • 4
  • 35
  • 39
  • what do you mean in step 2? you mean to copy the created file in step one to the machine from which I want to copy a file – Mohsen El-Tahawy Oct 27 '17 at 17:25
  • @MohsenEl-Tahawy, `ssh-copy-id` will take care of copying the encrypted credentials onto the remote host, that way it won't prompt for p/w next time that user `ssh` onto it. – fduff Oct 30 '17 at 08:28
9

you can use this: sshpass -p 'your_password' scp [email protected]:/usr/etc/Output/*.txt /usr/abc/ but remember that you store password in plaintext.

1

My version of ssh-copy-id requires '-i' option to work, or else it will assume copying credentials from ssh-agent, which will not work if you are not using it.

ssh-copy-id -i $myfile.pub remote-user@remote-host
Kostya Berger
  • 91
  • 1
  • 2