mles
mles

Reputation: 3476

rsync with ssh without using credentials stored in ~/.ssh/config

I have a script that transfers files. Everytime I run it It needs to connect to a different host. That's why I'm adding the host as parameter.

The script is executed as: ./transfer.sh <hostname>

#!/bin/bash -evx

SSH="ssh \
-o UseRoaming=no \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-i ~/.ssh/privateKey.pem \
-l ec2-user \
${1}"

files=(
  file1
  file2
)

files="${files[@]}"

# this works
$SSH

# this does not work
rsync -avzh --stats --progress $files -e $SSH:/home/ec2-user/

# also this does not work
rsync -avzh --stats --progress $files -e $SSH ec2-user@$1:/home/ec2-user/

I can properly connect with the ssh connection stored in $SSH, but the rsync connection attempts fails because of the wrong key:

Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.2]

What would be the correct syntax for the rsync connection?

Upvotes: 1

Views: 1003

Answers (2)

mles
mles

Reputation: 3476

My solution after Jakuje pointed me in the right direction:

#!/bin/bash -evx

host=$1

SSH="ssh \
-o UseRoaming=no \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-i ~/.ssh/privateKey.pem \
-l ec2-user"

files=(
  file1
  file2
)

files="${files[@]}"

# transfer all in one rsync connection
rsync -avzh --stats --progress $files -e "$SSH" $host:/home/ec2-user/

# launch setup script
$SSH $host ./setup.sh

Upvotes: 1

Jakuje
Jakuje

Reputation: 25986

Write set -x before the rsync line and watch how the arguments are expanded. I believe it will be wrong.

You need to enclose the ssh command with arguments (without hostname) into the quotes, otherwise the arguments will get passed to the rsync command and not to the ssh.

Upvotes: 2

Related Questions