为什么这个 rsync + ssh cron作业给了我'Permission denied (publickey)'错误?

分享于 

6分钟阅读

ubuntu

  繁體

问题

我经常把数据备份到本地驱动器,每天同步到远程服务器。

我使用cron和rsync,并且所有命令都可以单独工作,但合并一起时失败。


env -i sh -c"rsync -lrstRO --delete --exclude 'lost+found' /Backups/auto-daily-backups/./ backups-only@XX.XX.XX.XX:/backups/desktop/"



它返回错误


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.0]



  • ps aux | grep cron
  • Sep 7 13:22:01 desktop CRON[6735]: (tom) CMD (sh /home/tom/Documents/Scripts/offsite-backup)

  • rsync -lrstRO --delete --exclude 'lost+found' /Backups/auto-daily-backups/./ backups-only@XX.XX.XX.XX:/backups/desktop/
  • rsync -lrstRO --delete --exclude 'lost+found' -e 'ssh -i /home/tom/.ssh/backups-only' /Backups/auto-daily-backups/./ backups-only@XX.XX.XX.XX:/backups/desktop/

  • echo"Hello world" > ~/Desktop/test.txt

  • 这是crontab文件和它调用的脚本。

    
    ...
    
    
    # m h dom mon dow command
    
    
    MAILTO=""
    
    
    * * * * * sh /home/tom/Documents/Scripts/offsite-backup
    
    
    
    

    
    #!/bin/bash
    
    
    
    rsync -lrstRO --delete --exclude 'lost+found' /Backups/auto-daily-backups/./ backups-only@XX.XX.XX.XX:/backups/desktop/
    
    
    
    

    Sep 11 08:23:01 <hostname> CRON[24421]: pam_unix(cron:session): session closed for user root

    我将它作为用户'汤姆'运行在桌面上,与创建cron作业的用户一样,下面是对应的服务器日志的输出

    
    Sep 11 08:35:31 <hostname> sshd[25071]: error: Could not load host key: /etc/ssh/ssh_host_ed25519_key
    
    
    Sep 11 08:35:32 <hostname> sshd[25071]: Accepted publickey for backups-only from <desktop IP> port 54242 ssh2: RSA e2:e6:07:27:c1:continues...
    
    
    Sep 11 08:35:32 <hostname> sshd[25071]: pam_unix(sshd:session): session opened for user backups-only by (uid=0)
    
    
    Sep 11 08:35:32 <hostname> systemd-logind[638]: New session 12 of user backups-only.
    
    
    Sep 11 08:36:00 <hostname> sshd[25133]: Received disconnect from <desktop IP>: 11: disconnected by user
    
    
    Sep 11 08:36:00 <hostname> sshd[25071]: pam_unix(sshd:session): session closed for user backups-only
    
    
    
    

  • 答案1

    由于命令行的一切工作正常,错误Permission denied (publickey)表示rsync的SSH部分使用的标识文件与指定的用户名不同。

    可以在rsync命令中使用 -e 'ssh -i /path/to/identity.file' ...

    使用下面的命令启动cron中的新环境,并指定文件的完整路径解决了这个问题:

    
    env -i sh -c"rsync -lrstRO --delete --exclude 'lost+found' -e 'ssh -i /home/tom/.ssh/backups-only' /Backups/auto-daily-backups/./ backups-only@XX.XX.XX.XX:/backups/desktop/"
    
    
    
    

    我仍然很感兴趣,


    答案2

    
    rm ~/.ssh/known_hosts
    
    
    
    

    答案3

    rrsync脚本与专用ssh密钥一起使用,如下所示:

    远程服务器

    
    mkdir ~/bin
    
    
    gunzip /usr/share/doc/rsync/scripts/rrsync.gz -c > ~/bin/rrsync
    
    
    chmod +x ~/bin/rrsync
    
    
    
    

    本地计算机

    
    ssh-keygen -f ~/.ssh/id_remote_backup -C"Automated remote backup" #NO passphrase
    
    
    scp ~/.ssh/id_remote_backup.pub devel@10.10.10.83:/home/devel/.ssh
    
    
    
    

    远程计算机

    
    cat id_remote_backup.pub >> authorized_keys
    
    
    
    

    在新添加的行前面添加以下行

    
    command="$HOME/bin/rrsync -ro ~/backups/",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding
    
    
    
    

    结果看起来像

    
    command="$HOME/bin/rrsync -ro ~/backups/",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding ssh-rsa AAA...vp Automated remote backup
    
    
    
    

    本地

    使用crontabx权限放入以下脚本:

    
    #!/bin/sh
    
    
    echo""
    
    
    echo""
    
    
    echo"CRON:" `date`
    
    
    set -xv
    
    
    rsync -e"ssh -i $HOME/.ssh/id_remote_backup" -avzP devel@10.10.10.83:/ /home/user/servidor 
    
    
    
    

    来源:


    答案4

    尝试通过这种方式可以获得verbose模式,其中包含一些有用的信息。

    
    -v Verbose mode. Causes ssh to print debugging messages about its progress. This is helpful in debugging connection,
    
    
     authentication, and configuration problems. Multiple -v options increase the verbosity. The maximum is 3.
    
    
    
    

    答案5

    我认为你没有正确配置sshd_config文件,验证远程维护的PermitRootLogin yesPubkeyAuthentication yes



    Public  job  SSH  errors  CRON  Rsync