Copy files using rsync as root when the remote host does not allow root access and ssh is on a different port

  • Step 1: Make sure you can execute commands as root on the remote host after logging in, without entering your password. For this there are two alternative options:
    • You can allow the user to execute sudo without entring the users password. You can do this by adding the following line to the end of /etc/sudoers:
      •      ALL=NOPASSWD: ALL
    • You can enter the sudo password in advance once and make sure it is cached between sessions. To make sure it is cached between sessions you must add the '!tty_tickets' option to the Defaults line in /etc/sudoers, and then you must run a dummy command as root:
      • Defaults        env_reset,!tty_tickets
      • ssh -p PORT -t USER@REMOTE_HOST sudo id
  • Step 2: Do the rsync. Override the ssh port. Specify "sudo rsync" as remote rsync command. Target the rsync at the non-root user which can now sudo:
    •  sudo rsync -avRe "ssh -p PORT" --rsync-path "sudo rsync" LOCAL_DIR USER@REMOTE_HOST: