【问题标题】:rsync with ssh without using credentials stored in ~/.ssh/configrsync 与 ssh 不使用存储在 ~/.ssh/config 中的凭据
【发布时间】:2017-11-04 21:21:27
【问题描述】:

我有一个传输文件的脚本。每次我运行它都需要连接到不同的主机。这就是我将主机添加为参数的原因。

脚本执行为:./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/

我可以正常连接$SSH中存储的ssh连接,但是rsync连接尝试失败,因为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]

rsync 连接的正确语法是什么?

【问题讨论】:

    标签: bash ssh rsync


    【解决方案1】:

    rsync 行之前写set -x 并观察参数是如何展开的。我相信会错的。

    您需要将带有参数(不带主机名)的ssh 命令括在引号中,否则参数将传递给rsync 命令而不是ssh

    【讨论】:

    • 你是对的,$SSH 需要被封装。接受这个答案是正确的,因为你指出了我正确的方向。
    【解决方案2】:

    Jakuje 之后我的解决方案为我指明了正确的方向:

    #!/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
    

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 2016-10-14
      • 2017-11-06
      • 1970-01-01
      • 2022-07-10
      相关资源
      最近更新 更多