两天前我开始使用 SSH 将两台 Linux 服务器的备份传输到另一台。我创建了一个 shell 脚本以在 Crontab 中运行它。
两台服务器都使用Ubuntu 14.04.5 LTS
我的 SQL-Server 运行良好,它确实创建了数据库的备份,然后运行 SSH 脚本将文件发送到备份服务器。
现在,网页服务器是我遇到问题的地方。如果我手动使用脚本,它就可以工作,但在 cron 中它不起作用。我甚至备份了 .ssh 文件夹,并执行了与 SQL-Server 完全相同的步骤,它放的是
Host key verification failed.
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]
备份脚本如下,名为backup.sh:
#!/bin/sh
/usr/bin/rsync -e 'ssh -i /home/hostUser/.ssh/id_rsa' -avzp --delete /path/folder/ userRemote@IP:/path/to/backup/
这就是我的 Cron 任务
00 00 * * * sh /home/hostUser/backup.sh >> /var/log/backup.log 2>&1
我使用以下方法生成了 rsa 密钥:
ssh-keygen -t rsa
我必须在哪里输入来保存密钥。
我建立连接的方式如下:
ssh 'remoteUser@IP'
并使用以下命令复制密钥:
ssh-copy-id -i ~/.ssh/id_rsa.pub remoteUser@IP
我已完成这些步骤以便能够远程登录。使用 ssh -v userRemote@IP
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to remoteUserIP [remoteUserIP] port 22.
debug1: Connection established.
debug1: identity file /home/hostUser/.ssh/id_rsa type 1
debug1: identity file /home/hostUser/.ssh/id_rsa-cert type -1
debug1: identity file /home/hostUser/.ssh/id_dsa type -1
debug1: identity file /home/hostUser/.ssh/id_dsa-cert type -1
debug1: identity file /home/hostUser/.ssh/id_ecdsa type -1
debug1: identity file /home/hostUser/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/hostUser/.ssh/id_ed25519 type -1
debug1: identity file /home/hostUser/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.10
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.10 pat OpenSSH_6.6.1* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr [email protected] none
debug1: kex: client->server aes128-ctr [email protected] none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 73:de:35:34:54:46:00:f1:c6:1c:8e:bf:19:50:45:bc
debug1: Host 'remoteUserIP' is known and matches the ECDSA host key.
debug1: Found key in /home/hostUser/.ssh/known_hosts:1
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/hostUser/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to remoteUserIP ([remoteUserIP]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = es_MX.UTF-8
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-31-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Fri Aug 17 10:38:39 CDT 2018
System load: 0.0 Processes: 135
Usage of /: 65.4% of 1.70TB Users logged in: 0
Memory usage: 19% IP address for em1: remoteUserIP
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
72 packages can be updated.
9 updates are security updates.
New release '16.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
*** Es necesario reiniciar el sistema ***
Last login: Fri Aug 17 10:37:18 2018 from hostUserIP
我怎样才能让它工作,这很奇怪,因为我可以让它在我的 SQL-Server 上运行。
提前致谢。
答案1
作为运行备份的用户;
ssh -i /home/hostUser/.ssh/id_rsa 'remoteUser@IP'
这应该确保使用~/.ssh/known_hosts
服务器的 rsa 密钥进行更新。