0

To rsync (incremental) from remote server (Centos 6.x) to local client (Ubuntu 18.04) I copied the public key I created from client to server to execute rsync without password.

PasswordAuthentication yes at sshd_config

At local Ubuntu I have some executable scripts that look like

#!/bin/sh

RSYNC=/usr/local/bin/rsync-incr
SSH=/usr/bin/ssh\ -p\ xxxx
ROTATE=60
RUSER=yyyy
RHOST=zzzz
RPATH=/path-to-remote-dir/
LPATH=/path-to-local-dir/

$RSYNC -az --rsh="$SSH" $ROTATE $RUSER@$RHOST:$RPATH $LPATH

I run this script from command line /path-to-local-script-file

and it performs okay but if I add this line to crontab -e

00 00   * * *   /path-to-local-script-file

I get a cron error "Permission denied, please try again" when it is executed

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
rsync: connection unexpectedly closed (0 bytes received so far)
[Receiver] rsync error: unexplained error (code 255) at io.c(235) [Receiver=3.1.2]
*** ERROR: rsync returned code 255

Obviusly it is a permission issue. What I don't get is why I am able to execute /path-to-local-script-file from command line and perform rsync task successfully as user myname and cannot form cron (crontab -e executed as the same user.

Edit 1: I created a new key without passphrase, deleted the old rsa-ssh line with the public key from authorized_keys at remote server.
Same behavior, ok running script from command line, permission denied from cron

Edit 2:
Server log connecting ssh from command line

May 23 10:20:53 host sshd[21067]: Accepted publickey for root from xxx.xxx.6.13 port 42836 ssh2

May 23 10:20:53 host sshd[21067]: pam_unix(sshd:session): session opened for user root by (uid=0)

Server log connecting ssh from cron

May 23 10:17:03 host sshd[18163]: Failed password for root from xxx.xxx.6.13 port 42514 ssh2

May 23 10:17:03 host sshd[18163]: Failed password for root from xxx.xxx.6.13 port 42514 ssh2

May 23 10:17:03 host sshd[18164]: Connection closed by xxx.xxx.6.13

  • The host with the script is the centos machine right? Did you check sestatus to see if selinux is enforcing? – user188737 May 23 '18 at 18:17
  • No. My desktop computer is Ubuntu 18.04 with the scripts to backup from Centos 6 remote server to it. Cron run those scripts from Ubuntu – dstonek May 23 '18 at 18:35
1

It appears that you are running the script manually as a non-root user and cron is running it as root.

If that is the case then you will need to define the path to the ssh private key.

If you are connecting as root to the remote server (bad) then you will need the root authorized_keys to have the public key of the workstation

  • Cron is not running as root at local machine. I local login as 'user1' and added a line to crontab -e. The id_rsa.pub file is located at /home/user1/.ssh/. Its content was added to .authorized_keys at remote /root/.ssh/ As said, I got connection from command line ssh or the scripts – dstonek May 23 '18 at 19:09
  • The server log from the remote server indicates that it is trying to log in as root and is trying to use username/password. You should specify the path to the private key in your script. – JohnA May 23 '18 at 19:13
  • Instead of adding the path to all the scripts I locally created /home/user1/.ssh/config with "Host *" and next line with "IdentityFile /home/user1/.ssh/id_rsa" (path to private key). I have to say I've been doing that kind of backups during 10 years an it is the first time I have to add the path to the private key. – dstonek May 23 '18 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.