15

I want to set up an RSync job that would connect through SSH.

I have my computer ( backup@myhost ) and the remote host ( test@remhost ) and I need to backup the folder ~/something with all it's contents. The ssh user test only has READ access to all files and folders in the ~/ folder. I want to use rsync to copy the test@remhost:~/something folder to the backup@myhost:~/bak folder.

For this purpose I use the following command via BASH on Ubuntu 11.10 (Oneiric):

rsync -avz -e ssh test@remhost:~/something/ ~/bak/

After hitting enter I get this:

test@remhost's password:

I type the password and the rsync works.

I want to make the above command to automatically input the password and pass it as a parameter or to input it automatically and start the job.

I tried executing rsync -avz -e ssh test:password@remhost:~/something/ ~/bak/ but it still asks for the password and it's annoying.

I don't want to hear about any kind of keys (RSA,DSA or any other). I just want a simple command that would log me in and do the job.

EDIT: A possible scenario could be, if public key authentication is disabled and you can't change this. E.g. if you use OpenSSH, you'd need root privileges on the server in order to edit the file sshd_config and to add PubkeyAuthentication yes.

EDIT: This is what finally worked for me:

sshpass -p 'sshpassword' rsync --progress -avz -e ssh test@remhost:~/something/ ~/bak/

Please note that this method is not considered secure as it sends the password in plain text and it's vulnerable to man-in-the-middle attacks. It is advised to use the key athentication for a stronger security.

14
  • 4
    You won't get any answers if you tell people not to tell you the answer.
    – Mike Scott
    Jan 5, 2012 at 11:32
  • 8
    This is pointless. What's the problem with the key authentication? If key based authentication is configured, then from that point you have only a simple command which does the job, automatically, exactly that you want. So I can't see what's the problem with this. Storing the password somewhere to specify it "automatically" is one of the biggest security hole I can imagine, what's the point to do that?
    – LGB
    Jan 5, 2012 at 12:01
  • I don't need anyone to give me lectures on security. I already know the implications. I just have to leave everything on the server as is and not change anything. For your Information I already tried that with keys and it still asks me for the password therefore your argument is invalid! Jan 5, 2012 at 12:10
  • 1
    No, for good reasons there is no such parameter by default. Look at Migs' answer if you want such a parameter.
    – lumbric
    Jan 5, 2012 at 12:27
  • 2
    @Sorin Mihai Oprea: Well, if it does not work for you, it's bad? Interesting. For your information: as an internet system engineer working at an ISP we use key based authentication with ssh on more than 100 servers. It works for us, interesting. Don't call something bad just because you don't understand how it works and how it must be configured. If you are right, it would not work for anyone which would attract some attention as a major ssh server implementation bug :) So my agrument is not invalid, just you can't configure it well for some reason. It's not the same, sorry.
    – LGB
    Jan 5, 2012 at 12:53

6 Answers 6

39

maybe try sshpass.

seems simple enough to use... it's available through apt as well.

I was looking for something like this before I copied my keys around, but since I've got my key everywhere I need already anyway, I haven't taken the time to try this.

Do note the disclaimer on that tutorial there regarding the visibility of your password though.


For everyone who needs to do this:

sshpass -p 'sshpassword' rsync --progress -avz -e ssh test@remhost:~/something/ ~/bak/
3
  • You are most welcome. While I agree with the rest of the folks here that public-key-authentication is the best solution for this matter, sometimes alternative methods are needed, so long as we're aware of the trade-offs when using alternatives.
    – Migs
    Jan 5, 2012 at 13:41
  • Indeed ... but that's why it's called open source right? Multiple flavors ... same result! Jan 5, 2012 at 14:03
  • 2
    THANK YOU for not just saying passwords are bad dont use them, some of us on OLDDDD legacy equipment just need these to get by until the new equipment arrives :) Apr 9, 2019 at 20:05
8

A variation on your solution which is more secure to security threats is to store your password in a file with tight permissions and use the -f flag with shpass:

sshpass -f '/home/me/.password' rsync --progress -avz -e ssh
test@remhost:~/something/ ~/bak/

The difference is that listing the running processes will not show your password in the command line, it will now only show the path to the file in which your password is.

1
  • This answer better than the accepted answer. Jul 16, 2018 at 9:31
1

I can't imagine any situation where public-key authentication without passphrase wouldn't be the better solution for autmated ssh/rsync logins.

Anyway expect should be a way to achieve what you want to do. You can't pipe the password to ssh, but this is something very similar. How to do so, is answered here at stackoverflow.

1
  • Thank you for the suggestion but I actually found sshpass a lot more simpler! Jan 5, 2012 at 13:38
1

I found that sshpass works, but since the termanal says SSHPASS read: Enter passphrase for key '/home/ubuntu/.ssh/id_rsa': you need to run it something like this:

sshpass -P passphrase -p 'password' rsync /path/to hostname:/destination/path -av --progress -e ssh

Where -P specifies which prompt to look for I found that out by running sshpass -v and finding out its looking for the phrase password which isn't there.

1

This also works:

system <<~ "RSYNC";
    rsync \\
      --rsh="sshpass -p '$password' ssh -p 22" \\
      --archive \\
      --verbose \\
      --progress \\
      --partial \\
      --human-readable \\
      "$directory" \\
      "$options{host}:${directory}"
RSYNC

ie. passing sshpass in --rsh instead of in front of rsync

This method alongside with the usage of the SSHPASS environment variable is described in the Examples section of the manual:

https://www.freebsd.org/cgi/man.cgi?query=sshpass&sektion=1

0

the first thing is do ssh without passowrd/bypass the password login, as u can see we can use ssh-copy-id -i ./ssh/id_rsa.pub to target machine.

After that test the machine Can be remotely by ssh without password, then rsync in on the way next of course from local machine into target/server machine

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .