我需要在 crontab 中从 root 身份使用 rsync 命令在 Centos 6.6 上运行 baskup bash 脚本:rsync -avzPX --update --exclude 'www/bitrix/backup' --exclude 'www/bitrix/managed_cache' --exclude 'www/bitrix/cache' --exclude 'www/bitrix/stack_cache' -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' /home/bitrix/ [email protected]:/home/bitrix/
我愿意:
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
然后我尝试使用用户备份登录,但它仍然要求输入密码。
我究竟做错了什么?
答案1
这可能会给解决方案提供一些提示:使用 ssh 或 rsync 作为 sudo 时密钥验证不起作用
从 cron(未设置完整环境)运行时,可能需要明确指定某些内容,例如密钥。在这种情况下,我会添加-i /home/backup/.ssh/id_rsa
以确保它使用目录中的正确密钥。您没有将密钥文件传递给 ssh 命令,并且由于 USER 或 HOME 可能未在 cron 环境中设置,因此它可能未被使用。
答案2
问题出在主目录的权限上:
[root@bitrix2 home]# tail -10 /var/log/secure
Aug 9 10:19:59 bitrix2 sshd[14294]: Authentication refused: bad ownership or modes for directory /home/backup
Aug 9 10:24:13 bitrix2 sshd[14401]: Authentication refused: bad ownership or modes for directory /home/backup
Aug 9 10:24:13 bitrix2 sshd[14401]: Authentication refused: bad ownership or modes for directory /home/backup
Aug 9 10:24:14 bitrix2 sshd[14402]: Connection closed by 10.10.10.4
Aug 9 10:27:31 bitrix2 sshd[14454]: Authentication refused: bad ownership or modes for directory /home/backup
Aug 9 10:27:31 bitrix2 sshd[14454]: Authentication refused: bad ownership or modes for directory /home/backup
Aug 9 10:29:08 bitrix2 sshd[14455]: Connection closed by 10.10.10.4
Aug 9 10:29:12 bitrix2 sshd[14490]: Authentication refused: bad ownership or modes for directory /home/backup
Aug 9 10:29:12 bitrix2 sshd[14490]: Authentication refused: bad ownership or modes for directory /home/backup
Aug 9 10:31:13 bitrix2 su: pam_unix(su:session): session closed for user backup
然后我检查了权限:
[root@bitrix2 home]# ls -la backup
drwxr-x--x 4 backup backup 4096 Авг 9 10:09 backup
并赋予正常权限:
[root@bitrix2 home]# chmod go+xr-w backup
[root@bitrix2 home]# chmod a+rx-w backup
[root@bitrix2 home]# ls -la
drwxr-xr-x 4 backup backup 4096 Авг 9 10:09 backup
现在它工作正常