Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm migrating some home folders from one Linux box to another using scp. I've saved my public key into the destination box's authorized_keys file and the following command works perfectly when I type it at the command line:

scp myArch.tar root@myOtherServer:/root/dumps

... however when I put this into a script like so...

tar -cf /tmp/$1.tar $1; wait
scp /tmp/$1.tar root@myOtherServer:/root/dumps

.. and call it by passing a folder name as a parameter, I get the following error.

[root@myHost home]# /root/migrate myHomeFolder
Permission denied (publickey).
lost connection

Any ideas what could be the problem?

share|improve this question
Can you add an echo $HOME to the script? I wonder if the script doesn't have the environment variable to let it know where .ssh is. – Paul Oct 9 '11 at 11:23
I assume you meant export $HOME - still the same error – Xoundboy Oct 9 '11 at 11:27
I meant "echo" so you could take a look at the value the script thought it was set to. But forcing the issue works just as well. I think it might be time to run verbose logging on the server. – Paul Oct 9 '11 at 11:57
You could also use the -i parameter on the scp command to make absolutely sure it is sending the right private key, and -v to see if there were any issues with it – Paul Oct 9 '11 at 11:59

try executing the script with sudo ./ may be because you are writing to /root

share|improve this answer
Nope. That doesn't work...any why would it anyway? sudo only applies to the local machine - the problem here is the connection to the remote – Xoundboy Oct 9 '11 at 10:50

It's most likely due to the shebang starting a new instance of the bash shell, in which the ssh-agent hasn't been set up. To make sure, you can add the following line to the top of your script:

ssh-add -l >> $HOME/ssh.log 2>&1

run the script and check what you get in ssh.o

share|improve this answer

You must log in to answer this question.

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