Search code examples
macossshrsync

OS X run rsync under sudo using ssh_config


I want to set up an automatic rsync job to backup a bunch of user accounts on my OS X machine to a linux fileserver. I have set up password-free ssh from my account to another machine, and it works great, so I tried using this command:

sudo rsync -avz /Users/jbloggs myserv:/var/Backup/

where myserv is an alias set up in my ~/.ssh/config. The problem I have is that I have to use sudo to get that command to work -- under my personal account I don't have access to the other users' home directories to copy files for backup. That command works fine on my own account without sudo, but when I run under sudo it's not looking at my ~/.ssh/config any more (so it complains about "unknown host myserv").

How can I get the rsync running under sudo to still look at my personal ~/.ssh/config?

Thanks!


Solution

  • You can use

    ssh -i /Users/myuser/.ssh/id_rsa -F /Users/myuser/.ssh/config login@host
    

    to let ssh use your config / key files. Use "-v" to check which file it is using. You could also copy your configuration / id to /var/root/.ssh, which will be used by default when using ssh via sudo.

    To pass these options to rsync, you have to set the "--rsh" / "-e" like this:

    rsync -e "ssh -i ... -F ..."