Host key verification failed [rsync: connection unexpectedly closed]

21,315

Solution 1

Thank you all....got the answer finally. When I was running it manually I was running as a local user. But, on cron I was running it as root user, for which ssh key was not generated. I did ssh-keygen and created the keys. Now it works.

Solution 2

ssh (which rsync executes) doesn't trust the host key of the server, and since there is no interactive terminal to prompt the user it simply fails. The cause is probably that ssh is looking at a different known_hosts file when running under cron that when you execute it manually.

If the cronjob runs as a different user to you, you need to manually accept the host key by doing something like this:

sudo -u cronuser HOME=/home/cronuser ssh [email protected]

If the cronjob is running as your user then $HOME probably isn't set correctly. Specify that explicitly in the crontab:

HOME=/home/vishu
Share:
21,315

Related videos on Youtube

Rudra
Author by

Rudra

Updated on September 18, 2022

Comments

  • Rudra
    Rudra over 1 year

    I have been stuck with a peculiar problem, where rsync command is not running when it is executed through crontab.
    Below is the code :

    #!/bin/sh -x
    PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/mysql/bin:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools:~/usr/lib/jvm/jdk-6/bin
    /bin/sh /etc/profile
    MyUSER="root"       # USERNAME
    MyPASS="password"         # PASSWORD
    MyHOST="localhost"  # Hostname
    Password="" #Linux Password
    
    MYSQL="$(which mysql)"
    if [ -z "$MYSQL" ]; then
    echo "Error: MYSQL not found"
    exit 1
    fi
    MYSQLADMIN="$(which mysqladmin)"
    if [ -z "$MYSQLADMIN" ]; then
        echo "Error: MYSQLADMIN not found"
        exit 1
    fi
    CHOWN="$(which chown)"
    if [ -z "$CHOWN" ]; then
        echo "Error: CHOWN not found"
        exit 1
    fi
    CHMOD="$(which chmod)"
    if [ -z "$CHMOD" ]; then
        echo "Error: CHMOD not found"
        exit 1
    fi
    
    GZIP="$(which gzip)"
    if [ -z "$GZIP" ]; then
        echo "Error: GZIP not found"
        exit 1
    fi
    CP="$(which cp)"
    if [ -z "$CP" ]; then
        echo "Error: CP not found"
        exit 1
    fi
    MV="$(which mv)"
    if [ -z "$MV" ]; then
        echo "Error: MV not found"
        exit 1
    fi
    RM="$(which rm)"
    if [ -z "$RM" ]; then
        echo "Error: RM not found"
        exit 1
    fi
    RSYNC="$(which rsync)"
    if [ -z "$RSYNC" ]; then
        echo "Error: RSYNC not found"
        exit 1
    fi
    
    MYSQLBINLOG="$(which mysqlbinlog)"
    if [ -z "$MYSQLBINLOG" ]; then
        echo "Error: MYSQLBINLOG not found"
        exit 1
    fi
    # Get data in dd-mm-yyyy format
    NOW="$(date +"%d-%m-%Y-%T")"
    
    DEST="/home/db-backup"
    mkdir $DEST/Increment_backup.$NOW
    LATEST=$DEST/Increment_backup.$NOW
    $MYSQLADMIN -u$MyUSER -p$MyPASS flush-logs
    newestlog=`ls -d /usr/local/mysql/data/mysql-bin.?????? | sed 's/^.*\.//' | sort -g | tail -n 1`
    echo $newestlog
    for file in `ls /usr/local/mysql/data/mysql-bin.??????`
    do
            if [ "/usr/local/mysql/data/mysql-bin.$newestlog" != "$file" ]; then
         echo $file
         echo $Password | sudo -S $CHMOD 0777 $file
             #sudo $MYSQLBINLOG $file>$file.$NOW.sql
         $CP "$file" $LATEST
         #$RM "$file.$NOW.sql"
         #$MV $file.sql.gz /$LATEST
            fi
    done
    for file1 in `ls $LATEST/mysql-bin.??????`
    do
     $MYSQLBINLOG $file1>$file1.$NOW.sql 
     $GZIP -9 "$file1.$NOW.sql"     
     $RM "$file1"
    done
     $RSYNC -v -e ssh $LATEST [email protected]:/home/rsync-backup/
    #FILE=$LATEST/"mysql-bin.??????"
    #$MYSQLBINLOG $FILE>$FILE.$NOW.sql
    #$GZIP -f "$FILE.$NOW.sql"
    pwd
    

    Rsync happens when the code is run manually, but fails when it is run through crontab. Rest of the commands are working fine. From the logs I got this information:

    Host key verification failed.^M
    rsync: connection unexpectedly closed (0 bytes received so far) [sender]
    rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]
    
    • Khaled
      Khaled over 11 years
      It can be a permission problem. Are you running the cron job under same user when running the script manually??
    • Rudra
      Rudra over 11 years
      Thanks for the reply Gelraen. Fails means the script is running fine when executed manually, but in crontab nothing happens, no transfer of files, etc.
    • grassroot
      grassroot over 11 years
      Please show us your crontab entry. Additionally make sure you have error logging there also like this: '...script.sh >>/tmp/logfile 2>&1'
    • Rudra
      Rudra over 11 years
      */1 * * * * /home/db-backup/incrementaltest.sh>>/home/rsynclst 2>&1 is my cron entry. I'm getting this error Host key verification failed.^M rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]
  • Rudra
    Rudra over 11 years
    Thanks for the reply mgorven. I'm running this script as a root. The script runs with rsync when executed manually, but is failing on crontab, should I fix this with some environment variable.
  • Jenny D
    Jenny D over 11 years
    You could add the option -o StrictHostKeyChecking=no to the ssh command in your script.
  • mgorven
    mgorven over 11 years
    @Vishu Try setting HOME=/root in the crontab. Are there any prompts about SSH when running the script manually?