Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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...

#!/bin/bash
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
add comment

2 Answers

try executing the script with sudo ./script.sh 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
add comment

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
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.