在 cron 作业中通过 ssh 进行 Rsync:连接失败

在 cron 作业中通过 ssh 进行 Rsync:连接失败

我创建了一个脚本,以便通过 ssh 备份我的文件。使用的身份验证方法是无密码的 rsa 密钥。

#!/bin/env bash

LOG_FILE="/home/toto/bin/backups.log"
DEST="/dest/"
IP=10.1.0.5

host_is_up () {
  ping -c 1 -W 2 $1 &> /dev/null
}

rsync_backup () {
  # add --dry-run to test with false rsync
  rsync --dry-run -r -a -v -e "ssh -i /home/toto/.ssh/backup.id_rsa -v -o StrictHostKeyChecking=no" --delete $1 toto@$IP:$DEST --log-file=$LOG_FILE > /home/toto/bin/cron_output.log 2>&1
}

SRC="/home/toto/Music"


date > $LOG_FILE
if host_is_up $IP
then
  rsync_backup $SRC
else
  echo "Host $IP seems to be down" >> $LOG_FILE
fi

当我尝试启动该脚本时,一切都很好,备份已完成,我不需要添加密码。

当我尝试在 cron 中使用此脚本时出现了问题:

*/2 * * * * ~/bin/backups.sh

这是行不通的:

我有以下日志

OpenSSH_8.0p1, OpenSSL 1.1.1b  26 Feb 2019
debug1: Reading configuration data /home/toto/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 10.1.0.5 [10.1.0.5] port 22.
debug1: Connection established.
debug1: identity file /home/toto/.ssh/backup.id_rsa type 0
debug1: identity file /home/toto/.ssh/backup.id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.0
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.0
debug1: match: OpenSSH_8.0 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 10.1.0.5:22 as 'toto'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: #################################################################################
debug1: Host '10.1.0.5' is known and matches the ECDSA host key.
debug1: Found key in /home/toto/.ssh/known_hosts:10
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/toto/.ssh/backup.id_rsa RSA SHA256:############################################### explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /home/toto/.ssh/backup.id_rsa RSA SHA256:################################################# explicit
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Authentications that can continue: publickey,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Authentications that can continue: publickey,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Authentications that can continue: publickey,password
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.3]

答案1

当 xxxx.pub 文件存在时,ssh 命令会尝试使用该文件进行身份验证。

例如,如果由于某种原因您的 backup.id_rsa.pub 未从您的私钥 backup.id_rsa 正确创建,则公钥认证将失败,并出现以下几行:

debug1: Offering public key: /home/toto/.ssh/backup.id_rsa RSA SHA256:################################################# explicit
debug1: Authentications that can continue: publickey,password

如果重命名或删除 /home/toto/.ssh/backup.id_rsa.pub,会发生什么?

相关内容