Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to move my file to another system which is located in some other place, with this command:

rsync -avrz  src destination

It works fine. But what I need is to put this command in shell script and run it like:

#! /bin/sh
rsync -avrz  srcfilelocation destination

When it runs, it asks for the destination system password. I know that password and give it manually.

Now I have decided to assign the password to an environment variable, like pswd="destination system password". I need my shell script to read the password from this variable. How can I write a script to do this?

share|improve this question
1  
I've noticed your accept rate is low. If possible, please consider closing some of your other open questions by selecting the best answer (if they have one). Consider which answers have been useful and need an upvote. We need users to maintain their questions so that the site can be an effective tool for the next person with your problems. For more details on best practices consider reading the FAQ. Also, please don't post the same question multiple times. –  hexafraction Jul 23 '12 at 11:39
add comment

1 Answer

You don't need to worry about passwords when you can use something called Public Key Infrastructure.

This is a method of using public and private keys to authenticate a user. You store a copy of your private key, and the other server has a copy of your public key. When you log in, they have a little conversation with each other which confirms the public and private keys match so you can log in without entering a password.

This is as secure as anything as long as you don't share your private key with anyone!

To set this up is really simple.

On source machine, run ssh-keygen. You can accept all the defaults, that'll be enough for this purpose. It'll generate your private (id_rsa) and public(id_rsa.pub) keys in your ~/.ssh folder.

Now you want to get the public key on the server, which is easy too.

From the source pc, run ssh-copy-id username@servername.

This will place a copy of id_rsa.pub's content in the server's ~/.ssh/authorized_keys.

Now if you ssh from the source to the destination you will get in without requiring a password.

How does this affect rsync I hear you ask? Rsync uses ssh!

Please remember: NEVER GIVE OUT YOUR PRIVATE KEY (id_rsa) or someone can pretend to be you.

share|improve this answer
    
Its not work in my ubuntu 10.04 system. After did all the above steps, since it ask password. –  Viswa Jul 10 '12 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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